How to implement counter variable / function

How to implement counter variable / function - Messages

#1 Posted: 2/24/2016 3:37:37 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Would anyone know if there is a way to define a variable or a function that increases in value by 1 every time it is called?

I am trying to set up automatic naming scheme for the image output of my excel_PNG plugin. It takes a filepath as an input to be used when saving generated png. What i need is to generate the filepath string which is different every time its called. It also needs to be consistent (i.e it cannot change for a particular function call every time worksheet is calculated) so random() will not work.

Ideas? Unique consistency of the value for a certain position within workbook is s the goal.

Here what i tried, predictably without success.

ID.png

ID.sm (3 KiB) downloaded 31 time(s).
#2 Posted: 2/24/2016 4:54:24 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Not very clear as you don't seem to search for a specific file.
Smath does not have the push button to keep stepping from file to file.

Jean
#3 Posted: 2/24/2016 5:41:06 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Alex,

Try this:
Alex.sm (4 KiB) downloaded 41 time(s).

Ioan



I put this also like here:
Alex2.sm (6 KiB) downloaded 33 time(s).



Thank you, Ioan, this might be my best bet. Not ideal though because it does require my input; I have about 100 calls to excel_PNG in one file, each of the call generates a PNG file, and it can be cumbersome to change the id variable for each of them.

So i take there is no "counter" function in SMath is there?

#4 Posted: 2/24/2016 5:43:57 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Not very clear as you don't seem to search for a specific file.
Smath does not have the push button to keep stepping from file to file.

Jean



Jean, what I want is a function that generates an incrementally different, non random value every time its called.
#5 Posted: 2/24/2016 6:58:06 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello Alex

You can try this: Alex4.sm (11 KiB) downloaded 44 time(s).

findstr(...) is because IsDefined() doesn't work as I expect inside the function, not sure if is a bug or something else.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
3 users liked this post
Andrey Ivashov 2/25/2016 9:31:00 AM, Alexander O. Melnik 2/24/2016 8:26:00 PM, ioan92 2/25/2016 5:13:00 AM
#6 Posted: 2/24/2016 8:14:41 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Not ideal though because it does require my input



Whether via the mouse [slider] or finger [keyboard], it rquires your input.
What I don't understand is: why don't you use IrfanView, much simpler, click
on the arrow thumbnail <= ... => There are dozens of this gadgets.
IrfanView will let you capture portion of the image +++

Jean


1 users liked this post
Alexander O. Melnik 2/24/2016 8:26:00 PM
#7 Posted: 2/24/2016 8:15:12 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Hello Alex

You can try this: Alex4.sm (11 KiB) downloaded 44 time(s).

findstr(...) is because IsDefined() doesn't work as I expect inside the function, not sure if is a bug or something else.



Works great, thank you very much! I almost thought it is not possible hehe.

Now I do not quite get how the if() loop works, and why does it work, and why it shall not be equal to -1. Could you walk me through this line of code, explaining what it does exactly?

findstr(num2str(id+1),"+")≠-1

Thanks again, this is very clever and elegant!
#8 Posted: 2/24/2016 8:16:52 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Alex,

I thought on a use like here:Alex3.sm (9 KiB) downloaded 39 time(s).
You have the file name as you need (?) containing a variable.



Thank you Ioan, your function incorporates "current folder" very nicely - will use it in my worksheets, if you do not mind :-)
#9 Posted: 2/24/2016 8:18:01 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Wrote

Not ideal though because it does require my input



Whether via the mouse [slider] or finger [keyboard], it rquires your input.
What I don't understand is: why don't you use IrfanView, much simpler, click
on the arrow thumbnail <= ... => There are dozens of this gadgets.
IrfanView will let you capture portion of the image +++

Jean






Jean, if you can tell me exactly what you do not understand about what I would like to do I will gladly explain it ( I did not quite get it from your post)
#10 Posted: 2/24/2016 8:19:29 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Well my headache was solved by Davide and Ioan, thank you guys!
#11 Posted: 2/24/2016 8:34:22 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Jean, if you can tell me exactly what you do not understand about what I would like to do I will gladly explain it ( I did not quite get it from your post)



From Ioan & Davide, it's OK to view image in Smath, but useless as you can't
further process/select ... etc. You can "fmap vectorise", scale the grayscale
but can't scale RGB replot because Smath does not have the "Picture operator".

Jean

#12 Posted: 2/24/2016 8:41:13 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Wrote

Jean, if you can tell me exactly what you do not understand about what I would like to do I will gladly explain it ( I did not quite get it from your post)


it's OK to view image in Smath, but useless as you can't
further process/select ... etc.




From perspective of a person who prepared about 1200 pages of calculation books over past year only I can tell that it is very important (for me) to view pictures in the calcbooks without further processing. I could not care less to perform linear algebra on them. For me they are just sketches, not matrices.

Cheers,


Alex M.
#13 Posted: 2/25/2016 1:30:04 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

I understand your point of storing dead stuff...
Smath is live for Engineering/Edu/Sciemtific ...


Image W truss.gif


What's the point to read that dead image if the construction is not exemplified ?
There was a long thread on that one, particularly about not a "parrot plot"...
Maybe not yet finished ! ? I admit, humbly, I'm not a "bright spark".

Jean
1 users liked this post
ioan92 2/25/2016 5:16:00 AM
#14 Posted: 2/25/2016 2:36:03 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

