How can I iterate this process?Possible Memory Leak Issues in NestWhileConditional Gathering of listsHow to find first list element that differs from average of N previous elements by more than a given amount?How to simplify a resistence network and represent the simplifying process?Taking derivative using some variableSort matrix rows and columns while keeping headingCombining Lists in a specified sequenceNeed to add a different random number to each part of the tableBuilding a list of products from the elements in another listEfficient way to iterate over large list and check conditions

Are there any differences in causality between linear and logistic regression?

Shabbat clothing on shabbat chazon

Generator for parity?

Can I legally make a real mobile app based on a fictional app from a TV show?

Who are these characters/superheroes in the posters from Chris's room in Family Guy?

Ex-contractor published company source code and secrets online

(11 of 11: Meta) What is Pyramid Cult's All-Time Favorite?

Converting Piecewise function to C code

How do Mogwai reproduce?

Senior dev discreetly remoting in to computer and watching a coworker

Is TA-ing worth the opportunity cost?

How to use grep to search through the --help output?

Visa National - No Exit Stamp From France on Return to the UK

How does The Fools Guild make its money?

How can glass marbles naturally occur in a desert?

Could one become a successful researcher by writing some really good papers while being outside academia?

What method to use in a batch apex in order to get authentication token from a remote server?

Was the 2019 Lion King film made through motion capture?

Was this a rapid SCHEDULED disassembly? How was it done?

Why should we care about syntactic proofs if we can show semantically that statements are true?

How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?

As a 16 year old, how can I keep my money safe from my mother?

Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?

How can I tell if a flight itinerary is fake?



How can I iterate this process?


Possible Memory Leak Issues in NestWhileConditional Gathering of listsHow to find first list element that differs from average of N previous elements by more than a given amount?How to simplify a resistence network and represent the simplifying process?Taking derivative using some variableSort matrix rows and columns while keeping headingCombining Lists in a specified sequenceNeed to add a different random number to each part of the tableBuilding a list of products from the elements in another listEfficient way to iterate over large list and check conditions






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5












$begingroup$


 a = RandomVariate[UniformDistribution[0, 1]];
b = RandomVariate[UniformDistribution[0, 1]];
c = RandomVariate[UniformDistribution[0, 1]];
a + a b + a b c


I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?



Best regards
Geoffrey Critzer










share|improve this question









New contributor



geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




















    5












    $begingroup$


     a = RandomVariate[UniformDistribution[0, 1]];
    b = RandomVariate[UniformDistribution[0, 1]];
    c = RandomVariate[UniformDistribution[0, 1]];
    a + a b + a b c


    I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?



    Best regards
    Geoffrey Critzer










    share|improve this question









    New contributor



    geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$
















      5












      5








      5





      $begingroup$


       a = RandomVariate[UniformDistribution[0, 1]];
      b = RandomVariate[UniformDistribution[0, 1]];
      c = RandomVariate[UniformDistribution[0, 1]];
      a + a b + a b c


      I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?



      Best regards
      Geoffrey Critzer










      share|improve this question









      New contributor



      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$




       a = RandomVariate[UniformDistribution[0, 1]];
      b = RandomVariate[UniformDistribution[0, 1]];
      c = RandomVariate[UniformDistribution[0, 1]];
      a + a b + a b c


      I want to continue picking random reals in (0,1) and adding the product to the previous sum. How can Mathematica help me here?



      Best regards
      Geoffrey Critzer







      list-manipulation iteration






      share|improve this question









      New contributor



      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 8 hours ago









      C. E.

      53.8k3 gold badges103 silver badges211 bronze badges




      53.8k3 gold badges103 silver badges211 bronze badges






      New contributor



      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      geoffreygeoffrey

      261 bronze badge




      261 bronze badge




      New contributor



      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      geoffrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          2 Answers
          2






          active

          oldest

          votes


















          8












          $begingroup$

          Here's one way:



          SeedRandom[1];
          a = RandomVariate[UniformDistribution[0, 1]];
          b = RandomVariate[UniformDistribution[0, 1]];
          c = RandomVariate[UniformDistribution[0, 1]];
          a + a b + a b c



          0.980367




          SeedRandom[1];
          values = RandomVariate[UniformDistribution[0, 1], 3];
          Total@FoldList[Times, values]



          0.980367




          The number 3 can be replaced by any number, however many times you want to iterate.



          Here's a procedural solution (with the definition of values as in the previous example):



          prod = First[values];
          sum = First[values];
          Do[
          prod *= v;
          sum += prod,
          v, Rest[values]
          ];
          sum



          0.980367







          share|improve this answer











          $endgroup$






















            4












            $begingroup$

            C.E.'s answer is great already. I would just like to point out that we may exploit here that floating point addition is usually significantly faster than floating point multiplication that FoldList is just slow, and that multiplication can be cast into addition by applying Log so that we can use Accumulate instead. Morever, we may use vectorized built-in routines for that.



            n = 1000000;
            values = RandomVariate[UniformDistribution[0, 1], n];

            r1 = Total@FoldList[Times, values]; // RepeatedTiming // First
            r2 = Total[Exp[Clip[Accumulate[Log[values]], -700., ∞]]]; // RepeatedTiming // First

            Max[Abs[r1 - r2]]



            0.070



            0.0053



            0.




            For those who wonder what the Clip is for: This is in order to prevent underflow error handling to occurr (the latter slows down things considerably); that happens at about Exp[-709.] or so.



            Edit



            It is even faster to write a short compiled version of C.E.'s procedure (if do not count in the compilation time):



            cf = Compile[x, _Real, 1,
            Block[prod = 1., sum = 0.,
            Do[prod *= Compile`GetElement[x, i]; sum += r, i, 1, Length[x]];
            sum
            ],
            CompilationTarget -> "C"
            ];


            Now:



            r3 = cf[values]; // RepeatedTiming // First
            Max[Abs[r1 - r3]]



            0.0013



            1.77636*10^-15




            Remark



            I formerly claimed that floating point multiplication were slower than floating point addition. As Roman pointed out, that is not correct. While multiplication probably has higher complexity (and with floating point computations, some quite counterintuitive things happen), modern hardware is built such that variuous steps of the multiplication are performed in parallel. Nowadays, there is even a single circuit for fused multiply-add (FMA) and not necessarily any separated addition circuit, so addition and multiplication should take basically the same time.






            share|improve this answer











            $endgroup$










            • 1




              $begingroup$
              On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
              $endgroup$
              – Roman
              4 hours ago











            • $begingroup$
              @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
              $endgroup$
              – Henrik Schumacher
              3 hours ago










            • $begingroup$
              @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
              $endgroup$
              – Henrik Schumacher
              3 hours ago










            • $begingroup$
              Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
              $endgroup$
              – Henrik Schumacher
              3 hours ago










            • $begingroup$
              Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
              $endgroup$
              – Roman
              3 hours ago














            Your Answer








            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',
            autoActivateHeartbeat: false,
            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
            );



            );






            geoffrey is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f203562%2fhow-can-i-iterate-this-process%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









            8












            $begingroup$

            Here's one way:



            SeedRandom[1];
            a = RandomVariate[UniformDistribution[0, 1]];
            b = RandomVariate[UniformDistribution[0, 1]];
            c = RandomVariate[UniformDistribution[0, 1]];
            a + a b + a b c



            0.980367




            SeedRandom[1];
            values = RandomVariate[UniformDistribution[0, 1], 3];
            Total@FoldList[Times, values]



            0.980367




            The number 3 can be replaced by any number, however many times you want to iterate.



            Here's a procedural solution (with the definition of values as in the previous example):



            prod = First[values];
            sum = First[values];
            Do[
            prod *= v;
            sum += prod,
            v, Rest[values]
            ];
            sum



            0.980367







            share|improve this answer











            $endgroup$



















              8












              $begingroup$

              Here's one way:



              SeedRandom[1];
              a = RandomVariate[UniformDistribution[0, 1]];
              b = RandomVariate[UniformDistribution[0, 1]];
              c = RandomVariate[UniformDistribution[0, 1]];
              a + a b + a b c



              0.980367




              SeedRandom[1];
              values = RandomVariate[UniformDistribution[0, 1], 3];
              Total@FoldList[Times, values]



              0.980367




              The number 3 can be replaced by any number, however many times you want to iterate.



              Here's a procedural solution (with the definition of values as in the previous example):



              prod = First[values];
              sum = First[values];
              Do[
              prod *= v;
              sum += prod,
              v, Rest[values]
              ];
              sum



              0.980367







              share|improve this answer











              $endgroup$

















                8












                8








                8





                $begingroup$

                Here's one way:



                SeedRandom[1];
                a = RandomVariate[UniformDistribution[0, 1]];
                b = RandomVariate[UniformDistribution[0, 1]];
                c = RandomVariate[UniformDistribution[0, 1]];
                a + a b + a b c



                0.980367




                SeedRandom[1];
                values = RandomVariate[UniformDistribution[0, 1], 3];
                Total@FoldList[Times, values]



                0.980367




                The number 3 can be replaced by any number, however many times you want to iterate.



                Here's a procedural solution (with the definition of values as in the previous example):



                prod = First[values];
                sum = First[values];
                Do[
                prod *= v;
                sum += prod,
                v, Rest[values]
                ];
                sum



                0.980367







                share|improve this answer











                $endgroup$



                Here's one way:



                SeedRandom[1];
                a = RandomVariate[UniformDistribution[0, 1]];
                b = RandomVariate[UniformDistribution[0, 1]];
                c = RandomVariate[UniformDistribution[0, 1]];
                a + a b + a b c



                0.980367




                SeedRandom[1];
                values = RandomVariate[UniformDistribution[0, 1], 3];
                Total@FoldList[Times, values]



                0.980367




                The number 3 can be replaced by any number, however many times you want to iterate.



                Here's a procedural solution (with the definition of values as in the previous example):



                prod = First[values];
                sum = First[values];
                Do[
                prod *= v;
                sum += prod,
                v, Rest[values]
                ];
                sum



                0.980367








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 8 hours ago

























                answered 8 hours ago









                C. E.C. E.

                53.8k3 gold badges103 silver badges211 bronze badges




                53.8k3 gold badges103 silver badges211 bronze badges


























                    4












                    $begingroup$

                    C.E.'s answer is great already. I would just like to point out that we may exploit here that floating point addition is usually significantly faster than floating point multiplication that FoldList is just slow, and that multiplication can be cast into addition by applying Log so that we can use Accumulate instead. Morever, we may use vectorized built-in routines for that.



                    n = 1000000;
                    values = RandomVariate[UniformDistribution[0, 1], n];

                    r1 = Total@FoldList[Times, values]; // RepeatedTiming // First
                    r2 = Total[Exp[Clip[Accumulate[Log[values]], -700., ∞]]]; // RepeatedTiming // First

                    Max[Abs[r1 - r2]]



                    0.070



                    0.0053



                    0.




                    For those who wonder what the Clip is for: This is in order to prevent underflow error handling to occurr (the latter slows down things considerably); that happens at about Exp[-709.] or so.



                    Edit



                    It is even faster to write a short compiled version of C.E.'s procedure (if do not count in the compilation time):



                    cf = Compile[x, _Real, 1,
                    Block[prod = 1., sum = 0.,
                    Do[prod *= Compile`GetElement[x, i]; sum += r, i, 1, Length[x]];
                    sum
                    ],
                    CompilationTarget -> "C"
                    ];


                    Now:



                    r3 = cf[values]; // RepeatedTiming // First
                    Max[Abs[r1 - r3]]



                    0.0013



                    1.77636*10^-15




                    Remark



                    I formerly claimed that floating point multiplication were slower than floating point addition. As Roman pointed out, that is not correct. While multiplication probably has higher complexity (and with floating point computations, some quite counterintuitive things happen), modern hardware is built such that variuous steps of the multiplication are performed in parallel. Nowadays, there is even a single circuit for fused multiply-add (FMA) and not necessarily any separated addition circuit, so addition and multiplication should take basically the same time.






                    share|improve this answer











                    $endgroup$










                    • 1




                      $begingroup$
                      On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                      $endgroup$
                      – Roman
                      4 hours ago











                    • $begingroup$
                      @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                      $endgroup$
                      – Roman
                      3 hours ago
















                    4












                    $begingroup$

                    C.E.'s answer is great already. I would just like to point out that we may exploit here that floating point addition is usually significantly faster than floating point multiplication that FoldList is just slow, and that multiplication can be cast into addition by applying Log so that we can use Accumulate instead. Morever, we may use vectorized built-in routines for that.



                    n = 1000000;
                    values = RandomVariate[UniformDistribution[0, 1], n];

                    r1 = Total@FoldList[Times, values]; // RepeatedTiming // First
                    r2 = Total[Exp[Clip[Accumulate[Log[values]], -700., ∞]]]; // RepeatedTiming // First

                    Max[Abs[r1 - r2]]



                    0.070



                    0.0053



                    0.




                    For those who wonder what the Clip is for: This is in order to prevent underflow error handling to occurr (the latter slows down things considerably); that happens at about Exp[-709.] or so.



                    Edit



                    It is even faster to write a short compiled version of C.E.'s procedure (if do not count in the compilation time):



                    cf = Compile[x, _Real, 1,
                    Block[prod = 1., sum = 0.,
                    Do[prod *= Compile`GetElement[x, i]; sum += r, i, 1, Length[x]];
                    sum
                    ],
                    CompilationTarget -> "C"
                    ];


                    Now:



                    r3 = cf[values]; // RepeatedTiming // First
                    Max[Abs[r1 - r3]]



                    0.0013



                    1.77636*10^-15




                    Remark



                    I formerly claimed that floating point multiplication were slower than floating point addition. As Roman pointed out, that is not correct. While multiplication probably has higher complexity (and with floating point computations, some quite counterintuitive things happen), modern hardware is built such that variuous steps of the multiplication are performed in parallel. Nowadays, there is even a single circuit for fused multiply-add (FMA) and not necessarily any separated addition circuit, so addition and multiplication should take basically the same time.






                    share|improve this answer











                    $endgroup$










                    • 1




                      $begingroup$
                      On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                      $endgroup$
                      – Roman
                      4 hours ago











                    • $begingroup$
                      @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                      $endgroup$
                      – Roman
                      3 hours ago














                    4












                    4








                    4





                    $begingroup$

                    C.E.'s answer is great already. I would just like to point out that we may exploit here that floating point addition is usually significantly faster than floating point multiplication that FoldList is just slow, and that multiplication can be cast into addition by applying Log so that we can use Accumulate instead. Morever, we may use vectorized built-in routines for that.



                    n = 1000000;
                    values = RandomVariate[UniformDistribution[0, 1], n];

                    r1 = Total@FoldList[Times, values]; // RepeatedTiming // First
                    r2 = Total[Exp[Clip[Accumulate[Log[values]], -700., ∞]]]; // RepeatedTiming // First

                    Max[Abs[r1 - r2]]



                    0.070



                    0.0053



                    0.




                    For those who wonder what the Clip is for: This is in order to prevent underflow error handling to occurr (the latter slows down things considerably); that happens at about Exp[-709.] or so.



                    Edit



                    It is even faster to write a short compiled version of C.E.'s procedure (if do not count in the compilation time):



                    cf = Compile[x, _Real, 1,
                    Block[prod = 1., sum = 0.,
                    Do[prod *= Compile`GetElement[x, i]; sum += r, i, 1, Length[x]];
                    sum
                    ],
                    CompilationTarget -> "C"
                    ];


                    Now:



                    r3 = cf[values]; // RepeatedTiming // First
                    Max[Abs[r1 - r3]]



                    0.0013



                    1.77636*10^-15




                    Remark



                    I formerly claimed that floating point multiplication were slower than floating point addition. As Roman pointed out, that is not correct. While multiplication probably has higher complexity (and with floating point computations, some quite counterintuitive things happen), modern hardware is built such that variuous steps of the multiplication are performed in parallel. Nowadays, there is even a single circuit for fused multiply-add (FMA) and not necessarily any separated addition circuit, so addition and multiplication should take basically the same time.






                    share|improve this answer











                    $endgroup$



                    C.E.'s answer is great already. I would just like to point out that we may exploit here that floating point addition is usually significantly faster than floating point multiplication that FoldList is just slow, and that multiplication can be cast into addition by applying Log so that we can use Accumulate instead. Morever, we may use vectorized built-in routines for that.



                    n = 1000000;
                    values = RandomVariate[UniformDistribution[0, 1], n];

                    r1 = Total@FoldList[Times, values]; // RepeatedTiming // First
                    r2 = Total[Exp[Clip[Accumulate[Log[values]], -700., ∞]]]; // RepeatedTiming // First

                    Max[Abs[r1 - r2]]



                    0.070



                    0.0053



                    0.




                    For those who wonder what the Clip is for: This is in order to prevent underflow error handling to occurr (the latter slows down things considerably); that happens at about Exp[-709.] or so.



                    Edit



                    It is even faster to write a short compiled version of C.E.'s procedure (if do not count in the compilation time):



                    cf = Compile[x, _Real, 1,
                    Block[prod = 1., sum = 0.,
                    Do[prod *= Compile`GetElement[x, i]; sum += r, i, 1, Length[x]];
                    sum
                    ],
                    CompilationTarget -> "C"
                    ];


                    Now:



                    r3 = cf[values]; // RepeatedTiming // First
                    Max[Abs[r1 - r3]]



                    0.0013



                    1.77636*10^-15




                    Remark



                    I formerly claimed that floating point multiplication were slower than floating point addition. As Roman pointed out, that is not correct. While multiplication probably has higher complexity (and with floating point computations, some quite counterintuitive things happen), modern hardware is built such that variuous steps of the multiplication are performed in parallel. Nowadays, there is even a single circuit for fused multiply-add (FMA) and not necessarily any separated addition circuit, so addition and multiplication should take basically the same time.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 3 hours ago

























                    answered 8 hours ago









                    Henrik SchumacherHenrik Schumacher

                    67k5 gold badges95 silver badges185 bronze badges




                    67k5 gold badges95 silver badges185 bronze badges










                    • 1




                      $begingroup$
                      On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                      $endgroup$
                      – Roman
                      4 hours ago











                    • $begingroup$
                      @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                      $endgroup$
                      – Roman
                      3 hours ago













                    • 1




                      $begingroup$
                      On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                      $endgroup$
                      – Roman
                      4 hours ago











                    • $begingroup$
                      @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                      $endgroup$
                      – Henrik Schumacher
                      3 hours ago










                    • $begingroup$
                      Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                      $endgroup$
                      – Roman
                      3 hours ago








                    1




                    1




                    $begingroup$
                    On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                    $endgroup$
                    – Roman
                    4 hours ago





                    $begingroup$
                    On a modern CPU, multiplication is not slower than addition. See, e,g., this discussion that's already ten years old. Single-clock-cycle multipliers have been around for a while now.
                    $endgroup$
                    – Roman
                    4 hours ago













                    $begingroup$
                    @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago




                    $begingroup$
                    @Roman Yes, actually, I've read many things like that today, in particular about things that were introduce with the Haswell. Now I am not sure anymore why the Accumulate code is faster than the FoldList.
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago












                    $begingroup$
                    @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago




                    $begingroup$
                    @Roman I am still working with a Haswell processor and there the situation is such that both ADD and FMA (which is used there for multiplication on this architecture IIRC; since Skylake, FMA is used for both) have the same throughput but ADD has significantly less latency. And that might make the difference: We are not multipling or adding two vectors componentwise (in which case it is only about throughput, not about latency), we are indeed folding here - and that should also depend on latency, IMHO.
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago












                    $begingroup$
                    Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago




                    $begingroup$
                    Maybe the sole reason for the timing differences here is the simple fact that there is no such things as an efficiently implemented multiplicative variant of Accumulate in Mathematica...
                    $endgroup$
                    – Henrik Schumacher
                    3 hours ago












                    $begingroup$
                    Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                    $endgroup$
                    – Roman
                    3 hours ago





                    $begingroup$
                    Maybe related to this discussion about the memory hogging of NestWhile? Could it be that in general, nesting and folding are just not that efficient? After all, Accumulate[x] is ten or twenty times faster than FoldList[Plus, x], whereas FoldList[Times, x] is only imperceptibly slower than FoldList[Plus, x].
                    $endgroup$
                    – Roman
                    3 hours ago











                    geoffrey is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    geoffrey is a new contributor. Be nice, and check out our Code of Conduct.












                    geoffrey is a new contributor. Be nice, and check out our Code of Conduct.











                    geoffrey is a new contributor. Be nice, and check out our Code of Conduct.














                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f203562%2fhow-can-i-iterate-this-process%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

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480