Math 291: Intro to Mathematica (for Linear Algebra)

with dusty wilson, mathematics instructor, highline community college
week 2

Contents

- Review entering and manipulating matrices
- Manipulating multiple matrices
- Row reduce matrices
- Write more primative function to add matrices (use a clean notebook)
- Write more primative function to multiple matrices (use a clean notebook)

A Handy Bit of Code

A={{1,2},{3,4}} (*enter as a list of lists*)

A//MatrixForm (*express in our usual notation*)

$Post:=If[MatrixQ[#],MatrixForm[#],#]& (*outputs all matrices in MatrixForm (very slick, copy and don't memorize*)

A

Linear Algebra

Example 1: Manipulating matrices

A[[2,1]](*extract an element*)

A[[2]] (*extracts the second row*)

A[[{2,1}]] (*swaps the second and first rows*)

Transpose[A](*switches rows and columns*)

Det[A] (*finds the determinant of a square matrix*)

Dimensions[A][[2]] (*the dimensions of a matrix*)

Example 2: Manipulating multiple matrices

a.) Note: Mathematica "remembers" matrix A from the previous example.

B={{5,6},{7,8}} (*matrix defined as above*)

A+B

b.) The key here is that you need to use MatrixForm on results and not when defining matrices

F={{9,10},{11,12}}(*matrix defined, but displayed in MatrixForm*)

B+F

Example 3: Row Reducing Matrices

a.) Row reduction is pretty straightforward

G = {{1, 2, 3}, {4, 5, 6}}
RowReduce[G]

b.) Two ways to extract the solutions from the resulting matrix.

RowReduce[G][[All,3]](*Now that is a nice solution*)

c.) Non-numeric matrices may also be row reduced

J={{1,2,-3},{a,-2,5}}
RowReduce[J]

d.) Sometimes we want to extract the square portion of an augmented matrix out ... there has gotta be a better way to do this, but here is what I came up with.

J (*recall matrix J*)
J[[All,{1,2}]] (*a nice method*)

e.) Looking back to part (c.), notice that the determinant is zero at the same time as the system associated with matrix J would have no solution.

Det[%] (*Finds the determinant of the previous result*)
Solve[%==0,a]

Example 4: Adding Matrices

a.) Matrices are added by adding entries in equivalent locations.  This is a good chance to learn the function Table and/or Array (much cleaner).

A+B

Example 5: Multiplying Matrices

a.) Write a command to multiply the matrices G and H together.  This is a good chance to learn the functions: Array and Dimensions

G = {{1, 2, 3}, {4, 5, 6}} 
H={{9,8,7,6},{5,4,3,2},{1,3,5,9}}
G.H

a.) Use the Table command to enter a matrix with the integers 1 thru 10 on the diagonal, 0 below the diagonal, and 1 above the diagonal.

matA=ConstantArray[0,{10,10}];
Table[matA[[i,i]]=i,{i,1,10}];
Table[Table[matA[[i,j]]=1,{i,j+1,10}],{j,1,9}];matA

b.) Consider the block matrix shown below.  Write a custom command blockMatrix that will generate matrices of anysize of this form.  For instance the output below should result from blockMatrix[5].

blockMatrix[n_]:=Module[{matB},
z[m_]:=ConstantArray[m,{m,m}];
matB=ConstantArray[0,{n,n}];
Table[matB[[i,i]]=z[i],{i,1,n}];
ArrayFlatten[matB]]

blockMatrix[5]