... preferences aren't arguable. If I need an image in an Smath work sheet,
for instance a source image from reference, I save it *.GIF [256].
Bad luck, Smath does not open *.gif [not 24/32 bbp]. Why should I kneel down
and restrict myself because of Smath. Open my image in IrfanView, copy/paste
in Smath ... job done little memory, much faster than waiting Smath opens
an *.BMP or *.PNG. There are many ways to skin a cat [I didn't invent].

Jean
#15 Posted: 2/25/2016 6:53:43 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Wrote

Works great, thank you very much! I almost thought it is not possible hehe.

Now I do not quite get how the if() loop works, and why does it work, and why it shall not be equal to -1. Could you walk me through this line of code, explaining what it does exactly?

findstr(num2str(id+1),"+")≠-1

Thanks again, this is very clever and elegant!


You're welcome

The function uses pass-by-reference to output the value to the canvas via the input argument.
If the input value is an unknown, will be set to 1. This should be done in the if/else statement using the built-in ¬IsDefined(id) that for some reason I have to investigate doesn't works, so I've replaced it with findstr(num2str(id+1),"+")≠-1
num2str(id+1) converts the input in a math string -> id+1 will be converted in a number and then in a string if the input argument is a number, otherwise will be "something+1" where something is the name of the input unknown in the canvas (because id is a local name that contains the variable passed from the canvas -> a simple num2str(id)≡"id" is not possible); in the first case, findstr will returns -1, because the string contains a number, in the second case will returns a vector that contains the index of each "+" match. Hence if findstr(...)≠-1 -> the variable is not a number, set it to 1, otherwise increment by one.


Wrote

From Ioan & Davide, it's OK to view image in Smath, but useless as you can't
further process/select ... etc. You can "fmap vectorise", scale the grayscale
but can't scale RGB replot because Smath does not have the "Picture operator"


Jean, I think that both the purposes are OK and can/must coexist

If one needs only to show an image on the canvas, there's no need to fill the worksheet memory or the file itself of useless data.

If one want/needs to process images, this is another story (BTW, many thanks for all the worksheets you share here in the forum )
IMHO as it is now processing images on the worksheet is reliable only for small images (only teaching purposes); using plugins you can boost the math speed or build a region that can do all "in-place" but this is not funny because the math is hidden under the hood. A "picture operator" or a boost in matrices management is needed and can be done only by Andrey (here we are focusing on images but the issue may be the same in any signal processing problem, such as audio, wind, etc...).

The example below is ok for a 128x128 image, but too slow for a 256x256 image (or bigger), using greyscale images, and functions driven in plugin: fftmagnitude (abs() extended to matrices), fftphase (arg() extended to matrices), fftlog (basically a logarithm function for matrices); to run all except the ffts in the canvas (in a reasonable time) 128x128 is too big.
2016-02-25 10_46_19-SMath Studio Desktop - [shift.sm_].png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#16 Posted: 2/25/2016 10:35:10 AM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Davide, thank you for explanation, very useful.

I would agree with Davide that it is not the lack of picture operator, but the lack of processing horsepower that makes any sort of image/signal processing impossible in SMath. Right now it takes good 8 seconds just to display a 1000x2000 matrix in the worksheet. And than it lags when you scroll said worksheet as well..

Davide, was this ever brought up to Andrey? Matrix manipulation is an important bit for practical use of SMath and right now it is on a slower side.
#17 Posted: 2/25/2016 12:15:37 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Maybe, not yet from my side... about the displaying issues I've made a request in the BTS recently (SS-2291)

You can test performances with this simple function: mlog.sm (3 KiB) downloaded 32 time(s).
Try to change the size of the identity matrix, a 128x128 makes the function sooooooo slow...
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#18 Posted: 2/25/2016 5:59:28 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Wrote

Davide, was this ever brought up to Andrey? Matrix manipulation is an important bit for practical use of SMath and right now it is on a slower side.


Thank you for noting this. Just reviewed a code and found possibility to make great improvement to some operations with matrices.

This is latest available public build of SMath Studio (see the calculation time).
matrix_speed_test_1.png

This is a new build I've just created locally (changes not related to matrix() function - the overall speed of communication between SS Core and built-in symbolic library improved):
matrix_speed_test_2.png

...and even bigger:
matrix_speed_test_3.png

Will see if I can found some other places to improve.

Best regards.
4 users liked this post
Alexander O. Melnik 2/25/2016 6:03:00 PM, ioan92 2/25/2016 6:09:00 PM, Davide Carpi 2/25/2016 8:21:00 PM, sergio 2/26/2016 5:42:00 AM
#19 Posted: 2/25/2016 6:05:38 PM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

Wrote

Davide, was this ever brought up to Andrey? Matrix manipulation is an important bit for practical use of SMath and right now it is on a slower side.


Thank you for noting this. Just reviewed a code and found possibility to make great improvement to some operations with matrices.

Will see if I can found some other places to improve.

Best regards.



Andrey, this is incredible!

If you can make matricies fly (both calcs and UI / scrolling lag) its a huge-huge plus; this will opens doors to do "real" structural analysis, signal processing, image manipulations... Our digital world is made of matricies!

Thank you very much

Alex M.
  • New Posts New Posts
  • No New Posts No New Posts