If expr is an expression possibly involving a variable
x, you can use it to define a function f either by
f(x):=expr
or f := x->expr
(see Section 5.5.1).
Warning!!!
The expression after -> is not evaluated. You should use
unapply (see Section 6.15.2) if you expect the second
member to be evaluated before the function is defined.
Example.
To define f:(x)→ x*sin(x),
Input:
or:
then:
Output:
|
You can similarly define a function of several variables, by replacing x by a sequence (x1,…,xp) or a list [x1,…,xp] of variables.
Example.
Input:
or:
then:
Output:
1 |
You can also define a function with values in ℝq by replacing expr by a sequence (expr1,…,exprq) or list [expr1,…,exprq] of expressions.
Examples.
√ |
| , | √ |
|
⎡ ⎢ ⎣ | √ |
| , | √ |
| ⎤ ⎥ ⎦ |
Suppose that the function f: (x,y) → f(x,y) is defined, and you want to define a family of functions g(t) such that g(t)(y):=f(t,y) (i.e. t is viewed as a parameter). Since the expression after -> (or :=) is not evaluated, you should not define g(t) by g(t):=y->f(t,y); you have to use the unapply command (see Section 6.15.2).
For example, to define f:(x,y)→ xsin(y) and
g(t):y→ f(t,y):
Input:
then:
Output:
y↦ 2 siny |
Input:
Output:
2 sin | ⎛ ⎝ | 1 | ⎞ ⎠ |
For another example, suppose that you want to define the function h:
(x,y) → [x*cos(y),x*sin(y)] and then you want to define
the family of functions k(t) having t as parameter such that
k(t)(y):=h(t,y). To define the function h(x,y)
Input:
To define properly the function k(t): Input:
then:
Output:
x↦ | ⎛ ⎝ | x cos | ⎛ ⎝ | 2 | ⎞ ⎠ | ,x sin | ⎛ ⎝ | 2 | ⎞ ⎠ | ⎞ ⎠ |
Input:
Output:
cos | ⎛ ⎝ | 2 | ⎞ ⎠ | ,sin | ⎛ ⎝ | 2 | ⎞ ⎠ |