Mathematica calculates for ages

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

Discussion Overview

The discussion revolves around a Mathematica code snippet that is intended to perform a series of calculations efficiently. Participants explore the performance issues encountered, particularly the long execution time, and suggest modifications to improve the code's efficiency. The focus is on numerical versus symbolic computation and the implications for execution speed.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant reports that their Mathematica code runs for an extended period without stopping and asks for assistance in identifying the issue.
  • Another participant suggests that the long execution time is due to modifying the variable z within a nested loop, which causes it to become non-integer and expand symbolically.
  • A modification to the code is proposed to isolate the variable g from z, which leads to improved performance but still results in slow execution for a higher number of iterations.
  • Further suggestions indicate that switching from symbolic to numerical calculations could significantly enhance performance, with one participant asserting that this change would allow for faster execution of more iterations.
  • Participants express confusion about the differences in execution between symbolic and numerical calculations, with one noting the difference in how Mathematica handles large integers versus floating-point numbers.

Areas of Agreement / Disagreement

Participants generally agree that the execution time is related to the symbolic nature of the calculations. However, there is no consensus on the best approach to resolve the performance issues, as some participants still express confusion about the underlying differences in computation methods.

Contextual Notes

The discussion highlights limitations in understanding the implications of symbolic versus numerical computation in Mathematica, as well as the potential for performance issues when variables are modified within nested loops.

nikolafmf
Messages
112
Reaction score
0
Hello,

I told Mathematica to do a few calculations as follows:

a = 0;
b = 1;
n = 4;
For[i = 0, i < n, i++,
p = 2*x*b - 2*i*a;
a = b;
b = p]
c = CoefficientList[p, x];
l = Length[c];
m = 0;
For[i = 1, i < l + 1, i++,
r = Abs[Part[c, i]]/Abs[Part[c, l]];
If[r > m, m = r, m]]
lowerbound = -m - 1;
upperbound = m + 1;
iter = 5;
f = Function[k, p /. x -> k];
For[z = lowerbound, z < upperbound, z++,
For[i = 0, i < iter, i++,
t = z - f[z]/(D[f[k], k] /. k -> z);
z = t]
Print[N[t]]]

I think this should be executed in a second. But no, Mathematica would run for minutes and won't stop. Can somebody execute this code too, to check if the problem is with my computer or with the code? If the problem is with the code, what kind of problem is it?
 
Physics news on Phys.org
Mathematica is doing what you are telling it to do. Try modifying the last statement to read:

For[z = lowerbound, z < upperbound, z++,
For[i = 0, i < iter, i++,
t = z - f[z]/(D[f[k], k] /. k -> z);
Print; Print[z]; Print[t]; z = t]
Print[N[t]]]

Then run it and look at the output. Is this really what you want the code to do? You are modifying z inside the i loop, so each time through the z loop it adds 1 to z, until z > upperbound. But z is no longer an integer after the first time through the i loop. Since Mathematica is symbolic code, the fractions z and t are expanding until they are the ratios of huge integers. This is why the code is taking so long to run.
 
  • Like
Likes   Reactions: 1 person
Ok, I have modified that part as follows:

For[z = lowerbound, z < upperbound, z++,
g = z;
For[i = 0, i < iter, i++,
t = g - f[g]/(D[f[k], k] /. k -> g);
g = t]
Print[N[t]]]

That seems better, since it calculates 5 or 10 iterations in a reasonable time. But still, if I tell Mathematica to calculate 15 iterations in i loop, it takes too much time...
 
It's taking so much time because you are still doing the calculation symbolically. I suspect that you don't need to do that - doing it numerically will probably be sufficient. If you change the last statement so that it reads:

For[z = lowerbound, z < upperbound, z++,
g = z;
For[i = 0, i < iter, i++,
t = g - f[g]/(D[f[k], k] /. k -> g);
g = N[t]]
Print[N[t]]]

then it will do 15 iterations basically instantaneously. It's important to pay attention to what Mathematica is actually doing.
 
  • Like
Likes   Reactions: 1 person
It works (although I don't understand the difference).
 
nikolafmf said:
It works (although I don't understand the difference).

Try printing out g for each step in the case where g = t vs the case where g = N[t]. In the first case g will be ratios of two integers which are getting larger and larger (like 123456789987654321/987654321123456789, for example). The computer is doing the calculations, keeping all of the digits of those integers. In the second case, the computer is doing the division of those two large integers and only keeping about 10-20 digits of the numerical result, so g is a single floating point numbers (like 1.23456789, for example).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K