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?










share|improve this question
























  • Note that N has built-in meanings.
    – Αλέξανδρος Ζεγγ
    8 hours ago










  • OK I have changed it.
    – Hubble07
    8 hours ago















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?










share|improve this question
























  • Note that N has built-in meanings.
    – Αλέξανδρος Ζεγγ
    8 hours ago










  • OK I have changed it.
    – Hubble07
    8 hours ago













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









Henrik Schumacher

46.7k466133




46.7k466133










asked 9 hours ago









Hubble07

2,883717




2,883717












  • Note that N 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










  • 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










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}







share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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}







    share|improve this answer



























      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}







      share|improve this answer

























        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}







        share|improve this answer














        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}








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 4 hours ago

























        answered 7 hours ago









        Henrik Schumacher

        46.7k466133




        46.7k466133






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre