Matrix Algebra

Some Preliminary Programs

RandomInteger[x_, y_] := Random[Integer, {-50, 50}] ; RandomMatrix[n_, m_] := Array[RandomInteger, {n, m}] ; RandomMatrix[n_] := RandomMatrix[n, n] ;

Properties of Matrix Multiplication

Non-Commutativity

If you choose random matrices, their product won't commute.

A = RandomMatrix[4]

B = RandomMatrix[4]

( {{50, 33, -37, -43}, {-4, -31, 3, -36}, {-39, -9, 2, 14}, {11, 21, -7, 38}} )

( {{-30, -1, 48, -14}, {-4, -17, -4, 10}, {-30, 1, -39, 37}, {49, 36, 42, -14}} )

B . A

A . B

( {{-3522, -1685, 1301, 1466}, {134, 641, 19, 1108}, {424, 107, 776, 2114}, {514, -171, -1523, -3347}} )

( {{-2629, -2196, 1905, -1137}, {-1610, -762, -1697, 361}, {1832, 698, -1326, 334}, {1658, 993, 2313, -735}} )

Here's a more dramatic example of failure of commutativity.

Clear[A, B] ;

A = RandomMatrix[3, 4]

B = RandomMatrix[4, 3]

( {{-5, 10, 12, -24}, {-15, -29, -38, 9}, {1, 15, 48, -32}} )

( {{-26, -2, 46}, {-34, 23, 30}, {-11, 12, 29}, {-1, -40, 15}} )

A . B

B . A

( {{-318, 1344, 58}, {1785, -1453, -2527}, {-1032, 2199, 1408}} )

( {{206, 488, 1972, -866}, {-145, -557, 158, 63}, {-96, -23, 804, -556}, {620, 1375, 2228, -816}} )

Associativity

I can multiply three matrices in a product in whatever order I like.  That is to say, we have the following equality:

    A(BC) = (AB)C

We'll choose some random matrices A,B, and Cee.  (Mathematica is stupid and won't let me name something C.  Or perhaps more accurately, I'm stupider than Mathematica and can't get it to do what I say.)

Clear[A, B] ;

A = RandomMatrix[3]

B = RandomMatrix[3]

Cee = RandomMatrix[3]

( {{-2, 48, 11}, {-25, 5, -8}, {-11, 19, 18}} )

( {{-13, 0, -30}, {-35, 6, 35}, {-32, 14, 35}} )

( {{5, -3, 6}, {-16, -49, -38}, {-25, -14, -17}} )

AB = A . B

AB . Cee

( {{-2006, 442, 2125}, {406, -82, 645}, {-1098, 366, 1625}} )

( {{-70227, -45390, -64957}, {-12783, -6230, -5413}, {-51971, -37390, -48121}} )

BC = B . Cee

A . BC

( {{685, 459, 432}, {-1146, -679, -1033}, {-1259, -1080, -1319}} )

( {{-70227, -45390, -64957}, {-12783, -6230, -5413}, {-51971, -37390, -48121}} )

Distribuitivity

Matrix multiplication and addition behave well, in the sense that the following equations hold

A(B+C) = AB+AC
(B+C)A = BA+CA

Clear[A, B, Cee] ;

A = RandomMatrix[3]

B = RandomMatrix[3]

Cee = RandomMatrix[3]

( {{-39, 31, 1}, {-49, 43, 27}, {29, 37, 41}} )

( {{28, -32, 38}, {-43, 22, 17}, {14, 41, 1}} )

( {{8, 7, -22}, {-10, 45, -46}, {47, -30, -9}} )

I'll first add B and C and multiply the result on the left by A.

BPlusC = B + Cee

A . BPlusC

( {{36, -25, 16}, {-53, 67, -29}, {61, 11, -8}} )

( {{-2986, 3063, -1531}, {-2396, 4403, -2247}, {1584, 2205, -937}} )

Now I'll add the products AB and AC.  Hopefully these two will agree.

A . B + A . Cee

( {{-2986, 3063, -1531}, {-2396, 4403, -2247}, {1584, 2205, -937}} )

The same holds if I multiply on the right instead of the left.  I'll do my computations in the same order (first by adding and the multiplying, and second by multiplying and then adding).

