Will Mathematica Optimize Looping for Partial Sums?

  • Mathematica
  • Thread starter Swamp Thing
  • Start date
  • Tags
    Partial Sums
In summary, Mathematica may automatically optimize the procedure when using the DiscretePlot function to calculate a sum up to a certain value. However, it is difficult to know for sure how this optimization works behind the scenes. According to Paul Wellin's book Programming with Mathematica, the most efficient way to make a sum is to first create a list of inputs and then use the Total function instead of Sum. Another option is to use "Accumulate" to make a list of running totals or partial sums.
  • #1
Swamp Thing
Insights Author
874
519
TL;DR Summary
Does Mathematica optimize the calculation of a series of partial sums automatically?
In this example,
Code:
DiscretePlot[    Sum[   f[x],   {x,1,n}  ],{n,1,20}]
will Mathematica automatically optimize the procedure -- i.e., will it run a single loop where it calculates the sum up to 20 only once, transferring the partial sums to the output as it goes along? Assume that there is no formula for the sum of f[x], so it has to actually add the terms up one by one.
 
Physics news on Phys.org
  • #2
In my experience, it is hard to know how Mathematica works behind the scenes, so optimization can be hard.

Looking at the book Programming with Mathematica by Paul Wellin, I have found that the most efficient way to make the sum is to first create the list of input, and then use Total instead of Sum:
Code:
In[1]:= Timing[result = Table[Sum[f[x], {x, 1, n}], {n, 1000}];]
Out[1]:= {0.72136, Null}

In[2]:= Timing[x = Table[n, {n, 1000}];
 result = Table[Total[f[Take[x, n]]], {n, 1000}];]
Out[2]:= {0.009547, Null}
 
  • Informative
  • Like
Likes JD_PM and Swamp Thing

1. What does it mean to "loop through partial sums"?

Looping through partial sums is a mathematical process where a sequence of numbers is added together one by one, starting from the first number and continuing until the desired number of terms is reached. Each time a new term is added, the partial sum is recalculated and added to the previous partial sum, creating a series of increasing sums.

2. How is looping through partial sums useful in science?

In science, looping through partial sums is commonly used in data analysis to calculate running averages, moving averages, or to identify patterns in a data set. It can also be used in simulations and modeling to track changes over time.

3. What are the steps involved in looping through partial sums?

The basic steps for looping through partial sums are: 1) start with the first term in the sequence, 2) add it to the previous partial sum (which is 0 for the first term), 3) store the new partial sum, 4) repeat the process with the next term until the desired number of terms is reached.

4. Are there any limitations to looping through partial sums?

Yes, there are a few limitations to looping through partial sums. First, it can only be used on numerical data. Second, it can only be used on data that can be logically added together (e.g. you cannot add a string of text to a number). Lastly, the results may be affected by the order in which the terms are added.

5. Can looping through partial sums be applied to other mathematical operations besides addition?

Yes, the concept of looping through partial sums can be applied to other mathematical operations such as multiplication, exponentiation, or even more complex functions. However, the process may vary depending on the specific operation and the desired outcome.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
988
Replies
3
Views
944
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
26
Views
1K
Replies
6
Views
944
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top