Basic Linear Algebra with Mathematica
Version 0.97
©1999 by Gerald Teschl  <gerald@mat.univie.ac.at>, <http://www.mat.univie.ac.at/~gerald/>
©2000, ©2002 by Roland Steinbauer <roland.steinbauer@univie.ac.at>, <http://www.mat.univie.ac.at/~stein/>

This Mathematica notebook provides a brief introduction into doing Linear Algebra with the computer algebra system Mathematica. It introduces basic conceps like vectors,matrices and systems of linear equations.It was used in a first semester university course.

Basics: Input, calculations and variables

Mathematica is a powerful tool for doing mathematics. It can handle both symbolic and numeric expressions. You can use it just like a calculator: you type in questions, and Mathematica prints back answers.

27 + 7

The file you are reading is a so-called Mathematica notebook. It contains text as well as mathematical input and output. To actually do calculations you have to go to the input line and mark it active by moving the mouse to it and clicking the left button. Go and try to do so with the line reading "27+5" above. You should see the cursor blinking in this very line. Next you have to enter the input to the Mathematica kernel (the part of Mathematica that actually does the calculations for you) by pressing either SHIFT+ENTER or the ENTER key at the right hand side of the keyboard. Now go and try to evaluate the input line above. Do you agree with the anser? Feel free to change the input and evaluate it!

To go on we introduce powers, multiplication and division. A ^ marks powers, a space between two expressions is interpreted as multiplication (you can, however also use a *) and division is marked by a slash /. Go and evaluate the expressions below.

27 + 5^3

4 12

3*13

6/3

A Mathematica notebook is divided into so-called cells which are marked by the blue square brackets to the right. As you might have guessed by looking to the right border cells can be grouped together to structure a notebook. The most important cell types are input cells, output cells and text cells.
The cell you are reading right now is a text cell. New cells are input cells by default. To create a new cell just move the mouse between two cells already present: the courser becomes a horizontal bar. Next click the left button of your mouse: a horzontal line appears. Now you just can enter new input.  Alternatively you can just move through the notebook using the arrow-keys. A horizontal line will appear whenever you cross the border of a cell allowing you to enter new input. 
Try to do some more basic calcualtions below this cell!

By now your should have entered something like "2/3" to Mathematica. Were you surprised by the answer?  Mathematica tries to be as precise as possible. To get numerical results use:

2/3

N[%]

As you might have guessed % is substituted by the last output. To get back the last but one output use %%. 
To reach even higher precision you can tell Mathematica how many digits you like.

N[%%, 20]

You can also use variables or put several commands into one cell. To do so just hit the ENTER button

x = 5
27  + (x^3)

If you wonder how I could input the power as a superscript, go to the File menu and select Palettes->BasicInput. If you put a semicolon at the end of an input sequence the respective output is suppressed. So in real life you might want to use the following.

x = 5;
27  + x^3

You might have noticed that Mathematica assigns  numbers to every command you input and to the corresponding  output. This makes it more comfortable to use the output of the previuos calculations. Moreover all results are stored and you can use them in any following calculations as long as you do not quit Mathematica (more precisely the kernel).

Out[8] 3

Can you explain this result? Note that Out[8] was suppressed and remember that a space between two expressions is interpreted as multiplication. Be warned that if you omit this space, you will get unexpected results!

xy  +  x y

Mathematica also knows a lot of basic constants and operations.

Pi

To get numerical values you can use the N-operator as above or the following.

N[Pi]
π // N
E // N

Note that the Euler number is referred to as E in Mathematica, i.e., using the capital letter. It is a general rule in Mathematica to label all operators by a fist capital letter.

To avoid complications in the following we erase the assignments done in this section. To this end we use the Clear operator.

x
Clear[x]
x

You can also use the following syntax.

x = 7
x
x =. 
x

The output provided by Mathematica comes in one or more output cells. In and output cells are grouped together by default. What happens if you double click the bracket joining an input and an output cell?

Solving Equations

Note that from now on the input cells have not been evaluated before ---you have to do so by yourself! 
Mathematica can solve various kinds of equations. To do so we use the Equal operator (==) and the Solve command, e.g:

Solve[x^2 + 2 a x + b == 0, x]

So the syntax of the Solve operator is the following: Solve[equation,variable], where equation is an expression of the form left hand side==right hand side  and variable stands for the variable we are solving for. 
Go on and solve the equation y^2-7y+5=0!

