Can this be solved even faster?
up vote
5
down vote
favorite
So I would like to solve the following set of equation for $m_i$ given a set of ${M_m,N_m}$.
$$
m_1 +m_2 +m_3 +m_4 =M_m \
|m_1| +|m_2| +|m_3| +|m_4| =N_m
$$
All variables are integers.
Also $N_m ge M_m$ and their maximum value can reach up-to 30.
I only need the total number of possible solution not the solutions themselves. So my first trivial attempt was to just use Solve
dimNM1[Nm_, Mm_] :=
Length[(Solve[m1 + m2 + m3 + m4 == Mm &&
Abs[m1] + Abs[m2] + Abs[m3] + Abs[m4] == Nm, {m1, m2, m3, m4}, Integers])]
My second slightly non-trivial attempt is the following:-
dimNM2[Nm_, Mm_] :=
Which[Nm === Mm,
Length[Partition[
Flatten[Permutations /@ IntegerPartitions[Nm, {4}, Range[0, Nm]]],
4]], True,
Module[{res},
res = Partition[
Flatten[Permutations /@ IntegerPartitions[Mm, {4}, Range[-Nm, Nm]]],
4];
Length[
Select[res, (Abs[#[[1]]] + Abs[#[[2]]] + Abs[#[[3]]] +
Abs[#[[4]]]) == Nm &]]]]
The second method is much faster than the first specially for $N_m=M_m$.
But I would like to increase the speed further for $N_mge M_m$ case if possible.
dimNM1[2, 2] // AbsoluteTiming
(*{0.177768, 10}*)
dimNM2[2, 2] // AbsoluteTiming
(*{0.0000899056, 10}*)
So is there any other way to solve these equation faster?
equation-solving performance-tuning
add a comment |
up vote
5
down vote
favorite
So I would like to solve the following set of equation for $m_i$ given a set of ${M_m,N_m}$.
$$
m_1 +m_2 +m_3 +m_4 =M_m \
|m_1| +|m_2| +|m_3| +|m_4| =N_m
$$
All variables are integers.
Also $N_m ge M_m$ and their maximum value can reach up-to 30.
I only need the total number of possible solution not the solutions themselves. So my first trivial attempt was to just use Solve
dimNM1[Nm_, Mm_] :=
Length[(Solve[m1 + m2 + m3 + m4 == Mm &&
Abs[m1] + Abs[m2] + Abs[m3] + Abs[m4] == Nm, {m1, m2, m3, m4}, Integers])]
My second slightly non-trivial attempt is the following:-
dimNM2[Nm_, Mm_] :=
Which[Nm === Mm,
Length[Partition[
Flatten[Permutations /@ IntegerPartitions[Nm, {4}, Range[0, Nm]]],
4]], True,
Module[{res},
res = Partition[
Flatten[Permutations /@ IntegerPartitions[Mm, {4}, Range[-Nm, Nm]]],
4];
Length[
Select[res, (Abs[#[[1]]] + Abs[#[[2]]] + Abs[#[[3]]] +
Abs[#[[4]]]) == Nm &]]]]
The second method is much faster than the first specially for $N_m=M_m$.
But I would like to increase the speed further for $N_mge M_m$ case if possible.
dimNM1[2, 2] // AbsoluteTiming
(*{0.177768, 10}*)
dimNM2[2, 2] // AbsoluteTiming
(*{0.0000899056, 10}*)
So is there any other way to solve these equation faster?
equation-solving performance-tuning
Note thatN
has built-in meanings.
– Αλέξανδρος Ζεγγ
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
So I would like to solve the following set of equation for $m_i$ given a set of ${M_m,N_m}$.
$$
m_1 +m_2 +m_3 +m_4 =M_m \
|m_1| +|m_2| +|m_3| +|m_4| =N_m
$$
All variables are integers.
Also $N_m ge M_m$ and their maximum value can reach up-to 30.
I only need the total number of possible solution not the solutions themselves. So my first trivial attempt was to just use Solve
dimNM1[Nm_, Mm_] :=
Length[(Solve[m1 + m2 + m3 + m4 == Mm &&
Abs[m1] + Abs[m2] + Abs[m3] + Abs[m4] == Nm, {m1, m2, m3, m4}, Integers])]
My second slightly non-trivial attempt is the following:-
dimNM2[Nm_, Mm_] :=
Which[Nm === Mm,
Length[Partition[
Flatten[Permutations /@ IntegerPartitions[Nm, {4}, Range[0, Nm]]],
4]], True,
Module[{res},
res = Partition[
Flatten[Permutations /@ IntegerPartitions[Mm, {4}, Range[-Nm, Nm]]],
4];
Length[
Select[res, (Abs[#[[1]]] + Abs[#[[2]]] + Abs[#[[3]]] +
Abs[#[[4]]]) == Nm &]]]]
The second method is much faster than the first specially for $N_m=M_m$.
But I would like to increase the speed further for $N_mge M_m$ case if possible.
dimNM1[2, 2] // AbsoluteTiming
(*{0.177768, 10}*)
dimNM2[2, 2] // AbsoluteTiming
(*{0.0000899056, 10}*)
So is there any other way to solve these equation faster?
equation-solving performance-tuning
So I would like to solve the following set of equation for $m_i$ given a set of ${M_m,N_m}$.
$$
m_1 +m_2 +m_3 +m_4 =M_m \
|m_1| +|m_2| +|m_3| +|m_4| =N_m
$$
All variables are integers.
Also $N_m ge M_m$ and their maximum value can reach up-to 30.
I only need the total number of possible solution not the solutions themselves. So my first trivial attempt was to just use Solve
dimNM1[Nm_, Mm_] :=
Length[(Solve[m1 + m2 + m3 + m4 == Mm &&
Abs[m1] + Abs[m2] + Abs[m3] + Abs[m4] == Nm, {m1, m2, m3, m4}, Integers])]
My second slightly non-trivial attempt is the following:-
dimNM2[Nm_, Mm_] :=
Which[Nm === Mm,
Length[Partition[
Flatten[Permutations /@ IntegerPartitions[Nm, {4}, Range[0, Nm]]],
4]], True,
Module[{res},
res = Partition[
Flatten[Permutations /@ IntegerPartitions[Mm, {4}, Range[-Nm, Nm]]],
4];
Length[
Select[res, (Abs[#[[1]]] + Abs[#[[2]]] + Abs[#[[3]]] +
Abs[#[[4]]]) == Nm &]]]]
The second method is much faster than the first specially for $N_m=M_m$.
But I would like to increase the speed further for $N_mge M_m$ case if possible.
dimNM1[2, 2] // AbsoluteTiming
(*{0.177768, 10}*)
dimNM2[2, 2] // AbsoluteTiming
(*{0.0000899056, 10}*)
So is there any other way to solve these equation faster?
equation-solving performance-tuning
equation-solving performance-tuning
edited 8 hours ago
Henrik Schumacher
46.7k466133
46.7k466133
asked 9 hours ago
Hubble07
2,883717
2,883717
Note thatN
has built-in meanings.
– Αλέξανδρος Ζεγγ
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago
add a comment |
Note thatN
has built-in meanings.
– Αλέξανδρος Ζεγγ
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago
Note that
N
has built-in meanings.– Αλέξανδρος Ζεγγ
8 hours ago
Note that
N
has built-in meanings.– Αλέξανδρος Ζεγγ
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
7
down vote
It is more efficient to first pick the integer partions whose absolute values sum up to n
before generating the permutations.
dimNM3[n_, m_] := Total[
Map[
Length@*Permutations,
Pick[#, Abs[#].ConstantArray[1, 4], n] &[
IntegerPartitions[m, {4}, Range[-n, n]
]
]
]
];
m = 20;
n = 40;
dimNM1[n, m] // AbsoluteTiming
dimNM2[n, m] // AbsoluteTiming
dimNM3[n, m] // AbsoluteTiming
{0.116977, 3802}
{0.995365, 3802}
{0.005579, 3802}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
It is more efficient to first pick the integer partions whose absolute values sum up to n
before generating the permutations.
dimNM3[n_, m_] := Total[
Map[
Length@*Permutations,
Pick[#, Abs[#].ConstantArray[1, 4], n] &[
IntegerPartitions[m, {4}, Range[-n, n]
]
]
]
];
m = 20;
n = 40;
dimNM1[n, m] // AbsoluteTiming
dimNM2[n, m] // AbsoluteTiming
dimNM3[n, m] // AbsoluteTiming
{0.116977, 3802}
{0.995365, 3802}
{0.005579, 3802}
add a comment |
up vote
7
down vote
It is more efficient to first pick the integer partions whose absolute values sum up to n
before generating the permutations.
dimNM3[n_, m_] := Total[
Map[
Length@*Permutations,
Pick[#, Abs[#].ConstantArray[1, 4], n] &[
IntegerPartitions[m, {4}, Range[-n, n]
]
]
]
];
m = 20;
n = 40;
dimNM1[n, m] // AbsoluteTiming
dimNM2[n, m] // AbsoluteTiming
dimNM3[n, m] // AbsoluteTiming
{0.116977, 3802}
{0.995365, 3802}
{0.005579, 3802}
add a comment |
up vote
7
down vote
up vote
7
down vote
It is more efficient to first pick the integer partions whose absolute values sum up to n
before generating the permutations.
dimNM3[n_, m_] := Total[
Map[
Length@*Permutations,
Pick[#, Abs[#].ConstantArray[1, 4], n] &[
IntegerPartitions[m, {4}, Range[-n, n]
]
]
]
];
m = 20;
n = 40;
dimNM1[n, m] // AbsoluteTiming
dimNM2[n, m] // AbsoluteTiming
dimNM3[n, m] // AbsoluteTiming
{0.116977, 3802}
{0.995365, 3802}
{0.005579, 3802}
It is more efficient to first pick the integer partions whose absolute values sum up to n
before generating the permutations.
dimNM3[n_, m_] := Total[
Map[
Length@*Permutations,
Pick[#, Abs[#].ConstantArray[1, 4], n] &[
IntegerPartitions[m, {4}, Range[-n, n]
]
]
]
];
m = 20;
n = 40;
dimNM1[n, m] // AbsoluteTiming
dimNM2[n, m] // AbsoluteTiming
dimNM3[n, m] // AbsoluteTiming
{0.116977, 3802}
{0.995365, 3802}
{0.005579, 3802}
edited 4 hours ago
answered 7 hours ago
Henrik Schumacher
46.7k466133
46.7k466133
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187608%2fcan-this-be-solved-even-faster%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note that
N
has built-in meanings.– Αλέξανδρος Ζεγγ
8 hours ago
OK I have changed it.
– Hubble07
8 hours ago