Fast element-wise division of matrix, generated from vector with `Outer`, and another matrix











up vote
4
down vote

favorite












m = {a, b, c};
n = {{e, r, t}, {y, u, i}, {g, h, j}};
k = Outer[Divide, m, m];
k/n


gives



{{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h), 
1/j}}


I want to do this with very large matrices filled with numbers of arbitrary precision. Is there a faster way?



EDIT



The sizes I am looking at for my practical applications start at 20000 and 20000^2 for the vector and matrix, respectively (of course the examples don't have to be with that many).



I am also interested in any method that might parallelise well.










share|improve this question
























  • What is the length of m in practical use?
    – Αλέξανδρος Ζεγγ
    5 hours ago










  • You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
    – C. E.
    2 hours ago










  • @ΑλέξανδροςΖεγγ I editted my question to include some information on that.
    – ThunderBiggi
    1 hour ago















up vote
4
down vote

favorite












m = {a, b, c};
n = {{e, r, t}, {y, u, i}, {g, h, j}};
k = Outer[Divide, m, m];
k/n


gives



{{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h), 
1/j}}


I want to do this with very large matrices filled with numbers of arbitrary precision. Is there a faster way?



EDIT



The sizes I am looking at for my practical applications start at 20000 and 20000^2 for the vector and matrix, respectively (of course the examples don't have to be with that many).



I am also interested in any method that might parallelise well.










share|improve this question
























  • What is the length of m in practical use?
    – Αλέξανδρος Ζεγγ
    5 hours ago










  • You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
    – C. E.
    2 hours ago










  • @ΑλέξανδροςΖεγγ I editted my question to include some information on that.
    – ThunderBiggi
    1 hour ago













up vote
4
down vote

favorite









up vote
4
down vote

favorite











m = {a, b, c};
n = {{e, r, t}, {y, u, i}, {g, h, j}};
k = Outer[Divide, m, m];
k/n


gives



{{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h), 
1/j}}


I want to do this with very large matrices filled with numbers of arbitrary precision. Is there a faster way?



EDIT



The sizes I am looking at for my practical applications start at 20000 and 20000^2 for the vector and matrix, respectively (of course the examples don't have to be with that many).



I am also interested in any method that might parallelise well.










share|improve this question















m = {a, b, c};
n = {{e, r, t}, {y, u, i}, {g, h, j}};
k = Outer[Divide, m, m];
k/n


gives



{{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h), 
1/j}}


I want to do this with very large matrices filled with numbers of arbitrary precision. Is there a faster way?



EDIT



The sizes I am looking at for my practical applications start at 20000 and 20000^2 for the vector and matrix, respectively (of course the examples don't have to be with that many).



I am also interested in any method that might parallelise well.







list-manipulation matrix performance-tuning array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 8 hours ago









ThunderBiggi

376112




376112












  • What is the length of m in practical use?
    – Αλέξανδρος Ζεγγ
    5 hours ago










  • You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
    – C. E.
    2 hours ago










  • @ΑλέξανδροςΖεγγ I editted my question to include some information on that.
    – ThunderBiggi
    1 hour ago


















  • What is the length of m in practical use?
    – Αλέξανδρος Ζεγγ
    5 hours ago










  • You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
    – C. E.
    2 hours ago










  • @ΑλέξανδροςΖεγγ I editted my question to include some information on that.
    – ThunderBiggi
    1 hour ago
















What is the length of m in practical use?
– Αλέξανδρος Ζεγγ
5 hours ago




What is the length of m in practical use?
– Αλέξανδρος Ζεγγ
5 hours ago












You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
– C. E.
2 hours ago




You can try m/(n ConstantArray[m, Length[m]]) and see how fast it is.
– C. E.
2 hours ago












@ΑλέξανδροςΖεγγ I editted my question to include some information on that.
– ThunderBiggi
1 hour ago




@ΑλέξανδροςΖεγγ I editted my question to include some information on that.
– ThunderBiggi
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote













Pretty printing your result gives...



{{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h),1/j}}//MatrixForm


$left(
begin{array}{ccc}
frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
end{array}
right)$



Try this, it avoids constructing the huge Outer[ ] matrix



Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]] // MatrixForm


$left(
begin{array}{ccc}
frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
end{array}
right)$






