User-defined Functions in SMath Studio - A guide to the user-defined functions - Messages
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
Jean
WroteThank you, Davide!
Did you see this: Absolute definitions?![]()
Hello Andrey!

Absolutely to add into the next revision, thank you very much! :d
WroteI 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

WroteI 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;

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)
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).
WroteThe 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)
added: vectorize(), imaginary as argument, dynamic arrays, dummy arguments, absolute definitions, examples (5 ways to execute the same task)
see post #1

WrotePS: 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

WroteNeither 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)
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)
Wrotef(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)

number.sm (2 KiB) downloaded 61 time(s).
WroteMay 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

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+
Algo Basic.sm (48 KiB) downloaded 62 time(s).
WroteThis 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...
Wrote1.4 IMPORTANT [C] - there should be "a", not "b"
Thank you Mike, fixed!
WrotePure 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?

WroteA 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)
"This variable or function is undefined above"
"The expression to the left of the equal sign can not be defined"

WroteI 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.
WroteI'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...
WroteI 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.

Where is the Extensions Manager - Handbook? Never seen it in the forums. Wiki?
WroteMay 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
Wrotethe PDF file in post #1 did not appear as a download
I hadn't yet uploaded it, now is available

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
-
New Posts
-
No New Posts