Matrix Arithmetic Manipulations
In[21]:=
a={{a11, a12, a13, a14},{a21, a22, a23, a24},{a31, a32, a33, a34},
{a41, a42, a43, a44}}
Out[21]=
{{a11, a12, a13, a14}, {a21, a22, a23, a24},
{a31, a32, a33, a34}, {a41, a42, a43, a44}}
In[22]:=
MatrixForm[a]
MatrixForm[b]
MatrixForm[a+b]
Out[22]=
a11 a12 a13 a14 a21 a22 a23 a24 a31 a32 a33 a34 a41 a42 a43 a44
Out[23]=
b11 b12 b13 b14 b21 b22 b23 b24 b31 b32 b33 b34 b41 b42 b43 b44
Out[24]=
a11 + b11 a12 + b12 a13 + b13 a14 + b14 a21 + b21 a22 + b22 a23 + b23 a24 + b24 a31 + b31 a32 + b32 a33 + b33 a34 + b34 a41 + b41 a42 + b42 a43 + b43 a44 + b44
The scalar multiple of a matrix and a real or complex number is defined by multiplying each entry by the number.
In[25]:=
MatrixForm[a]
MatrixForm[5 a]
Out[25]=
a11 a12 a13 a14 a21 a22 a23 a24 a31 a32 a33 a34 a41 a42 a43 a44
Out[26]=
5 a11 5 a12 5 a13 5 a14 5 a21 5 a22 5 a23 5 a24 5 a31 5 a32 5 a33 5 a34 5 a41 5 a42 5 a43 5 a44
The scalar product (or dot product or inner product ) of two row vectors (or two column vectors) is defined as the sum of aibi. In order to take the scalar product of two vectors, they must have the same size. Note also that the scalar product is a number, not a vector.
In[27]:=
v={v1, v2, v3}
w={w1, w2, w3}
Out[27]=
{v1, v2, v3}
Out[28]=
{w1, w2, w3}
In[29]:=
v.w
Out[29]=
v1 w1 + v2 w2 + v3 w3
Matrix multiplication is defined in terms of the scalar product in a way that may seem unusual at first but in fact is quite natural, as we shall see later. If c is the matrix product of a and b, then cij is the scalar product of the ith row of a with the jth column of b.
In[30]:=
c=a.b
Out[30]=
{{a11 b11 + a12 b21 + a13 b31 + a14 b41,
a11 b12 + a12 b22 + a13 b32 + a14 b42,
a11 b13 + a12 b23 + a13 b33 + a14 b43,
a11 b14 + a12 b24 + a13 b34 + a14 b44},
{a21 b11 + a22 b21 + a23 b31 + a24 b41,
a21 b12 + a22 b22 + a23 b32 + a24 b42,
a21 b13 + a22 b23 + a23 b33 + a24 b43,
a21 b14 + a22 b24 + a23 b34 + a24 b44},
{a31 b11 + a32 b21 + a33 b31 + a34 b41,
a31 b12 + a32 b22 + a33 b32 + a34 b42,
a31 b13 + a32 b23 + a33 b33 + a34 b43,
a31 b14 + a32 b24 + a33 b34 + a34 b44},
{a41 b11 + a42 b21 + a43 b31 + a44 b41,
a41 b12 + a42 b22 + a43 b32 + a44 b42,
a41 b13 + a42 b23 + a43 b33 + a44 b43,
a41 b14 + a42 b24 + a43 b34 + a44 b44}}
In[31]:=
MatrixForm[c]
Out[31]=
a11 b11 + a12 b21 + a13 b31 + a14 b41 a11 b12 + a12 b22 +
a13 b32 + a14 b42 a11 b13 + a12 b23 + a13 b33 +
a14 b43 a11 b14 + a12 b24 + a13 b34 + a14 b44
a21 b11 + a22 b21 + a23 b31 + a24 b41 a21 b12 + a22 b22 +
a23 b32 + a24 b42 a21 b13 + a22 b23 + a23 b33 +
a24 b43 a21 b14 + a22 b24 + a23 b34 + a24 b44
a31 b11 + a32 b21 + a33 b31 + a34 b41 a31 b12 + a32 b22 +
a33 b32 + a34 b42 a31 b13 + a32 b23 + a33 b33 +
a34 b43 a31 b14 + a32 b24 + a33 b34 + a34 b44
a41 b11 + a42 b21 + a43 b31 + a44 b41 a41 b12 + a42 b22 +
a43 b32 + a44 b42 a41 b13 + a42 b23 + a43 b33 +
a44 b43 a41 b14 + a42 b24 + a43 b34 + a44 b44
Whew! Maybe smaller matrices would illustrate this better.
In[32]:=
d={{d11, d12}, {d21, d22}}
e={{e11, e12}, {e21, e22}}
Out[32]=
{{d11, d12}, {d21, d22}}
Out[33]=
{{e11, e12}, {e21, e22}}
In[34]:=
MatrixForm[d]
MatrixForm[e]
MatrixForm[d.e]
Out[34]=
d11 d12 d21 d22
Out[35]=
e11 e12 e21 e22
Out[36]=
d11 e11 + d12 e21 d11 e12 + d12 e22 d21 e11 + d22 e21 d21 e12 + d22 e22
In order to multiply two matrices, they must be conformable, that is, the number of columns of the first matrix must equal the number of rows in the second matrix. How does this fit in with the scalar product and the definition of the matrix product???
In[37]:=
m=Table[Random[],{4},{3}]
n=Table[10 Random[],{3},{5}]
Out[37]=
{{0.0499997, 0.787589, 0.775051},
{0.876872, 0.0135532, 0.279709},
{0.728216, 0.176528, 0.263129},
{0.0784836, 0.407847, 0.284064}}
Out[38]=
{{9.31301, 1.24126, 7.1071, 5.81226, 8.36503},
{2.05298, 7.639, 8.00021, 9.85009, 3.85097},
{7.8235, 6.78679, 9.3501, 5.97508, 0.0729916}}
In[39]:=
MatrixForm[m]
MatrixForm[n]
MatrixForm[m.n]
Out[39]=
0.0499997 0.787589 0.775051 0.876872 0.0135532 0.279709 0.728216 0.176528 0.263129 0.0784836 0.407847 0.284064
Out[40]=
9.31301 1.24126 7.1071 5.81226 8.36503 2.05298 7.639 8.00021 9.85009 3.85097 7.8235 6.78679 9.3501 5.97508 0.0729916
Out[41]=
8.14616 11.3386 13.903 12.6794 3.5078 10.3824 3.09028 8.95575 6.90139 7.40767 9.20288 4.0382 9.04804 7.54361 6.79055 3.79059 5.14084 6.47667 6.1708 2.24786
In[42]:=
x=Table[10 Random[],{2},{2}]
y=Table[10 Random[],{2},{4}]
Out[42]=
{{8.01806, 9.21456}, {3.17799, 2.79083}}
Out[43]=
{{6.25279, 6.58327, 2.39315, 8.71236},
{3.41215, 7.27027, 1.15189, 1.60526}}
In[44]:=
MatrixForm[x]
MatrixForm[y]
MatrixForm[x.y]
Out[44]=
8.01806 9.21456 3.17799 2.79083
Out[45]=
6.25279 6.58327 2.39315 8.71236 3.41215 7.27027 1.15189 1.60526
Out[46]=
81.5767 119.777 29.8026 84.6481 29.394 41.2117 10.8201 32.1678
In[47]:=
z=Table[10 Random[],{5},{2}]
j=Table[10 Random[],{2},{3}]
Out[47]=
{{7.59989, 8.90524}, {9.09891, 3.96626}, {9.59968, 9.05515},
{5.24794, 6.14276}, {2.81289, 9.70505}}
Out[48]=
{{9.27286, 6.06977, 4.79483}, {0.490486, 6.09488, 3.27894}}
In[49]:=
MatrixForm[z]
MatrixForm[j]
MatrixForm[z.j]
Out[49]=
7.59989 8.90524 9.09891 3.96626 9.59968 9.05515 5.24794 6.14276 2.81289 9.70505
Out[50]=
9.27286 6.06977 4.79483 0.490486 6.09488 3.27894
Out[51]=
74.8406 100.406 65.6399 86.3184 79.4022 56.6328 93.458 113.458 75.7201 51.6764 69.2932 45.3047 30.8438 76.2247 45.3096
In[52]:=
MatrixForm[j.z]
Dot::dotsh: Tensors {{9.27286, 6.06977, 4.79483}, <<1>>} and
{{7.59989, 8.90524}, <<3>>, {2.81289, 9.70505}}
have incompatible shapes.
Out[52]=
{{9.27286, 6.06977, 4.79483},
{0.490486, 6.09488, 3.27894}} .
{{7.59989, 8.90524}, {9.09891, 3.96626},
{9.59968, 9.05515}, {5.24794, 6.14276},
{2.81289, 9.70505}}
Based on the above matrix multiplications, when you multiply and nxm matrix with an mxk matrix, what size matrix do you get? Try more of your own if you do not see the pattern.
The identity matrices are the matrix multiplication identity elements.
In[53]:=
MatrixForm[z]
MatrixForm[z.IdentityMatrix[2]]
Out[53]=
7.59989 8.90524 9.09891 3.96626 9.59968 9.05515 5.24794 6.14276 2.81289 9.70505
Out[54]=
7.59989 8.90524 9.09891 3.96626 9.59968 9.05515 5.24794 6.14276 2.81289 9.70505
In[55]:=
MatrixForm[a.IdentityMatrix[4]]
Out[55]=
a11 a12 a13 a14 a21 a22 a23 a24 a31 a32 a33 a34 a41 a42 a43 a44
An nxn square matrix P may have an inverse Q, that is, an nxn matrix such that PQ=QP=Identity Matrix of size n. If P has an inverse, P is called nonsingular or invertible. Otherwise P is called singular.
In[56]:=
P={{1, 1, 4},{2, -2, 1},{0, 4, -1}}
Out[56]=
{{1, 1, 4}, {2, -2, 1}, {0, 4, -1}}
In[57]:=
Q={{-1/16, 17/32, 9/32},{1/16, -1/32, 7/32},{1/4, -1/8, -1/8}}
Out[57]=
1 17 9 1 1 7 1 1 1
{{-(--), --, --}, {--, -(--), --}, {-, -(-), -(-)}}
16 32 32 16 32 32 4 8 8
In[58]:=
MatrixForm[P.Q]
MatrixForm[Q.P]
Out[58]=
1 0 0 0 1 0 0 0 1
Out[59]=
1 0 0 0 1 0 0 0 1
Mathematica has a command to find the inverse of a (square) matrix.
In[60]:=
b={{1, -1, 2}, {3, 2, 4}, {0, 1, -2}}
Inverse[b]
MatrixForm[Inverse[b]]
Out[60]=
{{1, -1, 2}, {3, 2, 4}, {0, 1, -2}}
Out[61]=
3 1 1 3 1 5
{{1, 0, 1}, {-(-), -, -(-)}, {-(-), -, -(-)}}
4 4 4 8 8 8
Out[62]=
1 0 1
3 1 1
-(-) - -(-)
4 4 4
3 1 5
-(-) - -(-)
8 8 8
In[63]:=
MatrixForm[b.Inverse[b]]
Out[63]=
1 0 0 0 1 0 0 0 1
In[64]:=
a={{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}
Inverse[a]
Out[64]=
{{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}
LinearSolve::nosol:
Linear equation encountered which has no solution.
Out[64]=
Inverse[{{1, -1, 1}, {2, 0, 1}, {3, -1, 2}}]
Mathematica has told us that a is singular and therefore has no inverse.
You can repeatedly multiply a matrix by itself (raise it to a power) using the MatrixPower command.
In[65]:=
z={{1, 1, 0}, {0, 1, 0}, {0, 0, 1}}
Out[65]=
{{1, 1, 0}, {0, 1, 0}, {0, 0, 1}}
In[66]:=
MatrixPower[z, 4]
Out[66]=
{{1, 4, 0}, {0, 1, 0}, {0, 0, 1}}