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

Notification

Icon
Error

3 Pages123>
Options
Go to last post Go to first unread
Offline Davide Carpi  
#1 Posted : 14 February 2016 14:03:59(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
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 (294kb) downloaded 351 time(s).
User-defined functions in SMath Studio.pdf (723kb) downloaded 523 time(s).

P.S. I beg you pardon in advance if there are grammar errors, or wrong examples Blush
P.P.S. This may be used as trace to update the wiki, if it is consistent and clear enough

Edited by user 09 December 2016 01:35:33(UTC)  | Reason: fixed typos in rev.8

If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 5 users thanked Davide Carpi for this useful post.
on 14/02/2016(UTC),  on 14/02/2016(UTC),  on 14/02/2016(UTC),  on 15/02/2016(UTC),  on 15/02/2016(UTC)
Offline Jean Giraud  
#2 Posted : 14 February 2016 21:24:22(UTC)
Jean Giraud


Rank: Advanced Member

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 3,971
Canada

Was thanked: 708 time(s) in 554 post(s)
Thanks Davide.

Jean
thanks 1 user thanked Jean Giraud for this useful post.
on 15/02/2016(UTC)
Offline Andrey Ivashov  
#3 Posted : 15 February 2016 19:33:35(UTC)
Andrey Ivashov


Rank: Administration

Groups: Developers, Registered, Knovel Developers, Administrators, Advanced Member
Joined: 11/07/2008(UTC)
Posts: 1,377
Man
Russian Federation

Was thanked: 1607 time(s) in 538 post(s)
Thank you, Davide!

Did you see this: Absolute definitions? Whistle
thanks 2 users thanked Andrey Ivashov for this useful post.
on 15/02/2016(UTC),  on 15/02/2016(UTC)
Offline Davide Carpi  
#4 Posted : 15 February 2016 20:53:35(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Originally Posted by: Andrey Ivashov Go to Quoted Post
Thank you, Davide!

Did you see this: Absolute definitions? Whistle


Hello Andrey! Thank you

Absolutely to add into the next revision, thank you very much! Biggrin
If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
Offline Davide Carpi  
#5 Posted : 16 February 2016 11:07:10(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Hello Ioan,

Originally Posted by: ioan92 Go to Quoted Post
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 Good

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

Originally Posted by: ioan92 Go to Quoted Post
* 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;
B) 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)

Edited by user 16 February 2016 11:22:03(UTC)  | Reason: Not specified

If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 1 user thanked Davide Carpi for this useful post.
on 16/02/2016(UTC)
Offline Jean Giraud  
#6 Posted : 16 February 2016 16:59:10(UTC)
Jean Giraud


Rank: Advanced Member

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 3,971
Canada

Was thanked: 708 time(s) in 554 post(s)
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 (89kb) downloaded 40 time(s).

Offline Davide Carpi  
#7 Posted : 16 February 2016 17:54:58(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Hello Jean,

Originally Posted by: Jean Giraud Go to Quoted Post
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 a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
Offline Davide Carpi  
#8 Posted : 19 March 2016 21:57:14(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
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 a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 2 users thanked Davide Carpi for this useful post.
on 19/03/2016(UTC),  on 19/03/2016(UTC)
Offline Davide Carpi  
#9 Posted : 20 March 2016 12:03:29(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Thanks for the feedback guys Good

Originally Posted by: ioan92 Go to Quoted Post
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 :'(

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


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

2016-03-20 10_34_58-SMath Studio Desktop - [Page1_].png
number.sm (3kb) downloaded 29 time(s).

Edited by user 20 March 2016 14:52:35(UTC)  | Reason: Not specified

If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 1 user thanked Davide Carpi for this useful post.
on 20/03/2016(UTC)
Offline Davide Carpi  
#10 Posted : 20 March 2016 14:52:12(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Originally Posted by: ioan92 Go to Quoted Post
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 Good
If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
Offline Jean Giraud  
#11 Posted : 20 March 2016 16:27:20(UTC)
Jean Giraud


Rank: Advanced Member

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 3,971
Canada

Was thanked: 708 time(s) in 554 post(s)
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 (49kb) downloaded 30 time(s).
Offline mikekaganski  
#12 Posted : 21 March 2016 16:12:27(UTC)
mikekaganski


Rank: Advanced Member

Groups: Registered
Joined: 17/01/2013(UTC)
Posts: 296
Man
Russian Federation
Location: Khabarovsk, Russia

Was thanked: 151 time(s) in 107 post(s)
1.4 IMPORTANT [C] - there should be "a", not "b"
Best regards,
Mike Kaganski
thanks 1 user thanked mikekaganski for this useful post.
on 21/03/2016(UTC)
Offline mikekaganski  
#13 Posted : 21 March 2016 16:39:07(UTC)
mikekaganski


Rank: Advanced Member

Groups: Registered
Joined: 17/01/2013(UTC)
Posts: 296
Man
Russian Federation
Location: Khabarovsk, Russia

Was thanked: 151 time(s) in 107 post(s)
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
Best regards,
Mike Kaganski
Offline Alex M.  
#14 Posted : 21 March 2016 17:56:59(UTC)
Alex M.


Rank: Advanced Member

Groups: Registered
Joined: 03/03/2014(UTC)
Posts: 402
Canada

Was thanked: 119 time(s) in 92 post(s)
Originally Posted by: mikekaganski Go to Quoted Post
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...
Offline Davide Carpi  
#15 Posted : 21 March 2016 20:43:14(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Originally Posted by: mikekaganski Go to Quoted Post
1.4 IMPORTANT [C] - there should be "a", not "b"

Thank you Mike, fixed!

Originally Posted by: Alex.M Go to Quoted Post
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? Umnik

Originally Posted by: ioan92 Go to Quoted Post
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 Good

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)

Edited by user 21 March 2016 20:53:49(UTC)  | Reason: Not specified

If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 1 user thanked Davide Carpi for this useful post.
on 21/03/2016(UTC)
Offline Jean Giraud  
#16 Posted : 21 March 2016 23:51:03(UTC)
Jean Giraud


Rank: Advanced Member

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 3,971
Canada

Was thanked: 708 time(s) in 554 post(s)
Mathcad:
"This variable or function is undefined above"
"The expression to the left of the equal sign can not be defined"
Offline Davide Carpi  
#17 Posted : 22 March 2016 18:15:17(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Hello Ioan, Good

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

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

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

Edited by user 22 March 2016 18:21:38(UTC)  | Reason: Not specified

If you like my plugins please consider a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 3 users thanked Davide Carpi for this useful post.
on 22/03/2016(UTC),  on 22/03/2016(UTC),  on 23/03/2016(UTC)
Offline ElSid  
#18 Posted : 22 March 2016 23:39:38(UTC)
ElSid


Rank: Advanced Member

Groups: Registered
Joined: 05/03/2009(UTC)
Posts: 279
Man
United States
Location: USA

Was thanked: 9 time(s) in 8 post(s)
Davide or ioan92,
Where is the Extensions Manager - Handbook? Never seen it in the forums. Wiki?
Originally Posted by: ioan92 Go to Quoted Post
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

Edited by user 22 March 2016 23:48:01(UTC)  | Reason: Possible answer

Offline Davide Carpi  
#19 Posted : 23 March 2016 01:38:11(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,200
Man
Italy
Location: Italy

Was thanked: 1113 time(s) in 727 post(s)
Originally Posted by: ElSid Go to Quoted Post
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 a donation to SMath Studio; for personal contributions to me: paypal.me/dcprojects
thanks 2 users thanked Davide Carpi for this useful post.
on 23/03/2016(UTC),  on 23/03/2016(UTC)
Offline RFreund  
#20 Posted : 23 March 2016 05:16:40(UTC)
RFreund


Rank: Advanced Member

Groups: Registered
Joined: 25/09/2013(UTC)
Posts: 289
United States
Location: IL

Was thanked: 18 time(s) in 16 post(s)
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
thanks 1 user thanked RFreund for this useful post.
on 23/03/2016(UTC)
Users browsing this topic
3 Pages123>
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.