User-defined Functions in SMath Studio

User-defined Functions in SMath Studio - A guide to the user-defined functions - Messages

#1 Posted: 2/14/2016 8:03:59 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello anyone,

I've tried to summarize with progressive examples how functions works in SMath Studio

User-defined Functions in SMath Studio [rev.8].sm (293 KiB) downloaded 1036 time(s).
User-defined functions in SMath Studio.pdf (722 KiB) downloaded 1627 time(s).

P.S. I beg you pardon in advance if there are grammar errors, or wrong examples
P.P.S. This may be used as trace to update the wiki, if it is consistent and clear enough
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
6 users liked this post
ioan92 2/14/2016 8:07:00 AM, Radovan Omorjan 2/14/2016 8:44:00 AM, Вячеслав Мезенцев 2/14/2016 9:26:00 AM, Peter 2/15/2016 2:40:00 AM, Andrey Ivashov 2/15/2016 1:25:00 PM, oscar mauricio 11/28/2021 12:54:00 AM
#2 Posted: 2/14/2016 3:24:22 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Thanks Davide.

Jean
1 users liked this post
Davide Carpi 2/14/2016 6:50:00 PM
#3 Posted: 2/15/2016 1:33:35 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Thank you, Davide!

Did you see this: Absolute definitions?
2 users liked this post
Davide Carpi 2/15/2016 2:45:00 PM, Вячеслав Мезенцев 2/15/2016 2:52:00 PM
#4 Posted: 2/15/2016 2:53:35 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Wrote

Thank you, Davide!

Did you see this: Absolute definitions?



Hello Andrey!

Absolutely to add into the next revision, thank you very much! :d
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#5 Posted: 2/16/2016 5:07:10 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello Ioan,

Wrote

I use, as well for the input data into a function, as for the output of the results, the "System of values" technique


I've planned to add at the end of the file some examples of one function made with several techniques, obviously the system/matrix output will be one

Wrote

I have also two small questions:
* Is it preferable to include into a function body, the definition of other functions or, maybe
it better to let outside (isolated) any function definition ?


From performances side, since functions are made to run a task many times (hopefully "a lot"), hence would be better to define the function outside (at once), otherwise the nested function will be defined every time you run the function.
More, nesting big functions may makes less readable the code.

Wrote

* Is there any restriction for calling inside a user function, of an user function defined outside?


AFAIK nope.


Golden rules:
1) Everywhere: when a definition is executed, if something is defined the value is stored, otherwise the name is stored;
2) In programming functions (functions+line): values are passed by reference -> an assignment inside the function to an argument changes his value outside;

If you have to figure out how to manage unknowns:
A) think about a programming function without nested functions and an unknown;
in the flow on the canvas, imagine to replace the LHS with the RHS when the function is called to be evaluated;
C) the local unknown now is exposed to the canvas; when an assignment (:=) or an evaluation (=) that involves the unknown is executed, then apply the rule (1) -> everything is on the same level now, if a variable with the same name of the unknown was defined before -> a value is available and this value is used;
D) a further execution in the flow will be applied as in (C) (because is a function, the behavior replicates at each call)
E) imagine that the canvas was not the canvas, just a parent programming function of the 1st function -> you can do the same on the parent level (that may be the canvas or another function)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
ioan92 2/16/2016 5:14:00 AM
#6 Posted: 2/16/2016 10:59:10 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Salut Ioan,

About your first question, just be careful because Smath encapsulates.
Disaster when not used how it works, but gorgeous when used properly.
The problem remains Smath can't clear previously used variable. If the
next use overwrites completely: fine.
Otherwise scrapped result/syntax error/worst crashes Smath.
It toke me less than a minute to collect my Venturi points from the
ContourExtract and get the data set ready for the Genfit.

Chaining functions or other stuff is more delicate and depends upon the
goal to achieve [the algorthmic structure], NO general rule.
Davide rescued my Bézier, if you didn't catch, easy to attach agaain.

In the attached sheet, one may pedal trying to understand. No need to
sweat blood: the structure of the vector gradient governs and the only
way to make it work is as exposed. From there on, the structure of the
Cholesky olver follows, the last call of the model function wrt the
parameters auto-indexed ... job done. In fact this solver works better
than OriginLab and Mathcad because it does not depend upon LM
[LM = Levenberg-Marquardt least square]

The second example "spec" is passed by algo name, internally unested.
Ready to use in-situ. Works well and visible.

Example 3: can't be simpler. The Gaussian function is passed entirely,
just as one would be thinking of doing. The final evaluation collects
the user discrete data set, finally vectorised over a mapping function.

Smath supports lots of tortuting techniques ... good boy !

Often we have to reconsider the draw of the project, especially on
large projects that the client needs be confident and understand.
An algorithmic structure must be clear and doctorable.

Cheers Ioan and all.

