# Mathematica: Summation in the Do-Loop

• Mathematica
• kaizen.moto
In summary: I will try again to see if I can find a way to get all the code to work without seeing your results. I will try to make a new suggestion here soon.
kaizen.moto
Dear all,

Iam trying to get the right codes so that the summation could be done in the Do-Loop function using Mathematica.

The problem is that I could not get the summation for sumU1 and sumV1 to start from 1 using my codes as attached (Summation2.nb).

Please help me to improve the codes for sumU1 and sumV1 {that are put in the remark).

Thank you in advance.

#### Attachments

• Summation2.nb
72.3 KB · Views: 519
Perhaps you would repost your notebook using a Print to show the values of disp0, u0, v0, uuu1,sumU1, vvv1 and sumV1 only for {m,n}={5,7} or some other small pair of m,n that shows as simply as possible what you are getting and then carefully point out exactly where the error is and what you need instead. You might use If[m==5&&n==7,Print[...]] or something similar.

If this would be multiple pages for each expression and could not be easily understood then please ignore this request.

Thank you

I have now attached the revised codes with some outputs for one typical case, i.e when (m,n) -> (5,7).

Please help me to get the right codes for sumU1 and sumV1.

Many thanks.

#### Attachments

• Summation2.nb
110.6 KB · Views: 478
Thank you.

Is this at least part of what you are trying to accomplish?

Replace

Do[
Clear[ ...];
...
u0 = Simplify[Chop[disp0[[1, 1]]]]; v0 = Simplify[Chop[disp0[[2, 1]]]];
U1[m_, n_] := u0; uuu1[m, n] = Table[U1[i, n], {i, 1, m, 2}];
sumU1 = Simplify[Total[Table[uuu1[m, n][[1]], {m,1, m, 2}]]];
V1[m_, n_] := v0; vvv1[m, n] = Table[V1[m, jjj], {jjj, 1, n, 2}];
sumV1 = Simplify[ Total[Table[vvv1[m, n][[1]], {n, 1, n, 2}]]];
Print[Chop[sumU1]], {m, 1, 29, 2}, {n, 1, 29, 2} ]

with

uvmatrix = Table[
Clear[ ...];
...
{Simplify[Chop[disp0[[1, 1]]]]; Simplify[Chop[disp0[[2, 1]]]]},
{m, 1, 29, 2}, {n, 1, 29, 2} ];

Then try

umatrix = Map[First, uvmatrix, {-2}];
vmatrix = Map[Last, uvmatrix, {-2}];
usum[m_, n_] := Total[Take[umatrix[[(m + 1)/2]], (n + 1)/2]];
vsum[m_, n_] := Total[Take[vmatrix[[(m + 1)/2]], (n + 1)/2]];

where usum will sum the 1,3,...,n elements of the m row of your u0 values and vsum will do that for your v0 values. m and n are both odd in 1,3,...,27,29.

Manually carefully verify if usum[5,7] is correct in every detail.

If it is correct then perhaps you will want to try Table[usum[m, n], {m, 1, 29, 2}, {n, 1, 29, 2}] and see if it is correct.

If this is incorrect then please explain how the sum needs to be changed.

Frankly speaking, I have never used this command before, Map. When I run the codes of uvmatrix, then umatrix and vmatrix, I am just wondering why the result of umatrrix and vmatrix without the unknowns terms of Ao[1]...Ao[17] and Bo[1]...Bo[17], these are dissapeared. I assume this is due to the Map command. I checked through the help manual, i still could not understand such definition and application of Map.

Your overall idea seems to be correct anyway, but let me crack this matter first of all, i.e the missing unknowns..

The uvmatrix=Table[...{u0expression,v0expression},{m, 1, 29, 2}, {n, 1, 29, 2}] should create a matrix with each entry a pair of values, the u0 part and the v0 part, for each m,n value.

If I understand correctly everything inside your Do[] should be identical inside the Table[] down to and including your calculation of disp0. Everything after your calculation of disp0 appears to only depend on the value of your disp0 and that was what I changed.

Map[function,list] will do the function to each element in the list and return all those results as a new list. So Map[first,uvmatrix,{-2}] should extract the u0 expressions.

Inspect uvmatrix[[5,7]] to see if it is {u0,v0} for m=5,n=7. Then inspect umatrix[[5,7]] to see if it is u0 for m=5,n=7.

