1 Pages (6 items)
Use of 'eval' - Messages
#1 Posted: 5/7/2024 1:36:26 AM
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
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: 5/7/2024 2:22:45 AM
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).
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 5/7/2024 11:31:00 PM, ioan92 5/7/2024 11:50:00 PM, sergio 5/8/2024 3:04:00 AM, Alvaro Diaz Falconi 5/8/2024 3:58:00 AM
#3 Posted: 5/8/2024 4:01:03 AM
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 60 time(s).
2 users liked this post
#4 Posted: 5/14/2024 1:47:15 AM
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
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: 5/14/2024 5:11:49 PM
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) ).
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: 5/15/2024 1:37:55 AM
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 50 time(s).
Best regards.
Alvaro.
1 Pages (6 items)
-
New Posts
-
No New Posts