## Symbolic Toolkit

### Overview

Starting with version 2.0, Calcugator has a symbolic toolkit that provides simplification capabilities and symbolic differentiation.

The following basic functions have been implemented:

• `simplify(f)` simplifies function `f`.
• `compose(f)` expands all function compositions in the definition of function `f`.
• `partial(f,s)` returns the partial derivative of function `f` with respect to the argument name in variable `s`.
• `derivative(f)` returns the derivative of function `f`.
• `totalderivative(f,s)` returns the total derivative of function `f` with respect to the argument name in variable `s`.

The following utility functions have been implemented:

• `gradient(f)` returns the gradient of function `f` in Cartesian coordinates. Function `f` maps `Rn` into `R`.
• `jacobian(f)` returns the Jacobian of function `f` in Cartesian coordinates. Function `f` maps `Rn` into `Rm`.
• `divergence(f)` returns the divergence of function `f` in Cartesian coordinates. Function `f` maps `Rn` into `Rn`.
• `curl(f)` returns the curl (rotational) of function `f` in Cartesian coordinates. Function `f` maps `R3` into `R3`.
• `laplacian(f)` returns the Laplacian of function `f` in Cartesian coordinates. Function `f` maps `R3` into `R`.
• `taylor(f,a,n)` returns the Taylor expansion of function `f` about point `a` using up to `n` derivatives.

Also, functions `isDifferentiable`, `numberOfArguments` and `argumentName` are useful in the creation of programs that work with functions.

• `isDifferentiable(f)` returns `true` if function `f` is differentiable.
• `argumentName(f,i)` returns the name of the `i-th` argument of function `f`.
• `numberOfArguments(f)` returns the number of arguments of function `f`.

### Simplification

Function `simplify(f)` simplifies the definition of function `f` but leaves explicit function evaluations intact. For instance, assume you have these two functions defined:

g(x)=x*sin(x)
g(x)=x*sin(x)
f(x)=x+4*x + g(x)*x
f(x)=x+4*x + g(x)*x

Simplifying function `f` we obtain:

simplify(f)
f(x)=g(x)*x + 5*x

Notice that the simplification of function `f` left the explicit evaluations of function `g` intact. If you need to expand the definition of function `g`, use function `compose` as follows:

compose(f)
f(x)=sin(x)*x^2+5*x

### Differentiable functions

The Calcugator language permits the creation of user-defined functions or programs. Function `isDifferentiable(f)` returns `true` only if the following conditions are met:

• Function `f` is differentiable in the sense used in standard Calculus.
• The definition of function `f` is done in a single Calcugator statement without using curly brackets.

For example, function `f below is not differentiable:`

