User-Defined Functions in Smath Studio
User-Defined Functions in Smath Studio
sm
Inline
__functions
To define a function, type function's name and open a parenthesis "(", write the argument
name, move on the right of the closing parenthesis and then type the define operator ":".
3 1
[1.1] f x x
x
f 2 8.5 <- type f and look at the dynamic assistance, you will find the
functiom name and his content.
IMPORTANT
type = to evaluate numerically f 2 8.5
17
type CTRL+. to evaluate symbolically f 2
2
[1.2] F x x 3
f 2 8.5
F 2 5
It is possible to define several functions with the same name, if they have a different
number of arguments.
[1.3] f x, y 2 x 3 y 2 x 3 y
f 1, 2 8
f 2 8.5 <- f(x) is still defined; if you look at the dynamic assistance,
2 items are available now, f(1) and f(2). The number shows the
arguments required by each function, whereas the name without
numbers is used when there is just one function (see [1.1])
1 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
In SS a valid variable name cannot start with a number; it cannot contains whitespaces nor
the character used as argument separator in functions, as well as @ and the math symbols:
()[]{}+-/*=:≡<>≤≥≠¬&|¤±...
When you define a function, argument's name can be only: a variable name or a function;
units and empty operand can be used only as placeholders.
IMPORTANT
[1.5]
[A] f 1 a b 3 i <- the name of an argument cannot be a number
f x if x 2 g x x 2 10 x 2 2 x
10
else
2 x
[B] f x3 x3 4 <- the variable name can contain numbers (not as 1st character)
f 2 6
f 2 7
[E] f x 9 <- dummy argument (argument not used in the function - will be
discussed later in [3.14])
f 2 9 x
f 2 7 x
π
[G] f g x 2 g 0 3 g <- a function is a valid argument
5
(will be further discussed from [3.10] to [3.13])
π
f F x 16.885 2 F 0 3 F 16.885
5
π
f sin x 1.7634 2 sin 0 3 sin 1.7634
5
π
f exp x 7.6234 2 exp 0 3 exp 7.6234
5
2 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
You can access variables from the canvas (not passed by the arguments), defined or even
not yet defined (from left to right and from top to bottom, see [1.4]).
[1.6] a 5
f x a b 3 x <- hold the mouse over the function to see how "a" and "b"
are stored; you can do the same from the dynamic assistance
[A] b 2
f 2 13
IMPORTANT
[B] b 3 <- if a variable is not yet defined when you define the
function, it is kept as symbolical link (the name is stored);
f 2 14 hence if afterwards you change the value of the variable,
IMPORTANT the function will returns a different result.
[A] F x 5
f 1 6
[C] G x 3 x
You can use vectorize() to extend scalar logic to matrices/vectors elements avoiding the
use of loops; this can be done applying vectorize on you function or inside your function.
[1.8] f x x 1
1 2 3
M
1 2 3
0 1 2
f M <- the scalar logic is extended to any element of the matrix,
0 1 2 because requested by vectorize(...)
f 2 1
[1.9] f x x 1
0 1 2
f M <- the scalar logic is always extended to any matrix's element
0 1 2
f 2 1
3 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Dependent
__variables
Like in paper-math you may want to have a function without explicit arguments: y f x
d
[2.1] y 2 x 3 ẋ x t
dt
[A] x 2
y 7
[B] x 3
y 9
d
[C] y 2 <- even if x is defined at the execution, y keeps the symbolical link
dx (look at the dynamic assistance and [1.5][B])
2
y d x 10
0
[2.2] b 5
x 2 z b c 5 c 2 z <- hold the mouse over the variable to see how "b" and "c"
are managed: c was undefined and his name is stored,
[A] c 2 b was defined and his value is replaced and stored.
x 7 2 z
[B] c 4
x 9 2 z
[A] z 3 d 3
x 17 d wasn't defined before x's definition
=> his name is stored
[B] d 2 => defining d with different values, you obtain
x 16 different results when you evaluate again x
[C] z 2
x 13
[D] b 3 b was defined before x's definition
=> his value (5) is stored in x
x 13 => doesn't matter if you define b in another way
4 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
To make complex tasks, it is possible to use the line() function in the Right Hand Side.
This function and his block of statements applied right after the assignment operator is
called Procedure.
A procedure acts like a logical function (is fully evaluated anytime is called).
line() is available even in the "Programming" toolbox on the right side of the program.
Type an "argument separator" character to add a placeholder in the line.
[2.4] x a 3 <- hold the mouse over the region to see how the procedure
a 9 is stored; as you can see the whole logic is kept in
a memory, no replacements are made
a 7
a <- The last placeholder is used to output
the result
x 1.863065
[2.5] x a 3
a b <- even if it is available in the canvas, "b" is an unknown
inside the procedure (not defined anywhere locally, is
a "local unknown")
[A] b 4
x 7 <- 3+4=7; "b" is used with his canvas' value
a 10 <- the canvas value of "a" is still the same
[B] b 7
x 10 <- 3+7=10; changing the value of b changes even the output
value of x
[C] x x <- store the value of x inside a new variable named x
b 100
x 10 <- the dependancy from b is gone (see [2.4])
5 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Now let's go deeper; we know that a canvas value may be assigned (if available) to a
local unknow when the procedure is being evaluation, but in what point of our function the
canvas value is applied to our unknown when executed?
[A] b 2
x 25 <- (2+3)+20=25
b 2 <- the canvas value of "b" is still the same
[B] b 100
x 123 <- (100+3)+20=123
If line() is no more applied right after the definition operator, becomes a passive
wrapper for his content; anything will be immediatly evaluated
[2.8] x a b 3
<- notice the + before the line(); a parenthesis will do
b 20 the same (or sys(), etc...)
a a b
a <- last placeholder is still the output value
[2.9] x b <- move the cursor above a variable (i.e. a); you should see
a a
c all the occourrences of that item light-grayed
a c
use the escape key to go back to the normal mode (the same
can be addressed with a mouse click)
6 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Programming
__functions
f 2 10
Arguments in programming functions are passed by reference, this means that an input
variable can be modified from inside the function when you use the assignment operator
(:=) to that variable inside the function
IMPORTANT
[3.2] f x x x 1 <- assignment operator applied to an input argument (x:=x+1)
2
out x
out
[A] a 2
[B] a 1
while a 10 Note that the sintax is very compact
b f a
7 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
[C] a 1
while a 10
a f a <- result of the function is also the input variable
a 25 <- Pass by reference side effect overridden by the assignment
on the canvas (because is the last executed)
A variable not defined in the canvas can be set in the canvas from inside the function,
providing this variable is used as function's argument and a value assigned to it
in the function
[3.3] f x , msg 2
a x 10
if a 0
msg "result not allowable"
else
msg "ok"
a
If you expect to use the input variable as target of the calculations inside the function
and you want to avoid side effects, you have to transfer the value to another variable
and use the latest
[3.4] f x v x
v 2
v v v
2
v
a 2
f a 6.7071
Like for simple procedures, on evaluation "local unknowns" can get their value from the
parent level (see [2.5])
8 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
[A] a 2
c 3
[B] c 4
9 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
IMPORTANT
The use of local unknowns (or better "unassigned local variables") might lead you to
unwanted results in some circustances.
[3.7] f x M x g x M 0
11
M x
M 2 x 11
12
M 2 x
M 12
M
[A] M 1 1
[B] M 1 1 1
1
[C] M
1
2 4
f 2 g 2 2 4
1 0
[3.8] f x v1 x 2 y1
v2 x 2 y2
d
r1 v1
d y1
d
r2 v2
d y2
d
r3 x 2 y2
d y2
r1
r2
r3
y2 5 IsDefined y1 0 IsDefined y2 1
v1 stores 3+2*y1, because y1 doesn't exists anywhere;
2
f 3 0 v2 stores 13 (3+2*5), because y2=5 exists on the top level;
2
r1 stores 2 (expected value), because v1 contains the unknown y1;
r2 stores 0, because you are differentiating 13 (a constant);
r3 stores 2 (expected value), because the order of computation;
first of all differentiation is made (calculation of the RHS),
then the assignments looks to replace unknowns from RHS to store
it in the variable LHS variable
This might become a huge problem on big worksheets or if you share the document and who
will reuse your code doesn't realize it (maybe even you, after a long time).
10 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
IMPORTANT
What you can do to avoid this problem?
- The strong way: initialize local variables (M:0, y2:line(y2,1,1), Clear(y1,y2)*, ...);
- A less strong way: use "unique" names (x#, #x, _x, ...);
Most important: always document your functions (right click -> show description);
2 2 2
f1 3 2 f2 3 2 f3 3 2
2 2 2
[3.10] f g x out g 4
out
3 3
test x x x
test 4 64
[3.11] f g 1 out 2 g 3 <- you can define the type of input function by defining the
out number of the arguments required for that function
f test Z 54
test x , y 2 x 3 y 2 x 3 y
test 2 , 3 13
f test X , Y 26
11 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Can I pass a dependent variable as function argument? Yes, you can, providing that the
function's argument is declared as a function with 0 arguments.
[3.13] f x 0 m 5 <- 0-args function required as input; note that here you have
out 2 x to know the names of the indipendent variables behind the
out dependent variable, if you want to define them from inside
the function
k 3 m 3 m
f k 30 <- 2*3*5=30
There is also the possibility to point out dummy arguments (dummy argument = something not
used in the function)
[3.14] f out 2 a <- you may use a variable and then leave it unused, however
out this point out immediatly that everything in the argument
will be ignored in the function (you can use even an unit)
a 2
f "" 4
f <- you still need to add a value to evaluate the function
lastError "Fill in all empty elements."
a 3
f 1 6
f 0 6
Like for variables, it is possible to use functions already defined in the worksheet, or
define locally other functions (nesting)
[3.15] g x out 2 x
out
f x out 3 g x
out
g 4 8
f 4 24
12 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
[3.17] f x out 2 x 1 WARNING There's a know bug related to the control of recursive
if x 10 functions when they falls in infinite loops, that ends
f out in an unrecoverable crash of the worksheet;
check carefully the logic of your function
else
and save the worksheet before the evaluation
out
(disable auto calculation just-in-case)
As for inline functions, you can use vectorize() on your function or inside your function
[3.18]
f x
out ln 1 x 1
out
2 3 4
M
2 3 4
f 2 2.0986
2.0986 2.3863 2.6094
f M
2.0986 2.3863 2.6094
[3.19] f x out ln 1 x 1
out
f 2 2.0986
2.0986 2.3863 2.6094
f M
2.0986 2.3863 2.6094
13 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Absolute
__definitions
This feature send the value to the top of the worksheet, making it available before the
definition. To create an absolute definitions you have to type the TILDE (~) character
before the name.
The value sent at the top of the worksheet will be available at the next recalculation.
-> This features makes available values through worksheet's recalculations.
Pay attention to this, because actions on the worksheet may trigger partial evaluations of
the worksheet, making results different from what you may think.
f 6 15
14 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
Here there are some examples of how something simple can be made in several ways.
Namely we want to know Area, Perimeter and Centroid (refered to the bottom-left corner)
of a rectangle, given his base and height.
15 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
RectangleProperties_1 b , h if b 0 h 0
A b h
P 2 b h
b
2
C
h
2
else
A P C "-" <- chained definition; some alternatives:
C
A "-" A "-" P "-" C "-"
P
P "-"
A ↑NOTE: behavior of definitions
C "-" inside matrices changed since
SMath Studio 0.98
0.05 m
0.025 m
RP RectangleProperties_1 10 cm , 5 cm 0.3 m
2
0.005 m
5 2
c RP cm p RP 30 cm a RP 50 cm
1 2.5 2 3
"-"
RP RectangleProperties_1 10 cm , 5 cm "-"
"-"
16 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
RectangleProperties_2 b , h , out if b 0 h 0
A b h
P 2 b h
b
2
C
h
2
msg "done"
else
A P C "-"
msg "wrong dimensions"
C
out P
A
msg
5 2
c res cm p res 30 cm a res 50 cm
1 2.5 2 3
17 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
RectangleProperties_3 b , h , C , P , A if b 0 h 0
A b h
P 2 b h
b
2
C
h
2
msg "done"
else
A P C "-"
msg "wrong dimensions"
msg
RectangleProperties_3 20 cm , 10 cm , c , p , a "done"
10 2
c cm p 60 cm a 200 cm
5
2
c "-" cm p "-" cm a "-" cm
18 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
RectangleProperties_4 C , P , A if b 0 h 0
A b h
P 2 b h
b
2
C
h
2
msg "done"
else
A P C "-"
msg "wrong dimensions"
msg
b 10 cm
h 20 cm
RectangleProperties_4 c , p , a "done"
5 2
c cm p 60 cm a 200 cm
10
b 10 cm
h 20 cm
2
c "-" cm p "-" cm a "-" cm
19 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
RectangleProperties_5 if b 0 h 0
A b h
P 2 b h
b
2
C
h
2
else
A P C "-"
C
P
A
b 10 cm
h 20 cm
0.05 m
0.1 m
RP RectangleProperties_5"" 0.6 m
2
0.02 m
5 2
c RP cm p RP 60 cm a RP 200 cm
1 10 2 3
b 10 cm
h 20 cm
"-"
RP RectangleProperties_5"" "-"
"-"
20 / 21
8 dic 2016 23:30:20 - User-defined Functions in SMath Studio [rev.8].sm
"b" "h"
<- input database
10 20
20 10
DB
5 40 output databases ↴
40 5
25 25 out1 out2 out3 out4 out5 "C" "P" "A"
for j 2 .. rows DB
b DB
j1
h DB
j2
"example 01"
R1 RectangleProperties_1 b , h
out1 stack out1 , R1 1 R1 2 R1 3
IMPORTANT
"example 02"
RectangleProperties_2 b , h , R2 <- NOTE: functions inside procedures can be
evaluated without assignment/evaluation
out2 stack out2 , R2 1 R2 2 R2 3 operators
"example 03"
RectangleProperties_3 b , h , c3 , p3 , a3
out3 stack out3 , c3 p3 a3
"example 04"
RectangleProperties_4 c4 , p4 , a4
out4 stack out4 , c4 p4 a4
"example 05"
R5 RectangleProperties_50
out5 stack out5 , R5 1 R5 2 R5 3
"b" "h" "C" "P" "A" "C" "P" "A" "C" "P" "A"
10 20 5 5 5
60 200 60 200 60 200
20 10 10 10 10
DB
5 40 10 10 10
40 5 60 200 60 200 60 200
5 5 5
25 25
out1 2.5 out2 2.5 out3 2.5
90 200 90 200 90 200
20 20 20
20 20 20
90 200 90 200 90 200
2.5 2.5 2.5
12.5 12.5 12.5
100 625 100 625 100 625
12.5 12.5 12.5
21 / 21