Now we are using a slightly more elegant notation to solve the first equation above.

eqn := x^2 + 2 a x +  b == 0
sol = Solve[eqn, x]

Note that the Set operator (=) has lower precedence than the Equal operator (==).
Mathematica gives the result as a so-called list of rules. To verify this solution we can substitute the solution back into the equation. This is done using the following syntax (the list of rules, i.e., {{x-> -a+(Sqrt[a^2 - b])}, {x-> -a-(Sqrt[a^2 - b])}} is substituted into the expression eqn, that is (x^2)+ax+b).

eqn  /. sol

This is not exactly what we wanted to do. To get the right thing we have to simplify the above expression. This is done using the Simplify command.

Simplify[%]

This answer is now more satisfying!
In summary we have:

To get more information on any Mathematica command you can use the Online-Help. Just click on the Help menue at the upper right corner of the Mathematica window. Do so now and choose the item called Help Browser and enter "Solve" into the line beginning with "Go to:" to find out the details of the Solve command. At the very bottom of the respective page you can find a lot of useful examples! Try to find out how to solve a system of two equtions in two varaibles!  
Try to solve x+y=a, x-y=0 for y and y. Check that your solution is correct!

If you couldn't find out the answer by yourself; well here's one more hint:

To avoid complicatins in what follows we have to enter:

Clear[eqn, sol]

Note that in case you encounter unexpected results this might come from using definitions of certain variables you have long forgotten. The best way to proceed in such a situation is to quit the Mathematcia kernel. This is done using the menu Kernel; Quit kernel; Local.

Lists, Vectors, Matrices

Mathematica stores vectors and matrices als Lists. We have already encontered a list above in the context of the solutions to an equation. Lists are effective tools to group several objects together. Lists are entered in curly brackets and elements are separated by a comma.

mylist = {1, 2, 3}

We can now perform operations on a list as a whole. For example:

mylist + 4

2*mylist

x^mylist

In Mathematica vectors are simply lists; let us now define two vectors in R^4 and play with them.

v = {1, 2, 4, 0}
w = {3, 5, 1, 5}
v + w
3* v

We can even have variables as entries in vectors.

{1, a, b} + {2, c, d}

Hoever, Mathematica will also perform some list operations which are not Linear Algebra operations.  For instance

v*w

simply multiplies the vectors v and w term-by-term, but this is not a Linear Algebra operation. On the other hand Mathematica does not allow to do you obvious nonsene.

z = {(Subscript[x, 1]), (Subscript[x, 2]), (Subscript[x, 3])};
v + z

To Mathematica, a matrix is a list of lists: each row is a list of its own and the matrix is a list of rows.

M = {{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 0}}

To get Mathematica to display A in usual matrix form, we need the MatrixForm command which can be used in the following two ways.

MatrixForm[M]
M  // MatrixForm

You can use the MatrixForm command also to display a vector as a column vector.

MatrixForm[z]

Note that this is somehow inconsistent since v is a 1 by 4 matrix and not 4 by 1.

To multiply a matrix and a vector resp. two matrices the following syntax is necessary.

A = {{(Subscript[a, 11]), (Subscript[a, 12]), (Subscript[a, 13]), (Subscript[a, 14])}, {(Subscript[a, 21]), (Subscript[a, 22]), (Subscript[a, 23]), 
            (Subscript[a, 24])}, {(Subscript[a, 31]), (Subscript[a, 32]), (Subscript[a, 33]), 
            (Subscript[a, 34])}};
MatrixForm[A]
B = {{(Subscript[b, 11]), (Subscript[b, 12]), (Subscript[b, 13]), (Subscript[b, 14])}, {(Subscript[b, 21]), (Subscript[b, 22]), (Subscript[b, 23]), 
            (Subscript[b, 24])}, {(Subscript[b, 31]), (Subscript[b, 32]), (Subscript[b, 33]), (Subscript[b, 34])}, {(Subscript[b, 41]), (Subscript[b, 42]), (Subscript[b, 43]), 
            (Subscript[b, 44])}};
MatrixForm[B]
z = {(Subscript[x, 1]), (Subscript[x, 2]), (Subscript[x, 3]), (Subscript[x, 4])}; MatrixForm[z]
A . B // MatrixForm
A . z // MatrixForm

Is is possible to calculate the product BA and Bz? Go and try it!

