# DMelt:JMathlab/8 Symbolic Transformations

# Symbolic Transformations

## Substitution

Parts of an expression may be replaced by other expressions using subst(a,b,c): a is substituted for b in c. This is a powerful function with many uses.

First, it may be used to insert numbers for variables, in the example $3$ for $x$ in der formula $2\sqrt{x}\cdot e^{-x^2}$.

```
>> syms x
>> a=2*sqrt(x)*exp(-x^2);
>> subst(3,x,a)
ans = 4.275E-4
```

Second, one can replace a symbolic variable by a complex term. The expression is automatically updated to the canonical format. In the following example $z^3+2$ is inserted for $x$ in $x^3+2x^2+x+7$.

```
>> syms x,z
>> p=x^3+2*x^2+x+7;
>> subst(z^3+2,x,p)
ans = z^9+8*z^6+21*z^3+25
```

Finally, the term b itself may be a complex expression (in the example $z^2+1$). Jasymca then tries to identify this expression in c (example: $\frac{z\cdot x^3}{\sqrt{z^2+1}}$). This is accomplished by solving the equation $a = b$ for the symbolic variable in b (example: $z$), and inserting the solution in c. This does not always succeed, or there may be several solutions, which are returned as a vector.

```
>> syms x,y,z
>> c=x^3*z/sqrt(z^2+1);
>> d=subst(y,z^2+1,c)
d = [ x^3*sqrt(y-1)/sqrt(sqrt(y-1)^2+1)
-x^3*sqrt(y-1)/sqrt(sqrt(y-1)^2+1) ]
>> d=trigrat(d)
d = [ x^3*sqrt(y-1)/sqrt(y)
-x^3*sqrt(y-1)/sqrt(y) ]
```

## Simplifying and Collecting Expressions

The function trigrat(expression) applies a series of algorithms to expression.

** All numbers are transformed to exact format.**
** Trigonometric functions are expanded to complex exponentials.**
** Addition theorems for the exponentials are applied.**
** Square roots are calculated and collected.**
** Complex exponentials are backtransformed to trigonometric functions.**

It is often required to apply float(expression) to the final result.

```
>> syms x
>> trigrat(sin(x)^2+cos(x)^2)
ans = 1
>> b=sin(x)^2+sin(x+2*pi/3)^2+sin(x+4*pi/3)^2;
>> trigrat(b)
ans = 3/2
>> trigrat(i/2*log(x+i*pi))
ans = 1/4*i*log(x^2+pi^2)+(1/2*atan(x/pi)-1/4*pi)
>> trigrat(sin((x+y)/2)*cos((x-y)/2))
ans = 1/2*sin(y)+1/2*sin(x)
>> trigrat(sqrt(4*y^2+4*x*y-4*y+x^2-2*x+1))
ans = y+(1/2*x-1/2)
```

trigexpand(expression) expands trigonometric expressions to complex exponentials. It is the first step of the function trigrat above.

```
>> syms x
>> trigexp(i*tan(i*x))
ans = (-exp(2*x)+1)/(exp(2*x)+1)
>> trigexp(atan(1-x^2))
ans = -1/2*i*log((-x^2+(1-1*i))/(x^2+(-1-1*i)))
```

More information on this topic is in DMelt books |