Use of 'eval'

Use of 'eval' - Сообщения

#1 Опубликовано: 07.05.2024 01:36:26
Mick

Mick

2 сообщений из 18 понравились пользователям.

Группа: 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 Опубликовано: 07.05.2024 02:22:45
Davide Carpi

Davide Carpi

1417 сообщений из 2873 понравились пользователям.

Группа: 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 пользователям понравился этот пост
churichuro 07.05.2024 23:31:00, ioan92 07.05.2024 23:50:00, sergio 08.05.2024 03:04:00, Alvaro Diaz Falconi 08.05.2024 03:58:00
#3 Опубликовано: 08.05.2024 04:01:03
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: 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 КиБ) скачан 68 раз(а).

eval.png
2 пользователям понравился этот пост
ioan92 08.05.2024 13:53:00, Davide Carpi 08.05.2024 17:38:00
#4 Опубликовано: 14.05.2024 01:47:15
Mick

Mick

2 сообщений из 18 понравились пользователям.

Группа: 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 Опубликовано: 14.05.2024 17:11:49
Davide Carpi

Davide Carpi

1417 сообщений из 2873 понравились пользователям.

Группа: 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 Опубликовано: 15.05.2024 01:37:55
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: 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 КиБ) скачан 51 раз(а).

num-optimiz.png

Best regards.
Alvaro.
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений