Solving Mathematica Summation Code Problem

  • Context: Mathematica 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    Mathematica Summation
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Mathematica code related to summation and integration. Participants explore issues related to variable definitions and performance, focusing on the implementation of mathematical expressions in code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a Mathematica code that encounters performance issues when executed, questioning the underlying problem.
  • Another participant suggests changing the definitions of variables A and B to functions A[n_, m_] and B[n_, m_] to potentially resolve the issue.
  • Some participants mention trying various approaches, including using tables and changing kernel priorities, but report that nothing seems to work efficiently.
  • Another suggestion is to use delayed definitions for A and B, which might improve performance.
  • A participant points out a potential error in the code regarding the limits of summation, suggesting it should reference Mmax instead of Max.
  • Several participants agree on the importance of correctly defining variables and express frustration over time wasted due to coding errors.
  • A humorous anecdote is shared about the challenges of debugging code and the value of having another person review one's work to catch mistakes.
  • There is a proposal for enhancements in error reporting within Mathematica to help users identify mistakes more effectively.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correctly defining variables and the potential solutions proposed, but multiple competing views and unresolved issues remain regarding the best approach to fix the code.

Contextual Notes

Limitations include unresolved performance issues and the dependence on specific definitions and coding practices that may affect the execution of the Mathematica code.

Dustinsfl
Messages
2,217
Reaction score
5
Can anyone tell me what is the problem with this Mathematica code?
Code:
Nmax = 10;
Mmax = 10;

A = 4/Pi^2*Integrate[x*Sin[n*x]*Sin[m*y], {x, 0, Pi}, {y, 0, Pi}];
B = 4/Pi^2*Integrate[Sin[n*x]*Sin[m*y], {x, 0, Pi}, {y, 0, Pi}];

u[x_, y_, t_] = 
  Sum[Sin[n*x]*
    Sin[m*y] (A*Cos[(n^2 + m^2)*t] + 
      B/(n^2 + m^2)*Sin[(n^2 + m^2)*t]), {n, 1, Nmax}, {m, 1, Max}];
Even though Mathematica will get stuck running this code(above), the code below has no problems. What is the deal?
Code:
Nmax = 20;
Mmax = 20;
\[Lambda] = Table[n^2 + m^2, {n, 1, Nmax}, {m, 1, Mmax}];

u[x_, y_, t_] = 
  4/Pi^2*Sum[
    Sin[n*Pi/2]*Sin[m*Pi/2]/\[Lambda][[n, m]]*Sin[n*x]*Sin[m*y]*
     Sin[t*\[Lambda][[n, m]]], {n, 1, Nmax}, {m, 1, Mmax}];
 
Physics news on Phys.org
Try changing A to A[n_,m_] and B to B[n_,m_] in their definitions, and then invoking them with A[n,m] and B[n,m].
 
Ackbach said:
Try changing A to A[n_,m_] and B to B[n_,m_] in their definitions, and then invoking them with A[n,m] and B[n,m].

Ok so I have tried A = Table and then using A[[n,m]] as well as B.
I am trying A[n_,m_] = Table.
I have change the kernel priority to above normal. I will try if this doesn't work A[n_,m_] = expression no table
 
dwsmith said:
Ok so I have tried A = Table and then using A[[n,m]] as well as B.
I am trying A[n_,m_] = Table.
I have change the kernel priority to above normal. I will try if this doesn't work A[n_,m_] = expression no table

Nothing seems to work in a reasonable amount of time.
 
How about A[n_,m_]:= and B[n_,m_]:=? The colon equals gives you that delayed definition that might work out for you.

Another option would be to compute the integrals exactly (if possible), and put that expression into your u directly. Computing that many integrals might take awhile.
 
dwsmith said:
Can anyone tell me what is the problem with this Mathematica code?
Code:
...{m, 1, Max}];

I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=
 
BillSimpson said:
I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=

Good eye. Only if you knew how much time I wasted.
 
BillSimpson said:
I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=

Second dwsmith's comment. Excellent eye. This is why strictly typed programming languages are superior to weakly typed ones! The compiler would catch this one.
 
dwsmith said:
Good eye. Only if you knew how much time I wasted.

Funny story.
I remember too many decades ago when I was a student and time was running out, both wall clock and my allotted number of CPU seconds, and I just could not find a bug. In absolute desperation I turned, grabbed the nearest person by the collar, aimed him at my line printer listing and said "DON'T YOU MOVE!" Then at high speed I began to explain to him exactly what my thousand lines of code was doing, line by line. About a minute and two hundred lines into the code I said "AHHH! Found It!, Get away from me!" and frantically ran to the card punch to fix the flaw so I could get the job in the run queue before the deadline.

Moral: There are things in our own code that we are simply literally blind to. Having someone else, who has to know enough that you cannot just blow them off, you must take them seriously, is an extremely powerful tool to help us spot our own "obvious" mistakes. Having someone else look at the code for a few minutes is not as good, but sometimes that is all you have.

Now on the subject of checking.
I've been told that that my repeated pleas for a "novice mode" has finally been recognized and has been submitted for actual consideration. I do not know what the outcome of that will be and there are no guarantees. But if that is approved and implemented some day then it would be possible to turn on a flag that will make the system recognize and complain about incorrect capitalization and use of system function names as user variables.

My suggestion was that the text in error be displayed in GIANT RED BLINKING letters but I doubt they will actually do that. But the itty bitty little orange "warnings" on a separate line that they currently display are simply not doing the job that needs to be done.

If there are other enhancements to error reporting that are closely related then it might be possible for me to get those included if anyone can make a strong case for the change.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K