Very long calculation time of absolute values

Very long calculation time of absolute values - Сообщения

#1 Опубликовано: 20.01.2023 09:28:19
Maciej

Maciej

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

Группа: User

Hello everyone,

I have a problem with very long calculation time of absolute values using for loop. I am using Smath Solver version 1.0 build 8348.

For the purposes of my thesis I need to perform some calculations on complex numbers. Some of them must be ploted and to do that I need their absolute values.
In my first abs calculation I have used a for loop and everything worked fine - Smath has calculated abs (it took about 8 seconds - quite long but acceptable) and generated plot. Then I tought it would be better if the values on Y axis are one order of magnitude smaller. So and the end of the formula in for loop I added mutliplication by 1000. And then problems started. Smath was calculating the abs for about 3 minutes before I have thought something is broken and terminated the calculations. I have erased the multiplication and run calculation once more. Unfortunatelly that didn't help and Smath still processed for loop for the very long time. I even tried to delete the formula and write it from the beginnig but that also didn't work - still long abs calculations. What am I doing wrong and why Smath calculated abs wiht no problems in the first time?

I am certain that the problem is with for loop because when I delete the formula and run calculation everything works fine (calculation time about 8 seconds). I am also certain that number of calculations is not an issue - I need to calculate only about 3300 abs values. And thirdly, I am sure that the problembis not in my CPU, because it's quite new and seems to be powerfull enough to perform such calcualtions (Intel i7 12700H, 32 Gb RAM).

I enclose my whole calcualtion sheet. The despcriptions are in Polish, but I hope that won't be a problem. The abs value calculations are on the 5th page, hidden inside an area called "Dane do wykresu z6p = f(xe)". I would be very grateful for all your help and information.

PS. Please be aware that I'm not mathematicaly fluent and have very little experience in programmig. Untill now Excel was enough for my calculating pupropses. Since I am dealing with the complex numbers now, I need to find an alternative.

Calculations.sm (176 КиБ) скачан 30 раз(а).
#2 Опубликовано: 20.01.2023 09:51:14
Alvaro Diaz Falconi

Alvaro Diaz Falconi

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

Группа: User

Hi. SMath combines numerical and symbolic calculations, so it sometimes saves the information to get a result instead of the result itself. To "clear memory" and keep only numeric values, eval can be used, at the cost of being able to lose a bit of precision in the calculation, sometimes. Try to add this

Clipboard01.png

Also notice that if you have problems editing a long document you can disable the automatic calculation under "Calculation" menu, and press F9 for manual update.

SMath also has other ways to perform this calculation, such as the vectorize function or using an "index" variable.

Best regards.
Alvaro.
#3 Опубликовано: 20.01.2023 15:22:22
Jean Giraud

Jean Giraud

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

Группа: User

Page2 Calculations.sm (88 КиБ) скачан 25 раз(а).
#4 Опубликовано: 20.01.2023 16:34:55
Jean Giraud

Jean Giraud

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

Группа: User

Eternity.PNG

... as you complained about slow calculations.
#5 Опубликовано: 20.01.2023 16:51:42
overlord

overlord

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

Группа: Moderator

Meanwhile;
In an alternate universe where Jean reads previous posts.
And he replies questions with relevant answers:

Razonar's method
2023-01-20_22-49.png

#6 Опубликовано: 20.01.2023 17:04:54
Maciej

Maciej

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

Группа: User

Wrote

Hi. SMath combines numerical and symbolic calculations, so it sometimes saves the information to get a result instead of the result itself. To "clear memory" and keep only numeric values, eval can be used, at the cost of being able to lose a bit of precision in the calculation, sometimes. Try to add this

Clipboard01.png

Also notice that if you have problems editing a long document you can disable the automatic calculation under "Calculation" menu, and press F9 for manual update.

SMath also has other ways to perform this calculation, such as the vectorize function or using an "index" variable.

Best regards.
Alvaro.



Dear Razonar, thank you for your answer. I tried adding eval to all for loops in the document but it doesn't affect the time of abs values calculation (but after deleting the loop for abs values I noticed that it takes only 3 seconds to perform other calculation insted of previous 8 to 10 seconds so it's really a great spped boost and I'm definitely gonna use it in the future).

