# Matrix Multiply and Inverses¶

## Many ways to multiply matrices¶

In [4]:
A=rand(1:3,4,6)

Out[4]:
4x6 Array{Int64,2}:
3  3  1  3  3  1
2  2  2  3  1  3
3  1  2  2  1  3
3  3  3  2  1  2

In [5]:
B=rand(1:3,7,8)

Out[5]:
7x8 Array{Int64,2}:
3  3  3  3  3  1  1  2
1  1  1  1  3  2  3  1
3  2  3  2  2  3  2  2
3  3  1  2  3  2  3  1
3  3  3  1  3  2  3  2
2  1  3  2  2  1  2  3
2  3  1  1  1  2  1  2

In [6]:
A*B # matmul only works if A is m x n  and B is n x p

*: argument shapes do not match
at In[6]:1
in generic_matmatmul at linalg/matmul.jl:409
in generic_matmatmul at linalg/matmul.jl:398
in * at linalg/matmul.jl:391
In [7]:
B=rand(1:3,6,8)

Out[7]:
6x8 Array{Int64,2}:
2  2  2  2  2  2  3  2
3  3  2  2  1  1  3  2
1  1  2  1  2  1  2  1
3  2  2  2  1  1  2  2
1  1  1  1  2  2  2  2
1  2  2  1  3  1  3  2

In [8]:
A*B

Out[8]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [9]:
A[3,:]

Out[9]:
1x6 Array{Int64,2}:
3  1  2  2  1  3

In [10]:
B[:,4]

Out[10]:
6-element Array{Int64,1}:
2
2
1
2
1
1

In [13]:
A[3,:]*B[:,4]

Out[13]:
1-element Array{Int64,1}:
18


## Linear Combinations of Columns of A¶

In [19]:
[A*B[:,1] A*B[:,2] A*B[:,3] A*B[:,4] A*B[:,5] A*B[:,6]  A*B[:,7] A*B[:,8]]

Out[19]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [15]:
A*B  # Every column of A*B is some linear combination of columns of A

Out[15]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25


## Linear Combinations of Rows of B¶

In [23]:
[ A[1,:]*B; A[2,:]*B; A[3,:]*B; A[4,:]*B]

Out[23]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [21]:
A[1,:] % 3 x first row of B + 3 * 2nd row + 1* 3rd row + etc.

Out[21]:
1x6 Array{Int64,2}:
3  3  1  3  3  1


## Outer Product Matrix Multiply¶

In [25]:
v=rand(5,1); w=rand(1,5)

Out[25]:
1x5 Array{Float64,2}:
0.648415  0.107518  0.49727  0.0484907  0.148389

In [27]:
v*w;w*v

Out[27]:
1x1 Array{Float64,2}:
0.341224

In [32]:
[1:12]*[1:12]'  # Outer Products (recognize the 3rd grade mult table)

Out[32]:
12x12 Array{Int64,2}:
1   2   3   4   5   6   7   8    9   10   11   12
2   4   6   8  10  12  14  16   18   20   22   24
3   6   9  12  15  18  21  24   27   30   33   36
4   8  12  16  20  24  28  32   36   40   44   48
5  10  15  20  25  30  35  40   45   50   55   60
6  12  18  24  30  36  42  48   54   60   66   72
7  14  21  28  35  42  49  56   63   70   77   84
8  16  24  32  40  48  56  64   72   80   88   96
9  18  27  36  45  54  63  72   81   90   99  108
10  20  30  40  50  60  70  80   90  100  110  120
11  22  33  44  55  66  77  88   99  110  121  132
12  24  36  48  60  72  84  96  108  120  132  144

In [33]:
A*B

Out[33]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [47]:
A[:,1]*B[1,:] + A[:,2]*B[2,:] + A[:,3]*B[3,:] + A[:,4]*B[4,:]+A[:,5]*B[5,:]+A[:,6]*B[6,:]

Out[47]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [43]:
A*B

Out[43]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [44]:
A

Out[44]:
4x6 Array{Int64,2}:
3  3  1  3  3  1
2  2  2  3  1  3
3  1  2  2  1  3
3  3  3  2  1  2

In [45]:
B

Out[45]:
6x8 Array{Int64,2}:
2  2  2  2  2  2  3  2
3  3  2  2  1  1  3  2
1  1  2  1  2  1  2  1
3  2  2  2  1  1  2  2
1  1  1  1  2  2  2  2
1  2  2  1  3  1  3  2


## Block Matrix Multiply¶

In [49]:
A[1:2,1:2]*B[1:2,1:2] + A[1:2,3:4]*B[3:4,1:2]+ A[1:2,5:6]*B[5:6,1:2]

Out[49]:
2x2 Array{Int64,2}:
29  27
25  25

In [50]:
A*B

Out[50]:
4x8 Array{Int64,2}:
29  27  25  23  23  20  35  27
25  25  25  20  24  16  33  24
21  22  23  18  24  16  31  22
27  27  27  22  25  18  36  25

In [51]:
A[3:4,1:2]*B[1:2,1:2] + A[3:4,3:4]*B[3:4,1:2]+ A[3:4,5:6]*B[5:6,1:2]

Out[51]:
2x2 Array{Int64,2}:
21  22
27  27

In [52]:
A[3:4,1:2]*B[1:2,5:6] + A[3:4,3:4]*B[3:4,5:6]+ A[3:4,5:6]*B[5:6,5:6
]

Out[52]:
2x2 Array{Int64,2}:
24  16
25  18


# Matrix Inverses¶

In [53]:
A= rand(3,4)

Out[53]:
3x4 Array{Float64,2}:
0.973557  0.278488  0.300054  0.723139
0.222204  0.860992  0.562578  0.846122
0.130402  0.852375  0.108227  0.26576

In [54]:
eye(4)

Out[54]:
4x4 Array{Float64,2}:
1.0  0.0  0.0  0.0
0.0  1.0  0.0  0.0
0.0  0.0  1.0  0.0
0.0  0.0  0.0  1.0

In [72]:
A=rand(1:3,3,3)

Out[72]:
3x3 Array{Int64,2}:
1  1  2
1  1  2
1  1  1

In [73]:
inv(A)

SingularException(2)
at In[73]:1
in inv at linalg/factorization.jl:231
in inv at linalg/dense.jl:425
In [59]:
B=ans

Out[59]:
3x3 Array{Float64,2}:
-0.0  -0.333333   0.666667
-1.0   1.66667   -0.333333
1.0  -1.0        0.0

In [60]:
A*B

Out[60]:
3x3 Array{Float64,2}:
1.0  -4.44089e-16  1.11022e-16
0.0   1.0          1.11022e-16
0.0  -1.11022e-16  1.0

In [61]:
B*A

Out[61]:
3x3 Array{Float64,2}:
1.0  0.0   0.0
0.0  1.0  -2.22045e-16
0.0  0.0   1.0

In [63]:
2.0^(-52)

Out[63]:
2.220446049250313e-16

In []: