Mathematica: Please help me to improve the summation codes inside Do-Loop

  • Context: Mathematica 
  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica Summation
Click For Summary

Discussion Overview

The discussion revolves around improving Mathematica code for summation operations within a Do-Loop structure. Participants are seeking assistance in defining and refining functions for calculating totals based on varying parameters, specifically focusing on the summation of values derived from defined matrices and functions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes their initial code and requests help to express total variables (totalu1, totalv1, totalu2, totalv2) within a Do-Loop.
  • Another participant suggests troubleshooting by testing the code with fixed values for m and n before integrating it back into the Do-Loop.
  • Concerns are raised about defining functions inside the Do-Loop, with suggestions to use Module for better variable management.
  • Several participants propose that the summation functions should be defined outside the loop for clarity and efficiency.
  • One participant shares a modified Do-Loop code but notes it did not work, prompting further requests for assistance in transforming it into a Module format.
  • Another participant provides a structured Module example and recommends breaking it into smaller modules for easier troubleshooting.
  • There is ongoing discussion about the effectiveness of the proposed solutions, with some participants reporting success while others encounter issues with different cases.

Areas of Agreement / Disagreement

Participants generally agree on the need to restructure the code for better functionality, particularly advocating for the use of Module over Do-Loop. However, there are differing opinions on the specifics of implementation and the effectiveness of various approaches, indicating that the discussion remains unresolved.

Contextual Notes

Some participants highlight potential conflicts arising from variable naming within functions and suggest using distinct names to avoid confusion. There are also mentions of undefined variables and the need for clearer definitions of functions and their dependencies.

kaizen.moto
Messages
94
Reaction score
0
Dear all,

I have a problem with regard to the summation which is done inside the Do-Loop iteration. I have attached the file which includes further information about the defintions, some examples and etc.

Please help me to get the right codes in order to express totalu1, totalv1, totalu2 and totalv2. Please note that all of these codes to be kept inside the Do-loop.

The code is expressed as follows:

Do[Clear[mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat];mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1=?;totalv1= ? ;totalu2 = ? ;totalv2 = ? ;totalmat={totalu1,totalv1,totalu2,totalv2};Print["m=",m,",","n=",n,"-->",totalmat],{m,1,5,2},{n,1,5,2}]

mat1 and mat2 are column vectors. m and n are intergers where m = 1,3,5,7,9... and n = 1,3,5,7,9,11,...

totalu1 is the summation of usum1 with respect to ' m' term only and must begins with m = 1 (i.e. n is fixed). For example, usum1[5,3]: totalu1 = usum1[1,3]+ usum1[3,3] + usum11[5,3]

totalv1 is the summation of vsum1 with respect to ' n' term only and must begins with n = 1 (i.e. m is fixed). For example, vsum1[3,3]: totalu1 = usum1[3,1]+ usum1[3,3]

same as above, totalu2 is the summation of usum2 with respect to ' m' term only and must begins with m = 1 (i.e. n is fixed). For example, usum2[1,3]: totalu2 = usum2[1,3] only

same as above, totalv2 is the summation of vsum2 with respect to 'n' term only and must begins with n = 1 (i.e. m is fixed). For example, vsum2[1,3]: totalv2 = vsum2[1,1]+ vsum2[1,3] .

For examples: when m = 1 and n = 5,
totalu1[m, n] or totalu1[1, 5] = usum1[1, 5] = 6

when m = 5 and n = 5,
totalu1[m, n] or totalu1[5, 5] = usum1[1, 5] + usum1[3, 5] + usum1[5, 5] = 6 + 8 + 10 = 24

when m = 3 and n = 5,
totalv1[m, n] or totalv1[3, 5] = vsum1[3, 1] + vsum1[3, 3] + vsum1[3, 5] = 14 + 24 + 34 = 72

when m = 5 and n = 1,
totalu2[m, n] or totalu2[5, 1] = usum2[1, 1] + usum2[3, 1] + usum2[5, 1] = 7 + 13 + 19 = 39

