Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Offline Xylene  
#1 Posted : 20 January 2023 15:28:19(UTC)
Xylene

Rank: Newbie

Groups: Registered
Joined: 20/01/2023(UTC)
Posts: 5
Poland

Was thanked: 1 time(s) in 1 post(s)
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 (177kb) downloaded 8 time(s).
Offline Razonar  
#2 Posted : 20 January 2023 15:51:14(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,068
Uruguay

Was thanked: 603 time(s) in 383 post(s)
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.

Edited by user 20 January 2023 15:54:26(UTC)  | Reason: Not specified

Offline Jean Giraud  
#3 Posted : 20 January 2023 21:22:22(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Page2 Calculations.sm (89kb) downloaded 4 time(s).
Offline Jean Giraud  
#4 Posted : 20 January 2023 22:34:55(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Eternity.PNG

... as you complained about slow calculations.
Online overlord  
#5 Posted : 20 January 2023 22:51:42(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 900
Turkey

Was thanked: 359 time(s) in 240 post(s)
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

Offline Xylene  
#6 Posted : 20 January 2023 23:04:54(UTC)
Xylene

Rank: Newbie

Groups: Registered
Joined: 20/01/2023(UTC)
Posts: 5
Poland

Was thanked: 1 time(s) in 1 post(s)
Originally Posted by: Razonar Go to Quoted Post
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).

Originally Posted by: Jean Giraud Go to Quoted Post
Page2 Calculations.sm (89kb) downloaded 4 time(s).


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 Good

Thank you guys for your help!
Online overlord  
#7 Posted : 21 January 2023 00:17:57(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 900
Turkey

Was thanked: 359 time(s) in 240 post(s)
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 (188kb) downloaded 9 time(s).
Offline Jean Giraud  
#8 Posted : 21 January 2023 03:54:43(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Originally Posted by: Xylene Go to Quoted Post
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.

Offline Jean Giraud  
#9 Posted : 21 January 2023 18:56:47(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Originally Posted by: Xylene Go to Quoted Post
I have a problem with very long calculation time of absolute values ...

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

Vectorize.PNG

Option.PNG

Online overlord  
#10 Posted : 21 January 2023 19:10:49(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 900
Turkey

Was thanked: 359 time(s) in 240 post(s)
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 (187kb) downloaded 8 time(s).
Calculations2.pdf (795kb) downloaded 6 time(s).

2023-01-21_19-12.png
thanks 1 user thanked overlord for this useful post.
on 21/01/2023(UTC)
Offline uni  
#11 Posted : 21 January 2023 19:46:19(UTC)
uni


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 10/11/2010(UTC)
Posts: 1,343
Man
Russian Federation

Was thanked: 1174 time(s) in 676 post(s)
You can speed up by a few seconds if you enable numerical optimization for all expressions.

Calculations2 (numeric).sm (184kb) downloaded 4 time(s).

Click to enlarge

Edited by user 21 January 2023 19:55:23(UTC)  | Reason: Not specified

Russia ☭ forever
Viacheslav N. Mezentsev
Offline Xylene  
#12 Posted : 21 January 2023 21:47:51(UTC)
Xylene

Rank: Newbie

Groups: Registered
Joined: 20/01/2023(UTC)
Posts: 5
Poland

Was thanked: 1 time(s) in 1 post(s)
Thank you all for the help. To be honest I didn' expect such commitment. I see that Smath users community is awesome! Good
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 Good
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
Offline Razonar  
#13 Posted : 21 January 2023 22:29:45(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,068
Uruguay

Was thanked: 603 time(s) in 383 post(s)
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 (176kb) downloaded 11 time(s).

Best regards.
Alvaro.
Offline Jean Giraud  
#14 Posted : 22 January 2023 01:26:01(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Originally Posted by: Xylene Go to Quoted Post
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.
Online overlord  
#15 Posted : 22 January 2023 01:41:41(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 900
Turkey

Was thanked: 359 time(s) in 240 post(s)
Originally Posted by: Razonar Go to Quoted Post
Hi. I don't know if this is faster or not

It is indeed 1 second faster than my latest sample.
thanks 1 user thanked overlord for this useful post.
on 22/01/2023(UTC)
Offline Jean Giraud  
#16 Posted : 22 January 2023 04:45:38(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
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 (47kb) downloaded 5 time(s).
Offline Jean Giraud  
#17 Posted : 22 January 2023 20:39:34(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Calculations Comments.sm (168kb) downloaded 2 time(s).
Online overlord  
#18 Posted : 22 January 2023 21:53:28(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 900
Turkey

Was thanked: 359 time(s) in 240 post(s)
Originally Posted by: Xylene Go to Quoted Post
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?
Offline Jean Giraud  
#19 Posted : 24 January 2023 21:08:37(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,323
Canada

Was thanked: 918 time(s) in 746 post(s)
Xylene,
Does that graph make sense wrt your project ?
If so, can attach the document.
Cheers ... Jean.

UnkGraph.png
Offline Xylene  
#20 Posted : 24 January 2023 22:42:05(UTC)
Xylene

Rank: Newbie

Groups: Registered
Joined: 20/01/2023(UTC)
Posts: 5
Poland

Was thanked: 1 time(s) in 1 post(s)
Originally Posted by: overlord Go to Quoted Post
Originally Posted by: Xylene Go to Quoted Post
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 (172kb) downloaded 14 time(s).

Originally Posted by: Jean Giraud Go to Quoted Post
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.

Edited by user 24 January 2023 22:47:03(UTC)  | Reason: Not specified

thanks 1 user thanked Xylene for this useful post.
on 24/01/2023(UTC)
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.