Symbolic Computation

Algebra

Algebraic operations

Expand
	Factor
	Together
	Apart
	Cancel
	Simplify

Expand[(x+y)^2 + 7 (3 + x) ( x + y)]

The  symbol % refers the result from the immediately previous execution, not necessarily the result from the preceding cell.

Factor[%]

(This is the prefix form of a function.)

Expand[Sqrt[a^2 t^2]]

No assumption is made about the sign of a or t so the root is not taken.

PowerExpand[%]

Note that if a < 0 or t < 0 (but not both), this answer may not be what we want.

Together[ 1/(x + 1) - 2 x / (1 - x)^2]

Apart[%]

Simplify[%]

Factor[%]

Algebra with a Palette

These operations can be carried out by using a palette to apply transformations in an output cell.   From the menu choose File/AlgebraicManipulation.  Next, evaluate the cell below.

(x + y)^2  +  7 (3  +  x) ( x  +  y)

Place the cursor somewhere in the output cell inside the last term and click the mouse repeatedly until this part of the output is highlighted.  The choose Expand from the palette. A new cell will appear but it will be an Input cell!  In it you will see the last term expanded.  Place the cursor somewhere in the first term in this same cell and click until it is highlighed.  Choose Expand again. Note that the last output cell changes but a new one is not produced.  Click anywhrere in this expression until it is all highlighted.  Now choose Factor.

(x + y) (21 + 8 x + y)

Vectors and Matrices

Vector

A vector can be represented as list.

u = {u1,u2,u3};

Matrix

A matrix is a list with two levels, the second level being lists of equal length.

m = {{1,2,3},{4,5,6},{7,8,9}};

The function Dimensions gives the number of columns and rows of a matrix.

Dimensions[m]

We can display a matrix in the usual way with MatrixForm.

MatrixForm[m]

But never make an assignment of this form

n  =  MatrixForm[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}];

because n will not be a matrix; the head will be FullForm instead of List.

FullForm[n]

Assign the matrix to n before you apply MatrixForm to display it.

(n = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}) // MatrixForm

Tensor

A tensor is a list with three or more levels.

(t={{{0.1, 0.2},{0.3,0.4}},{{0.5, 0.6},{0.7,0.8}},
     {{0.9, 1.},{1.1, 1.2}},{{1.3,1.4},{1.5,1.6}}})//MatrixForm

Dimensions[t]

Vector and matrix operations

u.v			dot product Dot[u,v]
Transpose[m] 		transpose of matrix m
Inverse[m]     		inverse of a matrix
Det[m]                	determinant of a matrix

The scalar or dot product of two lists is a contraction of the last index of the first list with first index of the second list.  By "contraction of indices" is meant setting two indices with equal ranges equal to each other and then summing over the full range.  The dot product of two multidimensional lists is defined as follows:
		u . v      ==          (∑_j) (Subscript[u, ij]) (Subscript[v, jklmn])
	The Dot product of Mathematica follows this convention of tensor analysis and does not distinguish between row vectors and column vectors.  Here is the dot product of two 3-vectors.

Clear[a,b,c,d,e,f];
{a,b,c}.{d,e,f}

m={{a,b},{c,d}};

m.{e,f}

{e,f}.m

Equations

You have seen that  x = expr actually causes x to be replaced by expr in any expression that Mathematica evaluates; it is an assignment.  But in mathematics an equation  x = expr  is really a relation that  is sometimes used to replace the quantity on the left hand side, and sometimes solved for a symbol appearing on the right hand side.  In Mathematica we distinguish between an assignment and an equation very explicitly.

Expression for an equation

In Mathematica the symbol used to express an equation is the logical symbol  (==). 
         l.h.s. == r.h.s.,
If both sides of an equation can be evaluated, Mathematica will return the result  True or False.

2+2==5

Exercises

1. Evaluate the following equation for n = 1, 2, ..., 20 to determine the value of the machine epsilon on your computer.  (Hint: Use a list.)

1 == 1 + 1.0 (10^(-n))

2. Evaluate $MachinePrecision to check your result.

Solving algebraic equations

Solve[eqn, symb]	 		list of rules for 							assignment of solution
Solve[{eq1, eq2,...}, {var1, var2, ...}]   	 list of rules for 						assignment of solution

Single equation

Many equations can be solved by using Solve.  Use a symbol to store the solution so that you can work with it.

Clear[eq, soln, x, a, b, c];
eq1 =  a (x^2) + b x  + c == 0;
sol1 = Solve[eq1, x]

Mathematica has returned two rules for assigning a solution of the equation to x.  But no value has been assigned to x.  In fact, as already explained you should avoid making assignments to symbols that are being used as variables.  Assign one of the solutions to a different symbol by choosing to use one of these rules with ReplaceAll.

xs = x /. sol1[[2]]

You can always verify that xs satisfies the equation by using substitution.

eq1 /. x -> xs

It is necessary to Simplify the left side before Mathematica can verify that xs is a solution.   Use the AlgebraicManipulation  palette by placing the cursor into this output cell on the left side and repeatedly clicking until it is completely highlighted.  Then choose Simplify from the palette.  An Input cell will appear as you do this and the operations done with the palette will all be done in this same cell.  You will see the following result, showing that xs is a solution to the equation.

0 == 0

Mathematica always returns a list of lists as the solution to an equation, even when there is only one solution.

sol2 = Solve[a x  +  b  == c, x]

So you must index into the list of rules, even though there is only one.  If you do not, you assign a list to xs instead of an algebraic expression.

xs = x /. sol2

In the case of one solution you can also use First to pull out the first, and only, rule.

xs = x /. First[sol2]

Multiple equations

If you have more than one equation to solve, you just include them in a list and specify the unknowns in a second list.

Clear[x, y, a, b];
eq3 = {(x^2) - y == a,  x/y  ==  b};
sol3 = Solve[eq3, {x, y}]

There are two solutions for x and y.   If you want to use the second solution, you can define symbols xs and ys that satisfy the equation in one step.

{xs,ys}={x,y}/.sol3[[2]]

Verify the solution.

eq3 /. {x -> xs, y -> ys} // Simplify

Elimination

It is useful to be able to eliminate variables from simultaneous equations. There are two operations for doing this.

Eliminate[{eq1,...},{var1,...}]		eliminates variables
Reduce[{eq1,...},{var1,...}]		logical expressions

Let's try this on the eq3 that was solved in the last section.

Clear[x, y, a, b];
eq4 = Eliminate[eq3, y]

Verify that the solutions for x found above come from solutions of this equation.

Solve[eq4, x]

Reduce eliminates symbols from a set of equations but returns the result as a rigorous set of logical equations.  Apply this symbol to eq3, but enter eq3 explicitly so that it an be compared with the solution.

Reduce[{(x^2) - y == a, (x/y) == b}, y]

Refer to the relational and logical operators to interpret this output.

Use the symbols in this table to read the result returned by Reduce.  The symbol && stands for AND and the symbol || stands for OR.  The different solutions are separated by OR.  So, the first set of conditions for a solution are 
		a≠0&&y==-a&&b==0&&x==0
which can be translated as 
"a not equal to zero AND y equal to -a AND b equal to zero AND x equal to zero".

Exercise

Use Eliminate to find the trajectory of a projectile in a vacuum by eliminating the time from the equations  (Remember to assign each equation to a symbol for ease in manipulation and to clear each symbol of any previous values.)
       Clear[x,x0, v0,theta, t, y, y0, v0, t, g];
       xeq = x == v0 Cos[theta] t ;
       yeq = y == v0 Sin[theta]  t  - (1/2) g (t^2);
Proceed as follows.
          1. Eliminate the time from the equations, calling the result eqxy
          2. Use soln =Solve[ eqxy , y ] to find a rule for defining y as a function of x
          3. Use ysol = y /.  soln[[1]] // ExpandAll // Cancel 
               to get a reasonably simple expression for y

Calculus

D[expr,var]				partial derivative
Dt[expr,var]				total differential
Limit[expr,var-> limit]			limit
Integrate[expr,var]			indefinite integral
Integrate[expr,{var, start, end]	definite integral
Sum[expr,iterator]			sum over some variable
Product[expr,iterator]			product over some variable

Exercises

1.  Use this assignment.
       f = x^2 - 6 a x^3 + 8 b x^4 + 10;
 Then evaluate each of the following expressions.
 	D [f,x]
 	Integrate[f,x]
 	Integrate[f,{x,0,1}]
 	Integrate[f,x,x]
 	Dt[f,x]
 	Dt[ x^2 - 6 a x^3 + 8 b x^4, x, Constants->{a,b}]
 	Limit[Sin[x]/x, x->0]
 	Sum[1/(x^3), {x ,  1,  10,  0.5}]

2.  The period P of a pendulum of length L is repeatedly measured to determine a value for g.  The expression for g is
	g = 4 (π^2) L/(P^2).
Apply the total derivative Dt to g and use it to relate the uncertainty in g to the uncertainty in measured values for L and P.

Differential equations

?DSolve

DSolve[eqn, y[x], x] solves a differential equation for the
   functions y[x], with independent variable x. DSolve[{eqn1,
   eqn2, ...}, {y1[x1, ...], ...}, {x1, ...}] solves a list of
   differential equations.

Use a symbol to store the solution.

Clear[y,x,sol];
sol=DSolve[y'[x] == - y[x], y[x] , x]

The result is a rule for replacing y.  Use this rule to define the solution to the equation.  We do not want the result presented as a list with one element. So index into  when assigning the solution to a function.  Also, the symbol C[1] is built into Mathematica.  Substitution is needed to set it.  Here is t is a good idea to use Set for the assignment so that the right side need only be evaluated this time.

ys[x_] = y[x] /. sol[[1]] /. C[1] -> y0

Here is a second order ODE.

Clear[x,t];
sol=DSolve[x''[t] == - x[t], x[t], t]

When initial conditions are given, Mathematica uses them to determine the constants.

sol=DSolve[{x''[t] == - x[t], x[0] == 2, x'[0] ==0},
        x[t],t]