when m = 1 and n = 5,
totalv2[m, n] or totalv2[1, 5] = vsum2[1, 1] + vsum2[1, 3] + vsum2[1, 5] = 15 + 35 + 55 = 105

when m = 5 and n = 1, totalv2[5, 1] = vsum2[5, 1] = 35

Please note that all of these should be done inside the Do-Loop.

Any kind help is much appreciated.

Thank you.
 

Attachments

Last edited:
Physics news on Phys.org
kaizen.moto said:
Please note that all of these codes to be kept inside the Do-loop.
The first thing to do is to take them out of the Do loop and test them for a fixed m and n, so that you can see the output at every step of the way. Once you have it working for a fixed m and n then you can pack it back into the Do loop, but until you have fixed the code inside the act of packing it in the Do loop makes troubleshooting difficult.

Second, the various total variables are undefined, so you need to define them. From your descriptions they should be functions which are merely evaluated inside the Do loop, but defined outside.

Third, the various sum functions do not depend on m and n, so those should probably be variables rather than functions.

Fourth, the mat variables do depend on m and n, so they should probably be functions.

In short, pull everything outside of the Do loop for troubleshooting. Make sure that you are using variables and functions appropriately. Write the missing pieces. Test for fixed m and n. Then re-pack into the Do loop.
 
One other thing. Usually, if you are trying to Clear a bunch of variables at the beginning of each iteration of a loop that is a strong indication that the variables are effectively local variables which should be contained inside a With or a Module statement. Your Do loop should probably consist of a single call to a function defined with a Module.
 
Thank you for the advise.
I got it. I have solved my problem based on your first advise. However, I wanted to get the code as per your second advise, i.e. to use Module.

Could you please help me how to rectify the code from Do-loop to Module form:

For example:
(*Do Loop*)
Do[Clear[mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat];mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1[m_,n_]:=Total[Table[usum1[m,n][[1]],{m,1,m,2}]];totalv1[m_,n_]:=Total[Table[vsum1[m,n][[1]],{n,1,n,2}]];totalu2[m_,n_]:=Total[Table[usum2[m,n][[1]],{m,1,m,2}]];totalv2[m_,n_]:=Total[Table[vsum2[m,n][[1]],{n,1,n,2}]];totalmat={totalu1,totalv1,totalu2,totalv2};Print["m=",m,",","n=",n,"-->",totalmat],{m,1,5,2},{n,1,5,2}]

(*Module*)
qsolve[m_,n_]:=Module[{mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat},mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1[m_,n_]:=Total[Table[usum1[m,n][[1]],{m,1,m,2}]];totalv1[m_,n_]:=Total[Table[vsum1[m,n][[1]],{n,1,n,2}]];totalu2[m_,n_]:=Total[Table[usum2[m,n][[1]],{m,1,m,2}]];totalv2[m_,n_]:=Total[Table[vsum2[m,n][[1]],{n,1,n,2}]];totalmat={totalu1,totalv1,totalu2,totalv2}]

Sum[qsolve[m,n],{m,1,5,2},{n,1,5,2}]
 
I tried your Do loop code but it didn't work, so I can't really make specific suggestions. However I did notice one clear thing. You are defining several functions inside the module. That is not very usual and, because you are using the same variable names, it is causing some conflicts. You can fix that simply by using different letters for the variables in your local function definitions.
 
Try this Do-Loop:

Do[Clear[u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2,
totalmat]; u1[m_, n_] := m + n;
v1[m_, n_] := 3 m + 5 n;
u2[m_, n_] := 3 m + 4 n;
v2[m_, n_] := 5 m + 10 n; uu1[m, n] = Table[u1[i, n], {i, 1, m, 2}];
totalu1 = Simplify[Total[Table[uu1[m, n][[1]], {m, 1, m, 2}]]];
vv1[m, n] = Table[v1[m, jjj], {jjj, 1, n, 2}];
totalv1 = Simplify[ Total[Table[vv1[m, n][[1]], {n, 1, n, 2}]]];
uu2[m, n] = Table[u2[i, n], {i, 1, m, 2}];
totalu2 = Simplify[Total[Table[uu2[m, n][[1]], {m, 1, m, 2}]]];
vv2[m, n] = Table[v2[m, jjj], {jjj, 1, n, 2}];
totalv2 = Simplify[Total[Table[vv2[m, n][[1]], {n, 1, n, 2}]]];
totalmat = {totalu1, totalv1, totalu2, totalv2};
Print["m=", m, ",", "n=", n, "-->", totalmat], {m, 1, 5, 2}, {n, 1,
5, 2}]

Please help me to transform the above using Module statement. Thank you.
 
First you define your module:

Code:
f[m_, n_] :=
 Module[{u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2, totalmat},
  u1[a_, b_] := a + b;
  v1[a_, b_] := 3 a + 5 b;
  u2[a_, b_] := 3 a + 4 b;
  v2[a_, b_] := 5 a + 10 b;
  uu1[m, n] = Table[u1[i, n], {i, 1, m, 2}];
  totalu1 = Simplify[Total[Table[uu1[i, n][[1]], {i, 1, m, 2}]]];
  vv1[m, n] = Table[v1[m, jjj], {jjj, 1, n, 2}];
  totalv1 = Simplify[Total[Table[vv1[m, i][[1]], {i, 1, n, 2}]]];
  uu2[m, n] = Table[u2[i, n], {i, 1, m, 2}];
  totalu2 = Simplify[Total[Table[uu2[i, n][[1]], {i, 1, m, 2}]]];
  vv2[m, n] = Table[v2[m, jjj], {jjj, 1, n, 2}];
  totalv2 = Simplify[Total[Table[vv2[m, i][[1]], {i, 1, n, 2}]]];
  totalmat = {totalu1, totalv1, totalu2, totalv2}
  ]

Then you define your loop.

Code:
Do[
 Print["m=", m, ",", "n=", n, "-->", f[m, n]], {m, 1, 5, 2}, {n, 1, 5,
   2}]

I would even recommend breaking it up some more. The module is a pretty ugly module that is probably pretty hard for you to troubleshoot. It looks like it should be split up into several smaller modules that do each step of the process individually and would be individually easier to troubleshoot.
 
Thank you, yes, its working now using Module for the above case. However, it did not work for another case as shown below:

f[m_, n_] :=
Module[{u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2,
totalmat}, u1[a_, b_] := a + b;
v1[a_, b_] := 3 a + 5 b;
u2[a_, b_] := 3 a + 4 b;
v2[a_, b_] := 5 a + 10 b;
totalu1[mMAx_, n] := Sum[u1[m, n], {m, 1, mMAx, 2}];
totalv1[m, nMax_] := Sum[v1[m, n], {n, 1, nMax, 2}];
totalu2[mMAx_, n] := Sum[u2[m, n], {m, 1, mMAx, 2}];
totalv2[m, nMax_] := Sum[v2[m, n], {n, 1, nMax, 2}];
totalmat = {totalu1[m, n], totalv1[m, n], totalu2[m, n],
totalv2[m, n]}]

Do[Print["m=", m, ",", "n=", n, "-->", f[m, n]], {m, 1, 3, 2}, {n, 1,
3, 2}]

Could you please let me know how to fix this case?

Thanks.
 
Consider the call f[3,5]. Inside your module this expression:

Sum[u1[m, n], {m, 1, mMAx, 2}]

turns into this

Sum[u1[3,5],{3,1,mMAx,2}]

Can you spot the problem with this expression? I will give you a hint, the error message will be:
Sum::itraw: Raw object 3 cannot be used as an iterator.

And the link to the help documentation will give you:
http://reference.wolfram.com/mathematica/ref/message/General/itraw.html
 
  • #10
Sorry...i have deleted my previous message simply because finally I got the right code already using Module. it's finally working.
Thanks for your kind help.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
3K