Lay Chapter 2,
Matrix Algebra

Matrix Operations

Product of Matrices

In[1]:=

a = ( {{1, 2}, {3, 4}} ) ;

b = ( {{5, 6}, {7, 8}} ) ; 

a . b ;

%//MatrixForm

Out[4]//MatrixForm=

( {{19, 22}, {43, 50}} )

Powers of Matrices

In[5]:=

a = ( {{2, 0}, {0, 3}} ) ; 

MatrixPower[a, 4] ;

%//MatrixForm

Out[7]//MatrixForm=

( {{16, 0}, {0, 81}} )

Transpose of a Matrix

In[8]:=

a = ( {{1, 2}, {3, 4}} ) ; 

Transpose[a] ;

%//MatrixForm

Out[10]//MatrixForm=

( {{1, 3}, {2, 4}} )

Special Matrices

In[11]:=

IdentityMatrix[3] ;

%//MatrixForm

Out[12]//MatrixForm=

( {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} )

In[13]:=

DiagonalMatrix[{1, 2, 3}] ;

%//MatrixForm

Out[14]//MatrixForm=

( {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}} )

In[15]:=

<<LinearAlgebra`MatrixManipulation`

In[16]:=

ZeroMatrix[2, 3] ;

%//MatrixForm

Out[17]//MatrixForm=

( {{0, 0, 0}, {0, 0, 0}} )

Matrix Equation A.x == b

Matrix Equation A.x == b

In[18]:=

Clear[a, x, x1, x2, b, eqns] ; 

a = ( {{1, 2}, {3, 4}} ) ;

b = {5, 6} ; 

x = {x1, x2} ; 

eqn = {a . xb} ; 

Print[a//MatrixForm, ".", x//MatrixForm, " = ", b//MatrixForm] 

Solve[eqn, {x1, x2}]//Flatten

( {{1, 2}, {3, 4}} )  .  ( {{x1}, {x2}} )  =  ( {{5}, {6}} )

Out[24]=

{x1 -4, x29/2}

Matrix Generator

makeMatrix

Let's write a procedure which creates matrices.

In[25]:=

makeMatrix[n_, m_] := Table[Random[Integer, {-9, 9}], {n}, {m}]

Generate a few matrices.
New matrices will be generated each time these cells are evaluated.

In[26]:=

Clear[a, b, c] ; 

a = makeMatrix[2, 2] ;

%//MatrixForm

Out[28]//MatrixForm=

( {{3, 5}, {-2, 7}} )

In[29]:=

b = makeMatrix[3, 4] ;

%//MatrixForm

Out[30]//MatrixForm=

( {{0, -4, 9, 5}, {5, 0, 4, -9}, {-8, 4, -6, 6}} )

In[31]:=

c = makeMatrix[4, 1] ;

%//MatrixForm

Out[32]//MatrixForm=

( {{0}, {-7}, {4}, {3}} )

Inverse of a Square Matrix

Matrix Inverse

In[33]:=

a = ({{0, 1, 2}, {1, 0, 3}, {4, -3, 8}}) ; 

aInv = Inverse[a] ;

%//MatrixForm

Out[35]//MatrixForm=

( {{-9/2, 7, -3/2}, {-2, 4, -1}, {3/2, -2, 1/2}} )

Check.

In[36]:=

id = IdentityMatrix[3] ; 

a . aInvid

aInv . aid

Out[37]=

True

Out[38]=

True

Matrix Inverse Via Row Reduction

Use row reduction to find the inverse of the matrix a.

In[39]:=

<<LinearAlgebra`MatrixManipulation`

In[40]:=

Clear[a, id, aug, rr, aInv] ; 

a = ({{0, 1, 2}, {1, 0, 3}, {4, -3, 8}}) ;

id = IdentityMatrix[3] ; 

aug = AppendRows[a, id] ;

%//MatrixForm

Out[44]//MatrixForm=

( {{0, 1, 2, 1, 0, 0}, {1, 0, 3, 0, 1, 0}, {4, -3, 8, 0, 0, 1}} )

In[45]:=

rr = RowReduce[aug] ;

%//MatrixForm

Out[46]//MatrixForm=

( {{1, 0, 0, -9/2, 7, -3/2}, {0, 1, 0, -2, 4, -1}, {0, 0, 1, 3/2, -2, 1/2}} )

In[47]:=

aInv = TakeColumns[rr, -3] ;

%//MatrixForm

Out[48]//MatrixForm=

( {{-9/2, 7, -3/2}, {-2, 4, -1}, {3/2, -2, 1/2}} )

Check.

In[49]:=

a . aInvid

aInv . aid

Out[49]=

True

Out[50]=

True

Block Matrices

Building a Block Matrix

BlockMatrix maps AppendRows across each row of its argument, then applies AppendColumn to the result.

In[51]:=

<<LinearAlgebra`MatrixManipulation`

In[52]:=

a1 = ( {{1, 2}, {3, 5}} ) ;

a2 = {{2}} ;

a3 = ( {{7, 8}, {5, 6}} ) ; 

z23 = ZeroMatrix[2, 3] ;

z12 = ZeroMatrix[1, 2] ; 

b = BlockMatrix[ {{a1, z23},  {z12, a2, z12},  {z23, a3}}] ;

%//MatrixForm

Out[58]//MatrixForm=

( {{1, 2, 0, 0, 0}, {3, 5, 0, 0, 0}, {0, 0, 2, 0, 0}, {0, 0, 0, 7, 8}, {0, 0, 0, 5, 6}} )

Extracting a Block

In[59]:=

Take[b, {3, 5}, {3, 5}] ;

%//MatrixForm

Out[60]//MatrixForm=

( {{2, 0, 0}, {0, 7, 8}, {0, 5, 6}} )

An alternate method.

In[61]:=

rows = Range[3, 5] ;

cols = Range[3, 5] ; 

b[[rows, cols]] ;

%//MatrixForm

Out[64]//MatrixForm=

( {{2, 0, 0}, {0, 7, 8}, {0, 5, 6}} )


Created by Mathematica  (February 10, 2005) Valid XHTML 1.1!