BPlusC . A

( {{285, 633, 17}, {-2057, 165, 567}, {-3150, 2068, 30}} )

B . A + Cee . A

( {{285, 633, 17}, {-2057, 165, 567}, {-3150, 2068, 30}} )

Scalar Multiplication

Scalar multiplication is as well behaved as you can hope:
      
      A(kB) = k(AB) = (kA)B

Clear[A, B, Cee] ;

A = RandomMatrix[4, 3]

B = RandomMatrix[3, 4]

k = Random[Integer, {1, 100}]

( {{45, -36, 42}, {41, 40, -2}, {-44, 21, 31}, {2, -5, 26}} )

( {{-47, -17, 21, 20}, {-19, 42, -23, 11}, {-31, -9, -8, 49}} )

46

A . (k * B)

k * (A . B)

(k * A) . B

( {{-125718, -122130, 66102, 117852}, {-120750, 46046, -1978, 53452}, {32568, 62146, -76130, 40020}, {-37030, -21988, -2346, 57914}} )

( {{-125718, -122130, 66102, 117852}, {-120750, 46046, -1978, 53452}, {32568, 62146, -76130, 40020}, {-37030, -21988, -2346, 57914}} )

( {{-125718, -122130, 66102, 117852}, {-120750, 46046, -1978, 53452}, {32568, 62146, -76130, 40020}, {-37030, -21988, -2346, 57914}} )

Identity

Just as you can multiply any real number by 1 and get your original real number back, there is a special matrix which you can multiply by that returns the matrix you started with.  This is the identity, the matrix whose only nonzero entries are 1's along the diagonal

Clear[A, B] ;

Id = IdentityMatrix[5]

A = RandomMatrix[5]

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

( {{-47, -47, -15, -12, 32}, {-41, -18, -1, 13, 18}, {47, 49, 20, -29, -28}, {-33, -28, 22, 0, -8}, {-1, 36, -44, 26, -46}} )

I can multiply on either side by the identity, and the product will be A.

Id . A

A . Id

( {{-47, -47, -15, -12, 32}, {-41, -18, -1, 13, 18}, {47, 49, 20, -29, -28}, {-33, -28, 22, 0, -8}, {-1, 36, -44, 26, -46}} )

( {{-47, -47, -15, -12, 32}, {-41, -18, -1, 13, 18}, {47, 49, 20, -29, -28}, {-33, -28, 22, 0, -8}, {-1, 36, -44, 26, -46}} )

When I have a non-square matrix, I have to make sure my matrix dimensions are correct!

B = RandomMatrix[4, 6]

( {{-39, -31, -1, 43, 44, 27}, {5, 29, 12, 46, 20, 11}, {-9, -25, 17, -32, 28, 13}, {14, 26, -14, 14, 8, 17}} )

On the right I need to have a 6x6 identity matrix...

B . IdentityMatrix[6]

( {{-39, -31, -1, 43, 44, 27}, {5, 29, 12, 46, 20, 11}, {-9, -25, 17, -32, 28, 13}, {14, 26, -14, 14, 8, 17}} )

...but on the left I need to have a 4x4 identity matrix.

IdentityMatrix[4] . B

( {{-39, -31, -1, 43, 44, 27}, {5, 29, 12, 46, 20, 11}, {-9, -25, 17, -32, 28, 13}, {14, 26, -14, 14, 8, 17}} )

Inverses

Last class period we talked about a way to find a matrix associated to the inverse of a linear operator via row reduction.  

Clear[A] ;

A = RandomMatrix[5]

AugmentedA = Transpose[Join[Transpose[A], IdentityMatrix[5]]]

( {{15, 19, -1, -32, -11}, {-46, 4, -10, 44, -18}, {-5, 6, 30, -13, 32}, {-41, -37, -34, 2, 42}, {39, -20, 48, -10, 9}} )

AugmentedAInv = RowReduce[AugmentedA]

AInv = Transpose[Drop[Transpose[AugmentedAInv], 5]]

We also saw last time (even though we didn't know what matrix multiplication was!) that the product of a matrix with its inverse is the identity matrix.

A . AInv

AInv . A

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

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