Wrote

Page2 Calculations.sm (88 КиБ) скачан 25 раз(а).



Dear Jean Giraud, thank you for the answer and the calculation sheet provided, but I'm affraind I don't understand it. I see that you have improved my way of calculating linear interpolarion, but I don't think that my crappy interpolation is the cause of the problem with slow abs calculating time (but it surely does have an influence on general time of calculation).

I tried vectorize and... WOW! It did the job! Abs were calculated really hyperfast and it seems that plot works fine too. I need to implement vectorize on my previous calculations.

PS. I need way more time to catch on with your interpolation sheet, but when I finally understand it I will try to correct my interpolations according to your solution.

Dear overlord, thank yoy for the answer. It seems that your solution works too. But - to be honest - I don't know why...
Why is:
1.jpg
diffrent from:
2.jpg

And also vectorize is way faster.

So, to sum up:
1. Use eval on long interative calculations (like for loops).
2. Use vectorize.
3. Improve linear interpolation

Thank you guys for your help!
#7 Опубликовано: 20.01.2023 18:17:57
overlord

overlord

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

Группа: Moderator

Actually, your worksheet is open to some optimizations.
You run same length for() loops multiple times, for various functions.
There could be some vectorize() replacement, eval(), etc.
I am sure some advanced users may enhance these better.

Here your page finished under 10s with my computer.

Regards

Calculations2.sm (187 КиБ) скачан 27 раз(а).
#8 Опубликовано: 20.01.2023 21:54:43
Jean Giraud

Jean Giraud

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

Группа: User

Wrote

Dear Jean Giraud, thank you for the answer and the calculation sheet provided, but I'm affraind I don't understand it. I see that you have improved my way of calculating linear interpolarion, but I don't think that my crappy interpolation is the cause of the problem with slow abs calculating time (but it surely does have an influence on general time of calculation).

I tried vectorize and... WOW! It did the job! Abs were calculated really hyperfast and it seems that plot works fine too. I need to implement vectorize on my previous calculations.

PS. I need way more time to catch on with your interpolation sheet, but when I finally understand it I will try to correct my interpolations according to your solution.


Welcome Xylene,
linterp code is just to show not as simple as click/click done
Under the hood, |data| there are maths involved,
exceptionally well rendered by the vectorize operator.
A bit long document for my gray hairs.
Take care Xylene ... Jean.

#9 Опубликовано: 21.01.2023 12:56:47
Jean Giraud

Jean Giraud

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

Группа: User

Wrote

I have a problem with very long calculation time of absolute values ...


... some comparative |data|
Cheers ... Jean.

Vectorize.PNG

Option.PNG

#10 Опубликовано: 21.01.2023 13:10:49
overlord

overlord

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

Группа: Moderator

I have vectorized all possible for() loops.
NPlot regions are replaced with XYPlot.

This version is not the fastest but easier to read.
This is 1.5s slower than collective for() loops version.
I left those for() loops as disabled outside of canvas.

You don't need to augment for straight horizontal lines.
Just a zero value is enough. I have also disabled them.

Regards

Calculations2.sm (186 КиБ) скачан 27 раз(а).
Calculations2.pdf (794 КиБ) скачан 25 раз(а).

2023-01-21_19-12.png
1 пользователям понравился этот пост
#11 Опубликовано: 21.01.2023 13:46:19
Вячеслав Мезенцев

Вячеслав Мезенцев

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

Группа: Moderator

You can speed up by a few seconds if you enable numerical optimization for all expressions.

Calculations2 (numeric).sm (183 КиБ) скачан 29 раз(а).

Click to enlarge
Russia ☭ forever, Viacheslav N. Mezentsev
#12 Опубликовано: 21.01.2023 15:47:51
Maciej

Maciej

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

Группа: User

Thank you all for the help. To be honest I didn' expect such commitment. I see that Smath users community is awesome!
Thanks to your advices I managed to shorten the general calculation time from 8 seconds to 2,2 s which is really impressive.