B . A // MatrixForm
B . z // MatrixForm

An alternative way to enter a matrix into Mathematica is to use the Input...Create Table/Matrix/Palette menu item.You choose whether you are creating a table, matrix, or palette, and the number of rows and columns.You then fill in the boxes (either pressing theTabkey or using the mouse to move to empty boxes). I used this menu item to create the matrix in the next cell. Use this method to input 2*A.

M=(({{2, 4, 6}, {8, 10, 12}, {14, 16, 18}}))

The n by n identity matrix is given by IdentityMatrix[n].  Here's the 3 by 3 one.

MatrixForm[IdentityMatrix[3]]

Mathematica has a built-in command to row-reduce a matrix to Gauss form.

RowReduce[M]

To make the output more pleasing again use the MatrixForm command

MatrixForm[%]

One word of warning - if you row reduce a matrix which contains symbols, Mathematica will assume that any expression containing a symbol is non-zero.  This can sometimes lead to misleading answers.  For instance, the following row reduces to the identity matrix, even though it should not if ad-bc = 0.  (Why should it not in this case?)

MatrixForm[RowReduce[(({{a, b}, {c, d}}))]]

Be sure to evaluate the next cell. (Why?)

Clear[M, A, B, v, w, z]

Solving Systems of Linear Equations

Now we have collected enough knowledge to solve systems of linear equations with Mathematica. First we enter our equations:

eqs = {x + y + z == 2, x - 2*y + z == 0, 
        2*y -  2*z == 0}

We can now go and simply solve te equations using the Solve command.

Solve[eqs, {x, y, z}]

Of course one can alternatively enter the coefficient matrix of a system by hand---which in many cases will be the faster way
and use the RowReduce command.

A = {{1, 1, 1, 2}, {1, -2, 1, 0}, {0, 2, -2, 
            0}};
MatrixForm[A]

RowReduce[A] // MatrixForm

You now can directly read off the solution form the reduced form of the matrix.
Let's now see how Mathematica works if a system does not have a unique solution.

eqs1 = {x + y + z == 0, 2 x + 2 y + 2 z == 0, 
          x + y - z == 0};
Solve[eqs1, {x, y, z}]

Alternatively we may use the following:

B:=(({{1, 1, 1, 0}, {2, 2, 2, 0}, {1, 1, -1, 0}}));
RowReduce[B] // 
      MatrixForm

So we may read off the solution which in parameter form is given by:  y=t, z=0, x=-t with t an arbitrary real number. Copare this with the result the Solve operator returned above. Is it the same?
So the vector space of solutions of the system is one-dimensional. There is also an Operator which gives the basis of the nullspace of a matrix A, i.e., the space of solutions to Ax=0. To check our above result we may use:

NullSpace[(({{1, 1, 1}, {2, 2, 2}, {1, 1, -1}}))]

Returning to inhomogeneuous equations (i.e., equations with non-vanishing right hand side) we pick a new example. We want to solve Ax=b where:

A = {{3, 1, 2}, {2, -1, -1}, {1, 2, 3}}; 
    MatrixForm[A]
b = {{4}, {2}, {2}}; MatrixForm[b]

Note that I have entered the vector b explizitly as a 3 by 1 matrix since we will need that below.
There is a command which directly solves the system called LinearSolve. Well to be precise: LinearSolve just gives one solution to the system (compare the help page!).

LinearSolve[A, b]

Let's check whether this is really a solution.

A . % // MatrixForm
A . %% == b

To check wheter the is more than one solution we use RowReduce. To this end we have to join the vector b as last column to the matrix A. This can be done as follows using the AppendRows command, which, however is not included into the standard Mathematica kernel but has to be loaded as an additional package which we do here.

<<  LinearAlgebra`MatrixManipulation`

M := AppendRows[A, b]
MatrixForm[M]

Note that this would not have worked if b had been entered as a 1 by 3 matrix.

c = {4, 2, 2};
AppendRows[A, c]

Finally we may reduce the matrix.

RowReduce[M] // MatrixForm

So the solution is of the form z=t,y=(1/5)*(2-7*t), x=(1/5)*(6-t), with t a real number. Let's finally check this.

v = {(1/5)*(6 - t), (1/5)*(2 - 7*t), 
        t}
MatrixForm[A . v]

Now is this what you have expected?
If not you have to simplify the result to see whether is coincides with the vector b.

Simplify[%] // MatrixForm