Use of 'eval'

Use of 'eval' - Messages

#1 Posted: a year ago
Mick

Mick

2 likes in 18 posts.

Group: User

I don't fully understand the use of 'eval', which probably means I don't really understand how SMath works. Can anyone help me?

I sort of understand what it does, and have proved to myself that it can speed up calculations considerably.

Do I need to use eval on every expression if I want calculations as fast as possible (seems a bit of an odd way of doing things)? What do I lose by doing this? Is there some option I can set so that the whole worksheet is forced to assume that eval is in every expression?

I used MathCad for decades, and I don't remember anything equivalent. As I recall numerical and symbolic were easy to keep separate.

TIA for any help.

Jon
#2 Posted: a year ago
Davide Carpi

Davide Carpi

1,420 likes in 2,875 posts.

Group: Moderator

Hi Jon,

by default SMath evaluate simbolically. In short, this means that if you have (f.e.) X=a+b+c=A, the expression (a+b+c) is stored and then evaluated on request; so if you have Y=d+e=B and you want to calculate X+Y, SMath returns a+b+c+d+e, not A+B. This minimize the error propagation.

When you use eval(), the calculation of a+b+c is performed and the numerical result (A) is stored.

When you have loops or complex calculation, the expression terms might stack in a very long equation. If you have a vector or a matrix, each element might contains a lot of them.

When the complexity arise, since an evaluation requires to retrieve each element from the memory, replace it, and perform the actual calculations, it might be useful to perform an eval() (in some functions the result is forced to be a numerical evalution, so the performance improvement might vary).
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
4 users liked this post
churichuro a year ago, ioan92 a year ago, sergio a year ago, Alvaro Diaz Falconi a year ago
#3 Posted: a year ago
Alvaro Diaz Falconi

Alvaro Diaz Falconi

1,005 likes in 1,677 posts.

Group: User

Wrote

... When you have loops or complex calculation, the expression terms might stack in a very long equation. If you have a vector or a matrix, each element might contains a lot of them.

When the complexity arise, since an evaluation requires to retrieve each element from the memory, replace it, and perform the actual calculations, it might be useful to perform an eval() ...



Hi. This example can illustrate what Davide says.

Best regards.
Alvaro.

using_eval.sm (19 KiB) downloaded 147 time(s).

eval.png
2 users liked this post
ioan92 a year ago, Davide Carpi a year ago
#4 Posted: a year ago
Mick

Mick

2 likes in 18 posts.

Group: User

Thank you, Davide & Alvaro,

I understand that a bit better now.

I'm still not sure what I might lose by using 'eval' all the time, apart from minimisation of error propagation - not normally relevant to what I do with Smath. My background & training leads me to think about such errors as I'm designing algorithms.

I'm thinking that I might just as well use 'eval' all the time, on every expression, but this is a bit of a pain (just more typing, really, but ...). I can't help but think that an 'auto-eval' mode would be a useful feature, but maybe this is not possible to integrate into the Smath structure.

Anyway, thanks for the help.

Jon
#5 Posted: a year ago
Davide Carpi

Davide Carpi

1,420 likes in 2,875 posts.

Group: Moderator

As a rule of thumb, you can/should write everything without eval() and introduce some only if the evaluation as a whole is noticeably slow; the threshold is on you, I think 5 to 10 seconds is where you should start to investigate if something could be improved, looking at the regions with persistent green border (those under evaluation) and their contributors.
Too many eval() are useless and they make the worksheet less readable, though in this case you might use a user-defined overload one-character function to mitigate it (something like ''(x):eval(x) or \0024\(x):eval(x) ).
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#6 Posted: a year ago
Alvaro Diaz Falconi

Alvaro Diaz Falconi

1,005 likes in 1,677 posts.

Group: User

Wrote

... think that an 'auto-eval' mode would be a useful feature, but maybe this is not possible to integrate into the Smath structure. ...



Hi. There is something like that, the Optimization/Numeric option from the contextual menu. You can also select all expressions with Ctrl-A and choose that option for the entire worksheet.

num-optimiz.sm (71 KiB) downloaded 125 time(s).

num-optimiz.png

Best regards.
Alvaro.
  • New Posts New Posts
  • No New Posts No New Posts