# Transforming an expression into a function: unapply

### 5.15.2 Transforming an expression into a function: unapply

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.

- Input:
  unapply(exp(x+2),x)
  Output:

  x↦ ex+2

- Input:
  unapply(x*y-x-y,(x,y))
  Output:

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

Warning.
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:
Example.
Input:

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

Output:

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.

- Input:
  f:=unapply(lagrange([1,2,3],[4,8,12]),x)
  (See Section 5.27.29.) Output:

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

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

  x↦ x lnx−x+1

- Input:

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

  Output:

  x lnx−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.

Input:

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

Output:

x↦ 2 x+3