share|improve this answer




























    up vote
    3
    down vote













    m = RandomReal[{-1, 1}, {2000}];
    n = RandomReal[{-1, 1}, {2000, 2000}];
    a = Outer[Divide, m, m]/n; // RepeatedTiming // First
    b = Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]]; //
    RepeatedTiming // First
    c = m /(ConstantArray[m, Length[m]] n); // RepeatedTiming // First
    d = KroneckerProduct[m, 1./m]/n; // RepeatedTiming // First
    a == b == c == d



    0.958



    0.128



    0.0281



    0.0236



    True




    Edit



    A parallelized version



    cf = Compile[{{x, _Real}, {y, _Real, 1}, {z, _Real, 1}},
    x/(y z),
    CompilationTarget -> "C",
    RuntimeAttributes -> {Listable},
    Parallelization -> True,
    RuntimeOptions -> "Speed"
    ];
    e = cf[m, n, m]; // RepeatedTiming // First
    a == e



    0.0096



    True




    Timing has been measured on a Quad Core CPU which shows that this does not scale too well. Btw., the timing with CompilationTarget -> "C" is only 4% slower, so there is always no point to compile it into a library.






    share|improve this answer























    • I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
      – ThunderBiggi
      1 hour ago










    • See my edit for a parallelized version.
      – Henrik Schumacher
      1 hour ago










    • I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
      – ThunderBiggi
      28 mins ago










    • Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
      – ThunderBiggi
      23 mins ago












    • Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
      – Henrik Schumacher
      15 mins ago











    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%2f187804%2ffast-element-wise-division-of-matrix-generated-from-vector-with-outer-and-an%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Pretty printing your result gives...



    {{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h),1/j}}//MatrixForm


    $left(
    begin{array}{ccc}
    frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
    frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
    frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
    end{array}
    right)$



    Try this, it avoids constructing the huge Outer[ ] matrix



    Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]] // MatrixForm


    $left(
    begin{array}{ccc}
    frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
    frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
    frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
    end{array}
    right)$






    share|improve this answer

























      up vote
      3
      down vote













      Pretty printing your result gives...



      {{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h),1/j}}//MatrixForm


      $left(
      begin{array}{ccc}
      frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
      frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
      frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
      end{array}
      right)$



      Try this, it avoids constructing the huge Outer[ ] matrix



      Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]] // MatrixForm


      $left(
      begin{array}{ccc}
      frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
      frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
      frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
      end{array}
      right)$






      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        Pretty printing your result gives...



        {{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h),1/j}}//MatrixForm


        $left(
        begin{array}{ccc}
        frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
        frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
        frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
        end{array}
        right)$



        Try this, it avoids constructing the huge Outer[ ] matrix



        Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]] // MatrixForm


        $left(
        begin{array}{ccc}
        frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
        frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
        frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
        end{array}
        right)$






        share|improve this answer












        Pretty printing your result gives...



        {{1/e, a/(b r), a/(c t)}, {b/(a y), 1/u, b/(c i)}, {c/(a g), c/(b h),1/j}}//MatrixForm


        $left(
        begin{array}{ccc}
        frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
        frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
        frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
        end{array}
        right)$



        Try this, it avoids constructing the huge Outer[ ] matrix



        Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]] // MatrixForm


        $left(
        begin{array}{ccc}
        frac{1}{e} & frac{a}{b r} & frac{a}{c t} \
        frac{b}{a y} & frac{1}{u} & frac{b}{c i} \
        frac{c}{a g} & frac{c}{b h} & frac{1}{j} \
        end{array}
        right)$







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        MikeY

        1,762410




        1,762410






















            up vote
            3
            down vote













            m = RandomReal[{-1, 1}, {2000}];
            n = RandomReal[{-1, 1}, {2000, 2000}];
            a = Outer[Divide, m, m]/n; // RepeatedTiming // First
            b = Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]]; //
            RepeatedTiming // First
            c = m /(ConstantArray[m, Length[m]] n); // RepeatedTiming // First
            d = KroneckerProduct[m, 1./m]/n; // RepeatedTiming // First
            a == b == c == d



            0.958



            0.128



            0.0281



            0.0236



            True




            Edit



            A parallelized version



            cf = Compile[{{x, _Real}, {y, _Real, 1}, {z, _Real, 1}},
            x/(y z),
            CompilationTarget -> "C",
            RuntimeAttributes -> {Listable},
            Parallelization -> True,
            RuntimeOptions -> "Speed"
            ];
            e = cf[m, n, m]; // RepeatedTiming // First
            a == e



            0.0096



            True




            Timing has been measured on a Quad Core CPU which shows that this does not scale too well. Btw., the timing with CompilationTarget -> "C" is only 4% slower, so there is always no point to compile it into a library.






            share|improve this answer























            • I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
              – ThunderBiggi
              1 hour ago










            • See my edit for a parallelized version.
              – Henrik Schumacher
              1 hour ago










            • I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
              – ThunderBiggi
              28 mins ago










            • Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
              – ThunderBiggi
              23 mins ago












            • Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
              – Henrik Schumacher
              15 mins ago















            up vote
            3
            down vote













            m = RandomReal[{-1, 1}, {2000}];
            n = RandomReal[{-1, 1}, {2000, 2000}];
            a = Outer[Divide, m, m]/n; // RepeatedTiming // First
            b = Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]]; //
            RepeatedTiming // First
            c = m /(ConstantArray[m, Length[m]] n); // RepeatedTiming // First
            d = KroneckerProduct[m, 1./m]/n; // RepeatedTiming // First
            a == b == c == d



            0.958



            0.128



            0.0281



            0.0236



            True




            Edit



            A parallelized version



            cf = Compile[{{x, _Real}, {y, _Real, 1}, {z, _Real, 1}},
            x/(y z),
            CompilationTarget -> "C",
            RuntimeAttributes -> {Listable},
            Parallelization -> True,
            RuntimeOptions -> "Speed"
            ];
            e = cf[m, n, m]; // RepeatedTiming // First
            a == e



            0.0096



            True




            Timing has been measured on a Quad Core CPU which shows that this does not scale too well. Btw., the timing with CompilationTarget -> "C" is only 4% slower, so there is always no point to compile it into a library.






            share|improve this answer























            • I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
              – ThunderBiggi
              1 hour ago










            • See my edit for a parallelized version.
              – Henrik Schumacher
              1 hour ago










            • I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
              – ThunderBiggi
              28 mins ago










            • Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
              – ThunderBiggi
              23 mins ago












            • Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
              – Henrik Schumacher
              15 mins ago













            up vote
            3
            down vote










            up vote
            3
            down vote









            m = RandomReal[{-1, 1}, {2000}];
            n = RandomReal[{-1, 1}, {2000, 2000}];
            a = Outer[Divide, m, m]/n; // RepeatedTiming // First
            b = Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]]; //
            RepeatedTiming // First
            c = m /(ConstantArray[m, Length[m]] n); // RepeatedTiming // First
            d = KroneckerProduct[m, 1./m]/n; // RepeatedTiming // First
            a == b == c == d



            0.958



            0.128



            0.0281



            0.0236



            True




            Edit



            A parallelized version



            cf = Compile[{{x, _Real}, {y, _Real, 1}, {z, _Real, 1}},
            x/(y z),
            CompilationTarget -> "C",
            RuntimeAttributes -> {Listable},
            Parallelization -> True,
            RuntimeOptions -> "Speed"
            ];
            e = cf[m, n, m]; // RepeatedTiming // First
            a == e



            0.0096



            True




            Timing has been measured on a Quad Core CPU which shows that this does not scale too well. Btw., the timing with CompilationTarget -> "C" is only 4% slower, so there is always no point to compile it into a library.






            share|improve this answer














            m = RandomReal[{-1, 1}, {2000}];
            n = RandomReal[{-1, 1}, {2000, 2000}];
            a = Outer[Divide, m, m]/n; // RepeatedTiming // First
            b = Map[#/m &, MapThread[#1 #2 &, {m, 1/n}]]; //
            RepeatedTiming // First
            c = m /(ConstantArray[m, Length[m]] n); // RepeatedTiming // First
            d = KroneckerProduct[m, 1./m]/n; // RepeatedTiming // First
            a == b == c == d



            0.958



            0.128



            0.0281



            0.0236



            True




            Edit



            A parallelized version



            cf = Compile[{{x, _Real}, {y, _Real, 1}, {z, _Real, 1}},
            x/(y z),
            CompilationTarget -> "C",
            RuntimeAttributes -> {Listable},
            Parallelization -> True,
            RuntimeOptions -> "Speed"
            ];
            e = cf[m, n, m]; // RepeatedTiming // First
            a == e



            0.0096



            True




            Timing has been measured on a Quad Core CPU which shows that this does not scale too well. Btw., the timing with CompilationTarget -> "C" is only 4% slower, so there is always no point to compile it into a library.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            Henrik Schumacher

            47.3k466134




            47.3k466134












            • I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
              – ThunderBiggi
              1 hour ago










            • See my edit for a parallelized version.
              – Henrik Schumacher
              1 hour ago










            • I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
              – ThunderBiggi
              28 mins ago










            • Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
              – ThunderBiggi
              23 mins ago












            • Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
              – Henrik Schumacher
              15 mins ago


















            • I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
              – ThunderBiggi
              1 hour ago










            • See my edit for a parallelized version.
              – Henrik Schumacher
              1 hour ago










            • I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
              – ThunderBiggi
              28 mins ago










            • Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
              – ThunderBiggi
              23 mins ago












            • Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
              – Henrik Schumacher
              15 mins ago
















            I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
            – ThunderBiggi
            1 hour ago




            I was just typing a comparison of the answers so far, but you were first. I wouldn't've expected that KroneckerProduct would be that quick. Any ideas on any way that might parallelise well? I will edit my question to include that as well.
            – ThunderBiggi
            1 hour ago












            See my edit for a parallelized version.
            – Henrik Schumacher
            1 hour ago




            See my edit for a parallelized version.
            – Henrik Schumacher
            1 hour ago












            I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
            – ThunderBiggi
            28 mins ago




            I am using arbitrary precision numbers, so I guess 'Compile' is not really an option.
            – ThunderBiggi
            28 mins ago












            Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
            – ThunderBiggi
            23 mins ago






            Also, interestingly, but on my machine, with Mathematica 11.3, a is faster than b though still slower than the other two.
            – ThunderBiggi
            23 mins ago














            Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
            – Henrik Schumacher
            15 mins ago




            Yeah, I was also surprised that a was so slow on my machine. I don't know what to think about it...
            – Henrik Schumacher
            15 mins ago


















            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%2f187804%2ffast-element-wise-division-of-matrix-generated-from-vector-with-outer-and-an%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

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu