# Preface

Out of thousand Mathematica commands, this section emphasizes the usage of two the most popular---Simplify and Expand.

# Simplify and Expand

The simplify command finds the simplest form of an equation.
Simplify[expr,assum] does simplification using assumptions.
Expand[expr,patt] leaves unexpanded any parts of expr that are free of the pattern patt.
ExpandAll[expr] expands out all products and integer powers in ant part of exps.
ExpandAll[expr,patt] avoids expanding parts of expr that do not contain terms matching the pattern patt.

One of Mathematica’s most useful features for new users is algebraic manipulation. The program enables the user to avoid tedious exercises in simplification, expansion, and manipulation of algebraic expressions. For example, rather than spending odious amounts of time using the distributive property, Mathematica allows the user to quickly discover that $$(x-1)(x-7)(x+2)(x-4) = x^4 -10\,x^3 + 15\, x^2 + 50\, x -56.$$

D[Integrate[1/(x^3 + 1), x], x] (* integrate the expression *)
Out= 1/(3 (1 + x)) - (-1 + 2 x)/(6 (1 - x + x^2)) + 2/( 3 (1 + 1/3 (-1 + 2 x)^2))
Simplify[%]
Out= 1/(1 + x^3)
Simplify[(x - 1) (x + 1) (x^2 + 1) + 1]
Out= x^4
Simplify[(E^x - E^-x)/Sinh[x]]
Out= 2
Simplify command can be used on equations, and not just expressions.
Simplify[2 x - 4 y + 6 z - 10 == -8]
Out= x + 3 z == 1 + 2 y
Simplify[x^2 + 7 x + 10== (x-1)^3]
Out= x^3 == 11 + 4 4 + 4 x^2
The assumed features can be extremely useful in practical situations. When working with physical situations, a problem contains a number of assumptions (for example the length of the pipe is a positive, finite number). By including an assumption the user gives Mathematica more leeway in finding a simpler answer.
Simplify[1/Sqrt[x] - Sqrt[1/x], x > 0]
Out= 0
Simplify[Abs[2 x + 1 ] + 2 x + x^2 == 3, 2 x +1 > 0]
Out= x (4+x) == 2
Simplify[Sin[n Pi], Element[n, Integers]]
Out= 0
Simplify[x^2 > 3, x > 2]
Out= True
Assuming[x > 0, Simplify[Sqrt[x^2]]]
Out= x
Assuming[x > 0, Simplify[Sqrt[x^2 y^2], Assumptions -> y < 0]]
Out= -Sqrt[x^2] y
FullSimplify[Gamma[x] Gamma[1 - x]]
Out= $Pi] Csc[\[Pi] x] The Simplify command can also be used to substitute one variable for another. This is of enormous usefulness, as seen in the following example: Simplify[((R^2 - (z- (d/2)^2 )^(-3/2)) + ((R^2 + (z + (d/2))^2 ))^2)^(-3/2)), z== 0] Out= $$\frac{16}{( d^2 + 4 R^2 )^{3/2}}$$ Simplify[Out , d == R] Out= $$\frac{16}{5\,\sqrt{5}\, (R^2 )^{3/2}}$$ Example 1: There are a few different ways to evaluate a function at a particular variable in Mathematica. Say we know that $$x = 2y$$ in the following expression. We can substitute this in using the simplify command. Simplify[x^3 + x^6 -3 x^2 y + 12 x^5 y + 3 x y^2 + 60 x^4 y^2 - y^3 + 160 x^3 y^3 + 240 x^2 y^4 + 192 x y^5 + 64 , y = x/2] Out= $$64 + \frac{x^3}{8} + 63\, x^6$$ If you have defined the expression as a function, there is another simple way to evaluate the function at a particular value. We begin by defining the expression as a function. f[x_ , y_ ] = x^3 + x^6 -3 x^2 y + 12 x^5 y + 3 x y^2 + 60 x^4 y^2 - y^3 + 160 x^3 y^3 + 240 x^2 y^4 + 192 x y^5 + 64 We now evaluate the function at the value $$x = 2,\ y = -7$$ as follows: f[2, -7 ] Out= 2 986 713 Mathematica also has a built in command that allows us to perform this evaluation with a bit less trouble: Out /. {x->2, y->-7} Out= 2 986 713 ■ Example: A function f is determined by three dependent variables x, y, and z: \[ f(x,y,z) = x^2 \cos (x+z) + 2 \, \sin^2 (x+yz) + (xz+y)^3 .$

Simplify the expression in the following ways:
a) define a new function g(x,y) such that $$z = 2 ;$$
b) define a third function h(x) such that $$y = 1/(x+1)^2 ;$$
c) what is the value of h(x) when $$x = \pi ?$$

f[x_ , y_ , z_ ] = x^2 Cos[x+z] + 2 (Sin[x+yz])^2 + (x z+y)^3
Out /. z->2
Out= (2 x + y)^3 + x^2 Cos [2+x] + 2 Sin[x + 2 y]^2
Out /. y -> (x+1)^-2
Out= (2 x + 1/ (x+1)^2)^3 + x^2 Cos [2+x] + 2 Sin[x + 2/(x+1)^2]^2
Out /. x -> Pi
Out= (2 Pi + y)^3 + Pi^2 Cos [2+Pi] + 2 Sin[Pi + 2 /(1+Pi)^2]^2
N[Out ]
Out= 255.046 + 9.8696 Cos [5.14159] + 2 Sin[Pi + 2 /(1+Pi)^2]^2

■

The Mathematica simplify command can also be used to check the veracity of a mathematical statement. For example, if we want to confirm that x=6 is a solution to the equation $$x^3-3x^2-16x=12$$ the following Mathematica command accomplishes this.

Simplify[x^3 - 3 x^2 - 16 x == 12, x == 6]
Out= True

The Expand command expands a function or an equation into a given power series. The expand command does not work across both sides of an equation, whereas the simplify command does.

Expand[(1 + x + y) (2 - x)^3]
Out= 8 - 4 x - 6 x^2 + 5 x^3 - x^4 + 8 y - 12 x y + 6 x^2 y - x^3 y
ExpandAll[Sqrt[(1 + x)^2]]
Out= Sqrt[1 + 2 x + x^2]
Expand[Sin[x + y], Trig -> True]
Out= Cos[y] Sin[x] + Cos[x] Sin[y]
We expand the power expression $$(1+x)^{10}$$ into a regular polynomial using the following command:
Expand[(1 + x)^10]
Out= 11 + 10 x + 45 x^2 + 120 x^3 + 210 x^4 + 252 x^5 + 210 x^6 + 120 x^7 + 45 x^8 + 10 x^9 + x^10

However, if you want to see only odd coefficients of this expansion, use the following option:
Expand[(1 + x)^10, Modulus -> 2]
Out= 1 + x^2 + x^8 + x^10

The Expand command can conveniently be used to only expand certain parts of an expression. For example, in the following expression, suppose we don’t want to break up the polynomials. We only expand the (1-sin (x)) portion.

Expand[(1 - Sin[x]) (x+1)^2 + (1 - Sin[x]) (x+6)^2 , (1 - Sin[x]) ]
Out= (1 + x)^2 + (6+x)^2 - (1+x)^2 Sin[x] - (6+x)^2 Sin[x]

The Expand command together with Trig -> True will reshuffle trig expressions.
Expand[ Sin[x+y] + Cos[x] Sin[y] , Trig -> True]
Out= Cos[y] Sin[x] + 2 Cos[x] Sin[y]

Expand[ Sin[x+y] + Cos[x] Sin[y] , Trig -> False ]
Out= Cos[x] Sin[y] + Sin[x+y]

Or one can use just one command:
TrigExpand[Sin[x + y]]
Cos[y] Sin[x] + Cos[x] Sin[y]