### 8.3.3 Defining algebraic functions

##### Defining a function from ℝp to ℝq.

If expr is an expression possibly involving a variable x, use it to define a function f either by

f(x):=expr

or

f:=x->expr

(see Section 3.4.1). Note that the expression after -> is not evaluated. You should use unapply (see Section 8.2.2) if you expect the second member to be evaluated before the function is defined.

#### Example

To define f(x)=xsin(x), input:

f(x):=x*sin(x)

or:

f:=x->x*sin(x)

Then:

f(pi/4)

π | √ |
2
8

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

To define f(x)=xsin(y), input:

f(x,y):=x*sin(y)

or:

f:=(x,y)->x*sin(y)

Then:

f(2,pi/6)

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

Define the function h(x,y)=(xcos(y),xsin(y)).

h(x,y):=(x*cos(y),x*sin(y))

Then:

h(2,pi/4)

| √ |
2
| , | √ |
2

Define the function h(x,y)=[xcos(y),xsin(y)].

h(x,y):=[x*cos(y),x*sin(y)];

or:

h:=(x,y)->[x*cos(y),x*sin(y)];

or:

h(x,y):={ [x*cos(y),x*sin(y)] };

or:

h:=(x,y)->return [x*cos(y),x*sin(y)];

or:

h(x,y):={ return [x*cos(y),x*sin(y)]; }

Then:

h(2,pi/4)

| ⎡ ⎢ ⎣ | √ |
2
| , | √ |
2
| ⎤ ⎥ ⎦

##### Defining families of function from ℝp−1 to ℝq using a function from ℝp to ℝq.

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 8.2.2).

For example, to define f:(x,y)→ xsin(y) and g(t):y→ f(t,y):

f(x,y):=x*sin(y); g(t):=unapply(f(t,y),y)

then:

g(2)

y↦ 2 sin y |  |  |  |  |  |  |  |  |  |

As another example, suppose that you want to define the function h: (x,y) → [xcos(y),xsin(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):

h(x,y):=(x*cos(y),x*sin(y))

To define properly the function k(t):

k(t):=unapply(h(x,t),x)

then:

k(2)

x↦ | ⎛ ⎝ | x cos | ⎛ ⎝ | 2 | ⎞ ⎠ | ,x sin | ⎛ ⎝ | 2 | ⎞ ⎠ | ⎞ ⎠