``` f(x)={y=x; y=x*x; return y;} f(x)={y=x; y=x*x; return y;} isDifferentiable(f) false The following function is differentiable: f(x)=x*exp(sin(x-x^2))+x f(x)=x*exp(sin(x-x^2))+x isDifferentiable(f) true ```
``` Partial derivativesPartial derivatives of differentiable functions can be obtained using function partial. Function partial(f,s) requires that the call to isDifferentiable(f) returns true. Variable s must be a string with the name of the argument for which the partial derivative is being computed. f(x,y)=x*sin(y) f(x,y)=x*sin(y) f_y = partial(f,"y") f_y(x,y) = cos(y)*x The computed partial derivative is a full function object. You can evaluate it, plot it, etc. f_y(2,pi/4) 1.4142 plot3D(f_y, -2, 2, 25, -2, 2, 25, "wireframe") Figure 1. Plot of function f_y. The above partial could also be computed as follows: s="y" "y" f_y = partial(f,s) f_y(x,y) = cos(y)*x f_y(2,pi/4) 1.4142 You could also use function argumentName: f_y = partial(f,argumentName(f,2)) f_y(x,y) = cos(y)*x f_y(2,pi/4) 1.4142 If function f does not depend on a given argument name, function partial returns the constant function zero. f_z = partial(f,"z") f_z(x,y) = 0 f_z(2,pi/4) 0 The derivative functionThe derivative function returns the derivative of function f with respect to its unique argument. Function f must be a function of a single argument. The statement derivative(f) is equivalent to the statement partial(f,argumentName(f,1)). If function f has more than one argument, the call to derivative(f) will return an error. f(x) = x*cos(x) f(x) = x*cos(x) f' = derivative(f) f'(x) = cos(x)-sin(x)*x f'(pi/2) -1.5708 Notice that the single quote character (') is a valid Calcugator identifier. You can define variables and function names using single quotes: a''=3 3 a''*4 12 f'' = derivative(f')  f''(x) = -cos(x)*x-2*sin(x) f''(2)  -0.9863 The totalderivative functionIn standard Calculus, given a function f=f(x,y,t), the total derivative of f with respect to t is defined as follows: Defining x' as the total derivative is a function of x, y, x', y' and t. Given a function f=f(x, y, t), Calcugator can compute the total derivative of f using the function totalderivative. f(x,y,t)=x*sin(y)+cos(2*t) f(x,y,t)=x*sin(y)+cos(2*t) F = totalderivative(f,"t") F(x, y, y', x', t) = cos(y)*x*y'-2*sin(2*t)+sin(y)*x' Notice that Calcugator creates the new arguments x' and y'. If the function f already has an argument say z', Calcugator assumes it is the derivative with respect to the given argument. For example, let's assume the position vector of particle is given by the vector expression p below: p(a,t)=(cos(a) + t^2, sin(a) + t^2) p(a,t)=(cos(a) + t^2, sin(a) + t^2) The velocity vector is the total derivative of the position with respect to time: v=totalderivative(p,"t") v(a, a', t) = ( -a'*sin(a)+2*t, a'*cos(a)+2*t ) The acceleration vector is the total derivative of the velocity with respect to time: ac=totalderivative(p,"t") ac(a, a', a'', t) = ( 2-a''*sin(a)-a'^2*cos(a), 2+a''*cos(a)-a'^2*sin(a) ) Other utilitiesThe following utilities have been implemented: The gradient functionFunction gradient(f) returns the gradient of function f in Cartesian coordinates. Function f maps Rn into R. In standard Calculus the gradient of a function f(x,y,z) is defined as follows: Example: f(x,y,z)=x*sin(y)+5*z f(x,y,z)=x*sin(y)+5*z Nf = gradient(f) Nf(x, y, z) = ( sin(y), cos(y)*x, 5 ) The jacobian functionFunction jacobian(f) returns the Jacobian of function f in Cartesian coordinates. Function f maps Rn into Rm. In standard Calculus the Jacobian of a function f(x,y,z)=(fx(x,y,z), fy(x,y,z), fz(x,y,z)) is defined as follows: Example: f(x,y,z)=(x, y*cos(x), x*z) f(x,y,z)=(x, y*cos(x), x*z) Jf = jacobian(f) Jf(x, y, z) = ( 1, 0, 0 ; -sin(x)*y, cos(x), 0 ; z, 0, x ) The divergence functionFunction divergence(f) returns the divergence of function f in Cartesian coordinates. Function f maps Rn into Rn. In standard Calculus the divergence of a function f(x,y,z)=(fx(x,y,z), fy(x,y,z), fz(x,y,z)) is defined as follows: Example: f(x,y,z)=(x, y*cos(x), x*z) f(x,y,z)=(x, y*cos(x), x*z) df = divergence(f) df(x, y, z) = 1+cos(x)+x The curl functionFunction curl(f) returns the curl (rotational) of function f in Cartesian coordinates. Function f maps R3 into R3. In standard Calculus the curl of a function f(x,y,z)=(fx(x,y,z), fy(x,y,z), fz(x,y,z)) is defined as follows: Example: f(x,y,z)=(x, y*cos(x), x*z) f(x,y,z)=(x, y*cos(x), x*z) Cf = curl(f) Cf(x, y, z) = ( 0, -z, -sin(x)*y ) The laplacian functionFunction laplacian(f) returns the Laplacian of function f in Cartesian coordinates. Function f maps R3 into R. In standard Calculus the laplacian of a function f(x,y,z) is defined as follows: Example: f(x,y,z)=x^2*y^3*cos(z) f(x,y,z)=x^2*y^3*cos(z) Lf = laplacian(f) Lf(x, y, z) = 6*cos(z)*x^2*y-cos(z)*x^2*y^3+2*cos(z)*y^3 The taylor functionFunction taylor(f,a,n) returns the Taylor expansion of function f about point a using up to n derivatives. Function taylor only accepts functions of a single argument. In standard Calculus the Taylor expansion of a function f(x) at point a is defined as follows: The third argument to function taylor specifies the highest derivative to be used in the expansion. Example: f(x)=1/(1-x) f(x)=1/(1-x) Tf = taylor(f,0,9) Tf(x) = 1+x+x^2+x^3+x^4+x^5+x^6+x^7+x^8 ```