Jean

Utilities Algo Define.sm (88 KiB) downloaded 82 time(s).

#7 Posted: 2/16/2016 11:54:58 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello Jean,

Wrote

The problem remains Smath can't clear previously used variable.


True, a generic built-in Clear() function to unset any object defined before would be really useful; BTW the one in CustomRegion can clean variables and is based on the magic wormhole x:line(x,1,1)

2016-02-16 15_52_31-SMath Studio Desktop - [AS%207-7solve.sm_].png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#8 Posted: 3/19/2016 3:57:14 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

UPDATED

added: vectorize(), imaginary as argument, dynamic arrays, dummy arguments, absolute definitions, examples (5 ways to execute the same task)

see post #1
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
2 users liked this post
ioan92 3/19/2016 4:02:00 PM, frapuano 3/19/2016 4:07:00 PM
#9 Posted: 3/20/2016 6:03:29 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Thanks for the feedback guys

Wrote

PS: I hadn't noticed your advices concerning my post #9 ?


I'll add them. Unfortunately Yesterday before publishing I've had to rewrite the worksheet because the file was corrupted and wasn't in a synchronized folder

Wrote

Neither as for ... control variable ?


I've slighty modified the text of the advice (i can be used in the argument as input but not as name)

2016-03-20 09_42_41-SMath Studio Desktop - [User-defined Functions in SMath Studio.sm_].png

In simple examples the use of i as control variable (vs imaginary numbers) it works surprisingly fine, but in more complex tasks, should be investigated.

However if you may expect imaginary results, IMHO you shouldn't use i as control (for sanity of the reader, even if the program can handle it correctly)

2016-03-20 11_20_32-SMath Studio Desktop - [imaginary.sm_].png


Wrote

f(x):=1+2*x+3*x
i:=1.23456789
f(i)=7.172839

In CAS we go by the CAS rules.

f(1.23456789):=1+2+1.23456789=#@# [Syntax is incorrect]



Works for me (maybe I miss something)

2016-03-20 10_34_58-SMath Studio Desktop - [Page1_].png
number.sm (2 KiB) downloaded 61 time(s).
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
ioan92 3/20/2016 6:44:00 AM
#10 Posted: 3/20/2016 8:52:12 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Wrote

May I propose to you that after finishing this document, to include it (as pdf) in the SMath - Extensions Manager - Handbooks
if not, it can be lost in the sands as many other valuable contributions.



Sure
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#11 Posted: 3/20/2016 10:27:20 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Salut Ioan,

No need to put anything of that conversation in Hanbook.
It is just some refresher of "numerical maths knowledge".
When it comes to "compute", computer are dummy like stone.
Computers do less than kindengarden stuff. Computers know
only the addition. With supplementary instruction it can
be instructed to add/add/add/add ... then multiply. A bit
more instuctions ... wow ! subtraction. Division is another
monkey business of the 3 first above. And that's all what
"Big blues" or your French Terra 100 [Bull] that consumes
a Nuclear Power station do. Naturally of this so low "Do"
a large world of decoration have been added to make them
look more savant and more mystic.
At this point and in short, computers perform only the 4
arithmetic operations [+, -, *, /]. Division / being very
slow compared to +

How things [functions and the likes] are computed ?
ANSWER: by savant combination of the 4 [+, -, *, /]
called numerical approximations. In PC Pentium style
most range in the order of 21 Aops [Arithmetic operations]
i.e: the summ of [+, -, *, /]... exp(x), sin(x), ln(x) ...
X^Y consumes twice because it needs exp(x) & ln(x).
SQRT(x) is very fast because iterative Heron-Newton.
It starts from a very little numerical initialising push.

Revisit "Samples" for Bessel. See what the right side is
crunching, only [+, -, *, /]. What are called "Main Frame"
try to execute "Arithmetica" a kind of coincident bit wise
arithmetic multiple parallel computation at cryogenic °C.
Two constants are universally known [pi, e]. More are "user".

Gorgeous blue sky ! Cheers

Jean A+

Forum Procedure.gif


Algo Basic.sm (48 KiB) downloaded 62 time(s).
#12 Posted: 3/21/2016 10:12:27 AM
Mike Kaganski

Mike Kaganski

184 likes in 434 posts.

Group: User

1.4 IMPORTANT [C] - there should be "a", not "b"
С уважением, Михаил Каганский
1 users liked this post
Davide Carpi 3/21/2016 2:06:00 PM
#13 Posted: 3/21/2016 10:39:07 AM
Mike Kaganski

Mike Kaganski

184 likes in 434 posts.

Group: User

This was discussed in [B]; I beleive that here (in [C]) is discussed that as "a" was already defined by f()'s definition, now changing "a" doesn't mean nothing
С уважением, Михаил Каганский
#14 Posted: 3/21/2016 11:56:59 AM
Alexander O. Melnik

