User-defined Functions in SMath Studio - A guide to the user-defined functions - Сообщения
WroteTry this way:WroteUpdate ... In SMath --> Help --> Examples --> Handbooks? If so, the PDF file in post #1 did not appear as a download
SMath Menu --> Tools --> Plugins --> Extensions Manager --> Handbooks
Turns out it's the same location. Thank you
WroteWhen 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
WroteDavide,
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

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.
implicit of the associated argument. In this
example, the argument is β indexed, stored
in the algo stack.
Jean
Integrate Scalar Express.sm (11 КиБ) скачан 57 раз(а).
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.
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)

WroteThank 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()
WroteI think the empty placeholder is much better visual clue
Agreed 100% Martin ... 'x' makes "conventional sense" also.
Anything else => for the birds .
- 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?
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;
- v1 stores 13 (3+2*5), because b=5 exists on the top level;
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.
Wrote
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 (8 КиБ) скачан 48 раз(а).
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".
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 (89 КиБ) скачан 58 раз(а).
WroteUsing 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).
WroteOn 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()

WroteTwo 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 (18 КиБ) скачан 59 раз(а).
Visit the end of the work sheet.
Wrote
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.
WroteLeaving aside [SS-111]/[SS-2327] (this is a bug, Andrey will work on it)
Fixed. Thank you!
WroteI 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 #
-
Новые сообщения
-
Нет новых сообщений