Last edited:
After running the revised codes, I verify that uvmatrix[[5,7]] returned the correct output of {u0, v0} for m =5 and n = 7.

However, umatrix[[5,7]] and vmatrix[[5,7]] did not generate the correct u0 and v0, respectively when m =5 and n = 7.

Hence, usum[5,7] and vsum[5,7] do not give the right outputs.

Just a matter of checking, I have worked out manually the actual outputs of usum[5,7] and vsum[5,7]. These results are shown in the attached file.

For further info, I have changed slightly this code: {Simplify[Chop[disp0[[1, 1]]]] ; Simplify[Chop[disp0[[2, 1]]]]} into {Simplify[Chop[disp0[[1, 1]]]] , Simplify[Chop[disp0[[2, 1]]]]}.
I just changed semi colon to comma in the middle, because the first code only give me the output v0 without u0.

#### Attachments

• Summation2Revised.nb
88 KB · Views: 477
kaizen.moto said:
I just changed semi colon to comma in the middle, because the first code only give me the output v0 without u0.

Very good. You caught my mistake. I apologize for the error. I did not catch that because I had not been able to get all your code to work. I've been trying to get this to work without being able to see your results. That was why I asked you to show your output.

With your correction are umatrix, vmatrix, usum and vsum correct now? Or are there still errors.

Here is a tiny example showing how these should work

In[1]:= uvmatrix={{{u1,v1},{u2,v2},{u3,v3}},{{u4,v4},{u5,v5},{u6,v6}},{{u7,v7},{u8,v8},{u9,v9}}}
Out[1]= {{{u1,v1},{u2,v2},{u3,v3}},{{u4,v4},{u5,v5},{u6,v6}},{{u7,v7},{u8,v8},{u9,v9}}}

In[2]:= umatrix=Map[First,uvmatrix,{-2}]
Out[2]= {{u1,u2,u3},{u4,u5,u6},{u7,u8,u9}}

In[3]:= vmatrix=Map[Last,uvmatrix,{-2}]
Out[3]= {{v1,v2,v3},{v4,v5,v6},{v7,v8,v9}}

In[4]:= usum[m_,n_]:=Total[Take[umatrix[[(m+1)/2]],(n+1)/2]];usum[3,3]
Out[5]= u4+u5

In[6]:= vsum[m_,n_]:=Total[Take[vmatrix[[(m+1)/2]],(n+1)/2]];vsum[5,5]
Out[7]= v7+v8+v9

Do you still find that Ao[1]...Ao[17] and Bo[1]...Bo[17] are still missing? If those are still missing we need to find a way I can see what you are getting and enough detail that I can find my errors.

Please help find any remaining errors and we will finish this.
Thank you

Last edited:
No worries...
umatrix, vmatrix, usum and vsum are still errors. Iam still looking for a way to get the right ones. Yes, all Ao[1]...Ao[17] and Bo[1]...Bo[17] are still missing.

I would try my best to get the desired codes based from your suggested codes.

Thank you.

Restart Mathematica so there are no hidden assignments to variables.

Please do not do any other hidden calculations.

Evaluate your notebook down to and including
uvmatrix=Table[...]

Then show me the value of uvmatrix

Then show me the value of {Ao[1], Ao[3], Ao[5], Ao[7], Ao[9], Ao[11], Ao[13], Ao[15], Ao[17], Ao[19], Ao[21], Ao[23], Ao[25], Ao[27], Ao[29], Bo[1], Bo[3], Bo[5], Bo[7], Bo[9], Bo[11], Bo[13], Bo[15], Bo[17], Bo[19], Bo[21], Bo[23], Bo[25], Bo[27], Bo[29]}

Then show me the value of umatrix

Then show me the value of usum[1,3]

You can accomplish all this by just removing the semicolon from the end of appropriate lines.

Perhaps there is something in the way you have written the code or the way Mathematica is evaluating your expressions that is removing Ao[] and Bo[] with Table that were not removed with Do.

Have you made any other changes between the Do version and the Table version?

You use Evaluate 15 times in your notebook. In my experience Evaluate is very rarely needed unless you are trying to control the way Mathematica will process your notebook. You have also a number of functions that do not appear to be used, like h1*[z1_,r_] and others. I do not know what some of these things will do to your results.