Here is what I dit:
1. I have replaced my interpolation with built-in function linterp() - inspired by Jean Giraud
2. I used eval() in every iterative calculation (basicaly for loops) - thank you Razonar for introducing this function to me
3. I have replaced repetive for loops with one multiline for loop just like overlord did in his sheet provided
4. I used vectorize() for calculating absolute value - as Jean Giraud proved it was really fast and solved the original problem
5. Finally I have turned on the numerical otimalization for the whole sheet according to uni advice

During the implementation of above changes I have also noticed that sometimes the calculation time was incereasing (10 seconds and above) without any reason. When I noticed the increase in calculation time, I terminated the calculation, moved the "problematic" formula around the sheet and then run calculations again - everything was back to normal. No changes in the formula only moving around the sheet.

Unfortunatelly I am not able to recreate the situation when calculation time was increasing and I don't know precisely when the issue happened (I suppose that most often it happened while working with vectorize()). Is it possible that there is some kind of bug?

Regars
#13 Опубликовано: 21.01.2023 16:29:45
Alvaro Diaz Falconi

Alvaro Diaz Falconi

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

Группа: User

Hi. I don't know if this is faster or not, but it is just for show how to use range variables and the norme function in this case in particular.

Calculations - with norme and ranges.sm (175 КиБ) скачан 41 раз(а).

Best regards.
Alvaro.
#14 Опубликовано: 21.01.2023 19:26:01
Jean Giraud

Jean Giraud

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

Группа: User

Wrote

During the implementation of above changes I have also noticed that sometimes the calculation time was increasing (10 seconds and above) without any reason. When I noticed the increase in calculation time, I terminated the calculation, moved the "problematic" formula around the sheet and then run calculations again - everything was back to normal. No changes in the formula only moving around the sheet.


There is no bug. Something not well ordered.
Smath is in different way similar to Mathcad Global definition.
Increased timing suggests not fetching correctly.
My comparative timing is purely indicative 1.66 GHz laptop.
Cheers ... Jean.
#15 Опубликовано: 21.01.2023 19:41:41
overlord

overlord

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

Группа: Moderator

Wrote

Hi. I don't know if this is faster or not


It is indeed 1 second faster than my latest sample.
1 пользователям понравился этот пост
Alvaro Diaz Falconi 21.01.2023 23:55:00
#16 Опубликовано: 21.01.2023 22:45:38
Jean Giraud

Jean Giraud

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

Группа: User

Hello Xylene,
Interpolation is a big word, eventually crucifying option
as revealed in advanced projects from lack of inspection.
These 3 linear origin mostly from Mathcad.
Please, don't hesitate ... Jean.

Interpolate [Subspaces,Intervals,Pivot] Copy.sm (46 КиБ) скачан 25 раз(а).
#17 Опубликовано: 22.01.2023 14:39:34
Jean Giraud

Jean Giraud

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

Группа: User

Calculations Comments.sm (167 КиБ) скачан 27 раз(а).
#18 Опубликовано: 22.01.2023 15:53:28
overlord

overlord

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

Группа: Moderator

Wrote

I managed to shorten the general calculation time from 8 seconds to 2,2s


I would really like to see your version.
Could you please upload it?
#19 Опубликовано: 24.01.2023 15:08:37
Jean Giraud

Jean Giraud

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

Группа: User

Xylene,
Does that graph make sense wrt your project ?
If so, can attach the document.
Cheers ... Jean.

UnkGraph.png
#20 Опубликовано: 24.01.2023 16:42:05
Maciej

Maciej

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

Группа: User

Wrote

Wrote

I managed to shorten the general calculation time from 8 seconds to 2,2s


I would really like to see your version.
Could you please upload it?



Please find the attachement. As I checked it now it wasn't 2,2 s but more likely 2,5 s but still way faster than in the beginning.
Calculations.sm (171 КиБ) скачан 34 раз(а).

Wrote

Xylene,
Does that graph make sense wrt your project ?
If so, can attach the document.
Cheers ... Jean.

UnkGraph.png



Well I made a mistake and used in formula other values than I should, so the above graph doesn't have much sense (I posted the file with the origin graph to show that calculation speed up doesn't come from using diffrent formula).

The correct graph should look like this:
3.jpg

EDIT:
Disabling evaluation for plots has also a positive effect on calculations speed.
1 пользователям понравился этот пост
Mark R Harris 24.01.2023 16:55:00
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений