Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Summation in the Do-Loop

  1. Jun 21, 2011 #1
    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.

    Attached Files:

  2. jcsd
  3. Jun 22, 2011 #2
    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
  4. Jun 22, 2011 #3
    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.

    Attached Files:

  5. Jun 22, 2011 #4
    Thank you.

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


    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} ]


    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.
  6. Jun 23, 2011 #5
    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..
  7. Jun 23, 2011 #6
    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: Jun 23, 2011
  8. Jun 23, 2011 #7
    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.

    Attached Files:

  9. Jun 24, 2011 #8
    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: Jun 24, 2011
  10. Jun 24, 2011 #9
    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.
  11. Jun 24, 2011 #10
    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

    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.
  12. Jun 24, 2011 #11
    I tracked down and fixed several errors I made.

    Try these revised functions
    umatrix = Partition[Map[First, Partition[Flatten[uvmatrix], 2]], Length[uvmatrix]]
    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: Jun 24, 2011
  13. Jun 24, 2011 #12
    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}]


    ...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]]


    Last edited: Jun 24, 2011
  14. Jun 24, 2011 #13
    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.
  15. Jun 24, 2011 #14
    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.
  16. Jun 24, 2011 #15
    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: Jun 24, 2011
  17. Jun 24, 2011 #16
    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: Jun 24, 2011
  18. Jun 24, 2011 #17
    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.
  19. Jun 24, 2011 #18
    In[31]:= vmatrix={{a,b,c},{d,e,f},{g,h,i}};

    Out[33]= d+e

    In[43]:= umatrix={{a,b,c},{d,e,f},{g,h,i}};

    Out[45]= b

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

    Out[46]= c+f+i

    All cases you can test are correct now?
  20. Jun 24, 2011 #19
    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: Jun 24, 2011
  21. Jun 24, 2011 #20
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook