SMath Studio Forum
»
SMath Studio
»
Samples
»
User-defined Functions in SMath Studio
Rank: Advanced Member Groups: Registered
Joined: 05/03/2009(UTC) Posts: 433 Location: USA Was thanked: 17 time(s) in 15 post(s)
|
Originally Posted by: ioan92 Originally Posted by: ElSid Update ... In SMath --> Help --> Examples --> Handbooks? If so, the PDF file in post #1 did not appear as a download Try this way: SMath Menu --> Tools --> Plugins --> Extensions Manager --> Handbooks Turns out it's the same location. Thank you
|
1 user thanked ElSid for this useful post.
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: RFreund 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? You would install parameters up above the function definition in the case parameters result from other specifics of the project, i.e: if parameters result from some type of calculations. Otherwise, if the parameters are "solid", put them below the definition at the point of usage, like near the plot so you can play with the graph. Words speak and may be confusing, image don't speak but verdict. Jean
|
1 user thanked Jean Giraud for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Originally Posted by: RFreund 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 You're right, I'll use your formulation in next revision Edited by user 24 March 2016 00:33:19(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 25/09/2013(UTC) Posts: 325 Location: IL Was thanked: 19 time(s) in 17 post(s)
|
Sorry I should have shown with an example.
Another way to say it would be:
"when a canvas variable is already defined at the function's definition, the value is stored..."
I suppose any of those works. It's just that when you read the word "at" you think that it is being somehow defined in the function itself.
Anyway, Thanks for confirming.
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
No "value is stored" ... the algo is stored, implicit of the associated argument. In this example, the argument is β indexed, stored in the algo stack. Jean Integrate Scalar Express.sm (12kb) downloaded 32 time(s).
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1127 time(s) in 723 post(s)
|
0.98.6103 It seems that certain changes happened in what is allowed as formal arguments in function definitions. I compare the pdf and the .sm file of the user function guide. - dummy argument: empty string no more allowed, empty placeholder (type #) is accepted instead. - unit names are now protected, they stick to their meaning no matter what value is used in the function call. |
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Thank you Martin, yes these behaviors were unintentionally introduced by Andrey in 0.98, patched recently; I'll update this document ASAP (here and in the EM too) Edited by user 19 September 2016 23:57:06(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1127 time(s) in 723 post(s)
|
Originally Posted by: Davide Carpi Thank you Martin, yes these behaviors were unintentionally introduced by Andrey in 0.98, patched recently; I'll update this document ASAP I don't mind these changes to remain. I think the empty placeholder is much better visual clue to something that does not matter than the empty string (and requires less typing). I also would suggest to allow leaving the placeholder empty if no input to the function is required. Just as with DocumentDirectory() |
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: mkraska I think the empty placeholder is much better visual clue Agreed 100% Martin ... 'x' makes "conventional sense" also. Anything else => for the birds .
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1127 time(s) in 723 post(s)
|
Hello Davide, I refer to your tips in the guide to user-defined functions: Code:- if you are running NUMERICAL calculations inside the function:
- if you don't want/need values from outside, you have to check that everything is defined
inside the function ("pure" function -> copy the function in an empty worksheet, run it
directly -> shouldn't run if something is missing in the function)
- if you use unknowns to get values from outside the function:
1) document somewhere the dependancies (f.e. in the description)
2) test it in an empty worksheet, define the dependacies and check the consequences of
the loss of any single dependancy (A,B,C -> keep A and B, remove C, then keep A and C
and remove B, etc..)
- if you are running SYMBOLICAL calculations inside the function, you may need to choose
hardly predictable unknowns' names (such as x#, _x, _x_, #x, §x, ...) to avoid
unintentional replacements from the canvas;
I know that using strange variable names within functions is common practice in SMath programming. However, somehow I have forgotten why we do this or at least fail to explain it. Could you please give an example for an unintended replacement which has to be avoided by strange names? |
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Currently I'm reworking the document, I think that this section will be changed to be more clear. Leaving aside [SS-111]/[SS-2327] (this is a bug, Andrey will work on it), issues are related to an existing variable on the parent level (commonly the canvas, but you can extend the logic to nested functions), and a variable with the same name in the function; cases in my knowledge: - unintentional pass-by-reference: this is not really a problem if you pay attention to it; if you don't want to pay attention, you can use the "strange variables' names" approach (not really a best practice, but it is a way); this is the only case in which you can modify unintentionally external variables from inside the function.
- Matrices/systems: if are not initialized within the procedure and you write only some elements or you change the sizes; this may affects the expected result.
- Symbolic calculations: you assign a local unknown to a variable, then you do stuff like differentiation thinking that the variable contains an unknown; this may affects the expected result. If the worksheet is sharable or with complex logic, hardly predictable unknown names here are almost mandatory IMO (plus it might help to identify that is a generic unknown).
In the example above, this is the logic when you evaluate f(3):
- v1 stores 13 (3+2*5), because b=5 exists on the top level;
- v2 stores 3+2*c, because c doesn't exists anywhere;
- o1 stores 2 (expected value), because the order of computation: differentiation is made, then the assignments looks to replace unknowns from RHS to store it in the variable on the LHS;
- o2 stores 2 (expected value), because v2 contains the unknown c;
- o3 stores 0, because you are differentiating 13;
P.S. please note that the canvas' variables here are placed after functions' definitions, to remark that the logic applies when you evaluate the function. Edited by user 21 September 2016 20:09:34(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
1 user thanked Davide Carpi for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1127 time(s) in 723 post(s)
|
Originally Posted by: Davide Carpi Symbolic calculations: you assign a local unknown to a variable, then you do stuff like differentiation thinking that the variable contains an unknown; this may affects the expected result. If the worksheet is sharable or with complex logic, hardly predictable unknown names here are almost mandatory IMO (plus it might help to identify that is a generic unknown).
Thank you Davide, this is exactly what I was looking for. Using Clear(), you can close the canvas-bypass for variables used inside the functions. This is just the symbolic version of initialization. As long as you don't use the internal names as arguments in the function call, you are safe. Thus, I have to admit that still the names of the internal variables matter. clear.sm (9kb) downloaded 25 time(s).Edited by user 21 September 2016 23:24:17(UTC)
| Reason: Not specified |
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
I didn't put much effort trying to understand what f(x) should do. For sure it's not going to cut the mustard for Engineers. If I would have to QA such a mess, I would return red crossed. On the other hand, "Clear" does nor clear, it does not empty the variable/constant. It will clear pi for 1 and it does not clear predefined items before maple symbolic evaluation. Smath clear is nothing compared to Mathcad that creates "inexistance".
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
The compactum is very useful, but not always compatible. There are more instances of "not compatible".
|
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
... those things you want to compact in some way must be tested on real applications. Bits and pieces are no proof. This document shows two cases of failure [1: the compactum, 2: Algo] Jean Spline Quadratic [Model].sm (90kb) downloaded 30 time(s).
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,650 Location: Italy Was thanked: 1338 time(s) in 878 post(s)
|
Originally Posted by: mkraska Using Clear(), you can close the canvas-bypass for variables used inside the functions. This is just the symbolic version of initialization. Yes, I guess it might be the best approach (probably the most clean even for occasional readers). Originally Posted by: Jean Giraud On the other hand, "Clear" does nor clear, it does not empty the variable/constant. It will clear pi for 1 and it does not clear predefined items before maple symbolic evaluation. Smath clear is nothing compared to Mathcad that creates "inexistance". You are right; this is because Clear() is not a built-in function, just rides a feature of line() wrt anything but functions. Two points to have a built-in clear() Edited by user 22 September 2016 12:39:07(UTC)
| Reason: Not specified |
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: Davide Carpi Two points to have a built-in clear() Yes Davide, but simpler than a "clear function", just like Mathcad: assign undefined ... a:= ... f(x):= In Mathcad, undefined would evaporate 'a'. If more paramaters were previously used, undefining all individually would render the symbolic "virgin" for expansion. That was Mathcad 11 and earlier versions, at least back to Mathcad 8 Pro. Alike evaporating parameters, Mathcad evaporates a function, f(x):= past undefining f(x):= 'f' can be re-used. In fact, we have it all naturally in Smath. 1. Assume f(a,b,c,x) was a user function, now just define f(x):=ln(x) 2. To clear parameters a,b,c ... just define a:=Shift+" [the blanking tool]. Not completely proofed. Jean Maple coeffs NumDen [Companion].sm (19kb) downloaded 29 time(s).Visit the end of the work sheet.
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,988 Was thanked: 1127 time(s) in 723 post(s)
|
Originally Posted by: Jean Giraud
Yes Davide, but simpler than a "clear function", just like Mathcad: assign undefined ... a:= ... f(x):=
I agree with the proposal to have a:# or f(1):# or f(#):x being understood as deleting a definition. That could even be understood by non-SMath people, although an explicit "undefined" would be less subject to interpretation. Edited by moderator 23 September 2016 01:30:41(UTC)
| Reason: fixed BBCODE |
|
|
|
|
Rank: Administration Groups: Developers, Registered, Knovel Developers, Administrators, Advanced Member Joined: 11/07/2008(UTC) Posts: 1,616 Was thanked: 1978 time(s) in 666 post(s)
|
Originally Posted by: Davide Carpi Leaving aside [SS-111]/[SS-2327] (this is a bug, Andrey will work on it) Fixed. Thank you!
|
1 user thanked Andrey Ivashov for this useful post.
|
|
|
Rank: Guest
Groups: Registered
Joined: 04/07/2015(UTC) Posts: 6,866 Was thanked: 981 time(s) in 809 post(s)
|
Originally Posted by: mkraska I agree with the proposal to have a:# or f(1):# or f(#):x being understood as deleting a definition. That could even be understood by non-SMath people, although an explicit "undefined" would be less subject to interpretation. Good suggestion except that the gyzma # is not accessible in Smath. Not directly accessible via the Shift 3 keyboard, neither via the extended Alt 035. Smath 5346, XP Home SP3. My western keyboard is OK, but ignored by Smath 5346. gyzma from keyboard # ... gyzma Alt 035 #
|
|
|
|
SMath Studio Forum
»
SMath Studio
»
Samples
»
User-defined Functions in SMath Studio
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.