Multiplication of matrices
Definition
For general matrices A and B
multiplication of matrices (or inner/dot-product of row and column) is defined if and only if n1 = m2 = u.
Corollary: A⋅B and B⋅A are defined if and only if n1 = m2 = u and n2 = m1 = v.
Note that the dot-product of row and column can be considered the multiplication of the i row and j column of respective matrices. Therefore, dot-product is also called inner product.
mtimes
operator
Import the mtimes
operator in the namespace by doing
=> (require '[kigubkur.op [mtimes :refer [mtimes]]])
Examples using mtimes
Example 1
=> (def A [[1]
[2]
[3]])
=> (def B [[2 3 4]])
=> (mtimes A B)
[[2 3 4]
[4 6 8]
[6 9 12]]
Example 2
=> (def A [[2 5]
[8 10]])
=> (def B [[5 4]
[50 40]])
=> (mtimes A B)
[[260 208]
[540 432]]
Example 3
=> (def A [[1 -1 2]
[0 3 4]])
=> (def B [[2 7]
[-1 1]
[5 -4]])
=> (mtimes A B)
[[13 -2]
[17 -13]]
Example 4
=> (def A [[6 9]
[2 3]])
=> (def B [[2 6 0]
[7 9 8]])
=> (mtimes A B)
[[75 117 72]
[25 39 24]]
Commutativity of multiplication of matrices
Assuming AB and BA are defined, in general AB ≠ BA.
=> (def A [[1 -2 3]
[-4 2 5]])
=> (def B [[2 3]
[4 5]
[2 1]])
=> (mtimes A B)
[[0 -4]
[10 3]]
=> (mtimes B A)
[[-10 2 21]
[-16 2 37]
[-2 -2 11]]
Special case of commutativity
If A and B are diagonal matrices of same order then AB = BA.
=> (def A [[1 0]
[0 2]])
=> (def B [[3 0]
[0 4]])
=> (mtimes A B)
[[3 0]
[0 8]]
=> (= (mtimes A B) (mtimes B A))
true
Product of two non-zero matrices can be a zero matrix
Matrices A, B or both does not have to be a zero matrix for their product to be a zero matrix.
=> (def A [[0 -1]
[0 2]])
=> (def B [[3 5]
[0 0]])
=> (mtimes A B)
[[0 0]
[0 0]]
Basic properties of matrix multiplication
1. Associative Law
=> (def A [[1 1 -1]
[2 0 3]
[3 -1 2]])
=> (def B [[1 3]
[0 2]
[-1 4]])
=> (def C [[1 2 3 -4]
[2 0 -2 1]])
=> (= (mtimes (mtimes A B) C) (mtimes A (mtimes B C)))
true
2. Distributive Law
=> (require '[kigubkur.op [plus :refer [plus]]])
2.1. Given that the + and ⋅ operations on the matrices A, B and C are defined
=> (def A [[1 1]
[-1 3]])
=> (def B [[1 2]
[-4 2]])
=> (def C [[-7 6]
[3 2]])
=> (= (mtimes A (plus B C)) (plus (mtimes A B) (mtimes A C)))
true
2.2. Given that the + and ⋅ operations on the matrices A, B and C are defined
=> (def A [[0 6 7]
[-6 0 8]
[7 -8 0]])
=> (def B [[0 1 1]
[1 0 2]
[1 2 0]])
=> (def C [[2]
[-2]
[3]])
=> (= (mtimes (plus A B) C) (plus (mtimes A C) (mtimes B C)))
true
3. Existence of multiplicative identity
=> (def A [[2 0 1]
[2 1 3]
[1 -1 0]])
=> (def I [[1 0 0]
[0 1 0]
[0 0 1]])
=> (= (mtimes I A) (mtimes A I) A)
true