I tracked down and fixed several errors I made.

Try these revised functions
umatrix = Partition[Map[First, Partition[Flatten[uvmatrix], 2]], Length[uvmatrix]]
and
vmatrix = Partition[Map[Last, Partition[Flatten[uvmatrix], 2]], Length[uvmatrix]]

I believe those will now correctly preserve the Ao[1]...Bo[17]

After these changes please tell me if there are more errors.
I may not need the information I asked for in the previous message now.
Thank you

Last edited:
You have made changes to turn all this back into a Do[], you are not creating and initializing all the values in uvmatrix, instead you are assigning a single entry to uvmatrix and doing this over and over again. That was at least part of the reasons your original code with Do was not working days ago. So when you are done there is no matrix of uv values to extract the umatrix and vmatrix from. I cannot even see how to unDo what you have done now to get the matrix of values you need to be able to calculate your sums.

Change your
...I6 = IdentityMatrix[6];Do[...
disp0=();uvmatrix = {Simplify[disp0[[1, 1]]], Simplify[disp0[[2, 1]]]} // Chop;
Print["m=", m, ",", "n=", n, "-->", uvmatrix], {m, 1, 3, 2}, {n, 1, 3, 2}]

to

...I6 = IdentityMatrix[6]; uvmatrix = Table[...
disp0=();{Simplify[disp0[[1, 1]]], Simplify[disp0[[2, 1]]]} // Chop, {m, 1, 3, 2}, {n, 1, 3, 2}]

or if you have to print this out as it is working then change it to

...I6 = IdentityMatrix[6]; uvmatrix = Table[...
disp0=();uv={Simplify[disp0[[1, 1]]], Simplify[disp0[[2, 1]]]} // Chop;
Print["m=", m, ",", "n=", n, "-->", uv];uv, {m, 1, 3, 2}, {n, 1, 3, 2}]

so that you actually create a matrix of all the u and v values you need.

After that then do each of these lines

umatrix = Partition[Map[First, Partition[Flatten[uvmatrix], 2]], Length[uvmatrix]]

vmatrix = Partition[Map[Last, Partition[Flatten[uvmatrix], 2]], Length[uvmatrix]]

usum[m_, n_] := Total[Take[umatrix[[(m + 1)/2]], (n + 1)/2]]

vsum[m_, n_] := Total[Take[vmatrix[[(m + 1)/2]], (n + 1)/2]]

usum[1,1]

vsum[3,3]

Last edited:
Apologies for my previous message. I have deleted it here. I made the errors.

Now using the uvmatrix = Table[...], and

runnin your updated codes using Map and found that the outputs of umatrix and vmatrix are very good indeed, preserving the uknowns Ao[1]...Bo[17]. meaning, umatrix match with the u0 and vmatrix perfectly matched with v0. Fantastic!

Only remaining the last bit, usum[m_,n_] and vsum[m_,n_] did not return the desired outputs.

Are you certain that the results of usum and vsum are incorrect?

Or is it possible that Mathematica added the entries and then rearranged and combined the results.

If I do Total[umatrix[[1]]] to sum the first row of umatrix I get the same result as usum[1,3].
If I do Total[vmatrix[[2]]] to sum the second row of vmatrix I get the same result as vsum[3,3].
But the totals do not look like I just put a "+" between umatrix[[1,1]] and umatrix[[1,2]].
Please check to see if the total is correct and just looks different or if it is an error.

If it is an error then I need to know how it is in error so I can correct this.

I verify that Total[umatrix[[1]] do not match to usum[1,3] and Total[vmatrix[[2]]] do not get the same result with vsum[3,3]. Both of them look different.

The idea of usum is that usum[m,n] should only give the summation of umatrix with respect to m while n is unchanged. For instance, for usum[5,5] should give umatrix[1,5] + umatrix[3,5] + umatrix[5,5].
If usum[1,3], should only return umatrix[1,3].
I confirm that usum[1,3] = umatrix[[1,2]], if the syntax of usum[m_,n_] is correct.

The idea of vsum is that vsum[m,n] should only give the summation of vmatrix with respect to n while m is unchanged. For instance, for vsum[1,7] should give vmatrix[1,1] + vmatrix[1,3] + vmatrix[1,5] + vmatrix[1,7].
If vsum[1,3] should return vmatrix[1,1] + vmatrix[1,3].

Last edited:
My error, removed.

Then you write usum[5,5]== umatrix[1,5] + umatrix[3,5] + umatrix[5,5]
but then you write usum[1,3]==umatrix[1,3].
Why is usum[1,3] != umatrix[1,3] + umatrix[3,3]

Does this correct usum?

umatrix = {{a, b, c}, {d, e, f}, {g, h, i}};
usum[m_, n_] := Total[Take[Transpose[umatrix][[(m + 1)/2]], (n + 1)/2]];
usum[5, 5]

returns c+f+i

Is that correct?

If vmatrix == {{a, b, c}, {d, e, f}, {g, h, i}}
then what should vsum[3,3] have as a result?

Last edited:
Apologise for the unclear statements.

usum is the summation of umatrix of the first subscipt only and vsum is the summation of vmatrix of the second subscript.

usum[1,3] is not equal to umatrix[1,3] + umatrix[3,3] because the first subscript is 1, hence its summation start from and finish with 1 only. If usum[3,3], then it would become the summation of umatrix[1,3] + umatrix[3,3] since the first subscript is 3.

yes, that is correct, usum[5,5] gives c+f+i.
Then, vsum[3,3] should return d+e.

Hope this clear everything.
Thank you.

In[31]:= vmatrix={{a,b,c},{d,e,f},{g,h,i}};
vsum[m_,n_]:=Total[Take[vmatrix[[(m+1)/2]],(n+1)/2]];
vsum[3,3]

Out[33]= d+e

In[43]:= umatrix={{a,b,c},{d,e,f},{g,h,i}};
usum[m_,n_]:=Total[Take[Transpose[umatrix][[(n+1)/2]],(m+1)/2]];
usum[1,3]

Out[45]= b

In[46]:= usum[5,5]

Out[46]= c+f+i

All cases you can test are correct now?

Brilliant...u got them perfectly correct, both usum and vsum are now working great. I hope sum[m_,n_]:= and vsum[m_,n_]:= would pick up both the reals and imaginary parts collectively.

You are genius...

Can I just apply all these codes by pasting them in the Do-Loop?

Last edited:
I am not certain I know what you mean by "pick up both the reals and imaginary parts collectively." usum and vsum will add the elements, real or complex.

usum[m,n] and vsum[m,n] are functions and can be used just like Sin[theta]. But usum and vsum need the umatrix and vmatrix filled with all the appropriate values. They cannot sum up parts of rows or columns of a matrix if they do not have all the data in the rows and columns to work with. Imagine you want to sum up the first column of a matrix but all you know is a single number from the entire matrix. If you have to do it that way then it may be much easier if you learn more programming.

You can even have Table[usum[m,n],{m, 1, 29, 2}, {n, 1, 29, 2}] if umatrix has the data.

If you keep trying to use Do[] for everything I think you might benefit from learning a bit more about how to handle data inside Mathematica.

Good Luck.

I just wanted to say thank you for your relentless support and fantastic guidance. I really begin to enjoy Mathematica and this forum is really great.

Best wishes.

The code is working perfectly using uvmatrix = Table[...,{m,1,29,2},{n,1,99,2}];umatrix =...;vmatrix =...;usum[m_,n_]:=...vsum[m_,n_]:=...
Thank you.
However, since, the code is partially displayed, I wish to use Do-Loop and put everything inside the Do. Hence, the code shall look like...Do[...uvmatrix =...;umatrix=...;vmatrix=...;usum[m_,n_]=...vsum[m_,n_]:=..., {m,1,29,2},{n,1,29,2}]. However, this Do command did not work, from umatrix and vmatrix. But it gives the right output of uvmatrix.

Iam just wondering if this can done, i.e. everything is done inside the Do-loop.
Many thanks.

You have been fixated on using a Do for weeks.

Earlier I wrote:

Bill Simpson said:
But usum and vsum need the umatrix and vmatrix filled with all the appropriate values. They cannot sum up parts of rows or columns of a matrix if they do not have all the data in the rows and columns to work with.

If you just simply must use Do then you need to understand or learn enough about programming to realize that each iteration through the Do will only have the data calculated for that specific m and n and nothing else unless you save the data from all the previous iterations in some variables and can access those variables.

The code inside your Do will be evaluated again and again. Suppose it has done this many times and now m=7 and n=5. At that point it remembers nothing about m=7 and n=3 or m=7 and n=1 or m=5 and n=1 or any other previous values. All those old calculations are gone and have been replaced by the calculation for m=7 and n=5 unless you know enough to be able to somehow save the information that you need and can later access it.

Thank you for the feedback.
Actually, Iam fully aware and understand about the use of Do-loop, in which at every iteration of different m and n, it would return the latest results of usum and vsum. This process would involve in such a way that uvmatrix, umatrix, vmatrix, usum and vsum results are being overwrite in the memory at every m and n varies.

The main reason i wanted to use the Do, instead of Table is that I am going to create 34 expressions of usum and vsum at each m and n. Meaning, at each loop, I have to express usum1, usum2, usum3...usum17 and vsum1, vsum2, vsum3...vsum17.
Then, from here I already have 34 expressions based from usum and vsum, and also I have 34 unknowns comprising Ao[1], Ao[2]...Ao[17] and Bo[1]...Bo[17]. The I would be able find the values of each unknowns Ao[1]...Bo[17] by Solve command (simultaneous equations).

So, Iam aware that Do, would evaluate again and again depending on the m and n hoping that for each m and n, all the usum and vsums would be saved in the memory and can be recalled at later stage.

The idea is that for each iteration of m and n, I would be able to solve for the 34 unknowns using the 34 expressions of usum and vsum.

For now, since you have given me the precious codes, going to use this as a platform and Iam trying to make trial and error if these expressions can be used inside the Do, such as uvmatrix[m_,n_]:=..., umatrix[m_,n]:=..., vmatrix[m_,n_]:=...usum[m_,n_]:=...vsum[m_n_]:=... so that the outputs can be saved in the memory.

Thank you.

In each iteration of your Do there is no uvmatrix and so there is no umatrix, no vmatrix, nothing for usum or vsum to calculate with. There is only disp0[[1, 1]] and disp0[[2, 1]]. Anything else must be created from those.

The good news is that if I use Do[...uvmatrix={Simplify[disp0[[1, 1]]], Simplify[disp0[[2, 1]]]} // Chop;Print["m=", m, ",", "n=", n, "-->", uvmatrix], {m, 1, 7, 2}, {n, 1, 7,
2}], I could get the outputs of uvmatrix={u0, v0} at every m and n.

The error started if I put umatrix and vmatrix inside the Do, in which they gave empty elements, i.e. umatrix = {} and vmatrix = {}. So I would try my best, how to retrieve the elements of umatrix and vmatrix from the available data of uvmatrix inside the Do.

## 1. What is the purpose of using a Do-Loop in Mathematica for summation?

The Do-Loop in Mathematica is a programming construct that is used to repeat a set of statements a certain number of times. In the case of summation, it allows us to perform a series of mathematical operations on a given set of numbers without having to write out each step individually.

## 2. How do I set the initial and final values for the Do-Loop in Mathematica?

The initial and final values for the Do-Loop can be set using the syntax: Do[statement, {iterator, initial, final}]. The iterator is a variable that is used to keep track of the current iteration, and the initial and final values determine the range of values that the iterator will take on.

## 3. Can I use a Do-Loop to perform multiple summations in Mathematica?

Yes, the Do-Loop can be nested within another Do-Loop to perform multiple summations. This is useful when dealing with multidimensional arrays or when performing more complex calculations that require multiple iterations.

## 4. How can I incorporate conditions into the Do-Loop in Mathematica?

The Do-Loop can be modified to include conditions using the If statement. The syntax is: Do[If[condition, statement], {iterator, initial, final}]. This allows for more control over the iterations and enables us to perform different operations depending on certain conditions.

## 5. Is it possible to break out of a Do-Loop in Mathematica?

Yes, the Break statement can be used to exit a Do-Loop prematurely. This is useful when a certain condition is met and we no longer need to continue with the iterations. The syntax is: Do[If[condition, Break[]], {iterator, initial, final}].

Replies
4
Views
2K
Replies
8
Views
3K
Replies
9
Views
472
Replies
2
Views
889
Replies
1
Views
1K
Replies
6
Views
3K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K