### 8.2.2 Transforming an expression into a function

The unapply command transforms an expression into a function.

- unapply takes two arguments:
  - expr, an expression.
  - x, the name of a variable or sequence of names of variables.
- unapply(expr,x) returns the function defined by the expression expr and variable(s) x, as in x->expr.

#### Examples

unapply(exp(x+2),x)

x↦ex+2 |  |  |  |  |  |  |  |  |  |

unapply(x*y-x-y,(x,y))

| ⎛ ⎝ | x,y | ⎞ ⎠ | ↦x y−x−y

##### Remark.

When a function being is defined, the right side of the assignment is not evaluated, hence g:=sin(x+1); f(x):=g does not define the function f: x → sin(x+1) but defines the function f: x → g. To define the former function, unapply should be used, as in the following example:

g:=sin(x+1); f:=unapply(g,x)

sin | ⎛ ⎝ | x+1 | ⎞ ⎠ | ,x↦sin | ⎛ ⎝ | x+1 | ⎞ ⎠

Hence, the variable g is assigned to a symbolic expression and the variable f is assigned to a function.

#### Examples

f:=unapply(lagrange([1,2,3],[4,8,12]),x)

(See Section 17.1.1.)

x↦ 4 | ⎛ ⎝ | x−1 | ⎞ ⎠ | +4

f:=unapply(integrate(log(t),t,1,x),x)

x↦x ln x−x+1 |  |  |  |  |  |  |  |  |  |

f:=unapply(integrate(log(t),t,1,x),x):; f(x)

x ln x−x+1 |  |  |  |  |  |  |  |  |  |

##### Remark.

Suppose that f is a function of 2 variables f:(x,w)→ f(x,w), and that g is the function defined by g: w → hw, where hw is the function defined by hw(x)=f(x,w).

unapply can also be used to define g.

#### Example

f(x,w):=2*x+w:; g(w):=unapply(f(x,w),x):; g(3)

x↦ 2 x+3 |  |  |  |  |  |  |  |  |  |