from numpy import array
A = array([[1, 2, 3], [4, 5, 6]])
b = 2
C = A * b
print(C)
[[ 2 4 6]
[ 8 10 12]]
Matrix-Vector Multiplication
A matrix can be multiplied by a vector represented as C = A . v where v is a vector given it follows the rule of matrix multiplication.
Rule of matrix multiplication
For example, matrix A has the dimensions m rows and n columns and matrix B has the dimensions n and k. The n columns in A and n rows b are equal. The result is a new matrix with m rows and k columns.
from numpy import array
from numpy import dot
A = array([[1, 2], [3, 4], [5, 6]])
v = array([0.5, 0.5])
C = dot(A, v)
print(C)
[1.5 3.5 5.5]
Matrix Multiplication (Hadamard Product)
Two matrices with the same dimension can be simply multiplied element-wise. Also known as Hadamard Product, it is represented with a circle asC = A o B
Note: Implemented using star operator as in Matrix-Scalar Multiplication
from numpy import array
A = array([[1, 2, 3], [4, 5, 6]])
B = array([[2, 2, 2], [.5, .5, .5]])
C = A * B
print(C)
[[2. 4. 6. ]
[2. 2.5 3. ]]
Matrix-Matrix Multiplication (Dot Product)
Matrix-Matrix, also called the matrix dot product is a complicated multiplication which must follow the rule of matrix multiplication represented asC = A * B.
This is made clear with the following image:
A: 4 x 2; B: 2 x 3; C = A * B => 4 x 3
For calculating:
C12(marked with red arrow) = a11*b12 + a12*b22
C33(marked with blue arrow) = a31*b13 + a32*b23
from numpy import array
from numpy import dot
A = array([[1, 2], [3, 4], [5, 6], [7, 8]])
B = array([[2, 2, 2], [.5, .5, .5]])
C = dot(A,B)
print(C)