Alexander O. Melnik

127 likes in 494 posts.

Group: Moderator

Wrote

This was discussed in [B]; I beleive that here (in [C]) is discussed that as "a" was already defined by f()'s definition, now changing "a" doesn't mean nothing



Pure curiosity - is this definition method used anywhere but SMath?

I got used to it by now, however it would seem to be simpler if something that was defined as a variable would be stored as a variable...
#15 Posted: 3/21/2016 2:43:14 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Wrote

1.4 IMPORTANT [C] - there should be "a", not "b"


Thank you Mike, fixed!

Wrote

Pure curiosity - is this definition method used anywhere but SMath?

I got used to it by now, however it would seem to be simpler if something that was defined as a variable would be stored as a variable...


No idea; once it is documented is not a problem for me. Consider that keeping one of the two approaches you have to find a way to provide the other. If you store only the name instead of the value -> for(j,range(1,10),x:x+1) should returns x:x+1 as final result?

Wrote

A variable, used in a function,defined before the function definition is used as constant for that function.nAlthough, this a variable value can be changed as value for other uses, as it can be seen here.

If this aspect is confirmed by Davide, I think it could be useful to be explicated in his document.


It is already written: IMPORTANT "[...] hence changes on variable's value doesn't affects function's results when executed."

I accept any suggestion about better ways to explain this or other points

In the new update I've added also these points: [1.4] (argument name), [1.6] (functions in inline functions), [2.3] (functions in dependent variables), [3.15]/[3.16] (nesting functions), [3.17] (recursive functions), [3.20] (rename a variable in functions)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
ioan92 3/21/2016 2:46:00 PM
#16 Posted: 3/21/2016 5:51:03 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Mathcad:
"This variable or function is undefined above"
"The expression to the left of the equal sign can not be defined"
#17 Posted: 3/22/2016 12:15:17 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Hello Ioan,

Wrote

I hardly doubt if the major part of SMath users known and applied all this in their practice


Any program/environment/OS has a lot of most unused features and peculiarities, more or less powerful, but sometimes they may come in handy to have them. In excel, cell names, vlookup, conditional formatting, ... all very powerful but ignored by a lot of people (even skilled). Here just think about the recent Alex code to increment the filename or the "Sets of all disjoint pairs of a set of even elements" of frapuano; both solved with features already available in SS with few differences from other languages... there are other tricky ways to accomplish these tasks, but you have to reinvent the wheel. Anyway if not documented these features are useless or even harmful.

Wrote

I'm tempted for an indiscreet question: where from do you know all this? Is the natural comparison with something known, your "trick"?[/indent]


I think it is because some of these features are available in most common programming languages/environments (since Fortran to the latest); but in the end I've just spent time testing the limits and figure out schemes followed by the program...

Wrote

I know that by insisting, I risk hardly to be taken as a stubborn guy, and therefor, preventive, I ask pardon (I hope no offense), but:

* at this point there are two parts implied: the variable (a) and the function;
* you pointed the the fate of the function; for me is important also what what can we do with that "duplicity" variable;
* I believe you think in the frame of the programming logic - by name / by value; "try also the other one skin"; my reflex is by practical/religious rule of good behavior.


However, we are dealing with some nuances, viewed from different angles.

If I may, thank you again for this excellent contribution


No offense, and no problems. I'll try to point out better this issue.
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
Alexander O. Melnik 3/22/2016 12:41:00 PM, ioan92 3/22/2016 12:20:00 PM, frapuano 3/22/2016 7:34:00 PM
#18 Posted: 3/22/2016 5:39:38 PM
Ruben Sidranski

Ruben Sidranski

17 likes in 463 posts.

Group: User

Davide or ioan92,
Where is the Extensions Manager - Handbook? Never seen it in the forums. Wiki?
Wrote

May I propose to you that after finishing this document, to include it (as pdf) in the SMath - Extensions Manager - Handbooks...


Update ... In SMath --> Help --> Examples --> Handbooks? If so, the PDF file in post #1 did not appear as a download
#19 Posted: 3/22/2016 7:38:11 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: Moderator

Wrote

the PDF file in post #1 did not appear as a download



I hadn't yet uploaded it, now is available
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
2 users liked this post
ElSid 3/22/2016 11:17:00 PM, ioan92 3/23/2016 7:41:00 AM
#20 Posted: 3/22/2016 11:16:40 PM
Ryan Freund

Ryan Freund

19 likes in 339 posts.

Group: User

Davide,

When you say
"when a canvas variable is defined at function's definition, the value is stored..."
This really means:
"when a canvas variable is defined before the function's definition, the value is stored..."
before = above or left of the function (on the canvas)
Right?

Thanks
1 users liked this post
Davide Carpi 3/23/2016 3:55:00 PM
  • New Posts New Posts
  • No New Posts No New Posts