Matrices

from numpy import inf
from numpy import array
from numpy import identity
from numpy import trace
from numpy import count_nonzero
from numpy import tensordot
from numpy import tril, triu, diag
from numpy.linalg import norm, inv, det, matrix_rank
from scipy.sparse import csr_matrix

Vectors and Vector Arithmetic

Defining a Vector

vector = array([1,2,3])
print(vector)
[1 2 3]

Vector Arithmetic

Vector Addition

a = array([1,2,3])
print(a)
[1 2 3]
b = array([4,5,6])
print(b)
[4 5 6]
c = a + b
print(c)
[5 7 9]

Vector Subtraction

a = array([1,2,3])
print(a)
[1 2 3]
b = array([0.5,0.5,0.5])
print(b)
[0.5 0.5 0.5]
c = a - b
print(c)
[0.5 1.5 2.5]

Vector Multiplication

a = array([1,2,3])
print(a)
[1 2 3]
b = array([1,2,3])
print(b)
[1 2 3]
c = a*b
print(c)
[1 4 9]

Vector Division

a = array([1,2,3])
print(a)
[1 2 3]
b = array([1,2,3])
print(b)
[1 2 3]
c = a/b
print(c)
[1. 1. 1.]

Vector Dot Product

a = array([1,2,3])
print(a)
[1 2 3]
b = array([1,2,3])
print(b)
[1 2 3]
c = a.dot(b)
print(c)
14

Vector-Scalar Multiplication

a = array([1,2,3])
print(a)
[1 2 3]
b = 0.5
print(b)
0.5
c = b*a
print(c)
[0.5 1.  1.5]

Vector Norms

a = array([1,2,3])
print(a)
[1 2 3]

Vector L1 Norm

l1 = norm(a, 1)
print(l1)
6.0

Vector L2 Norm

l2 = norm(a)
print(l2)
3.7416573867739413

Vector Max Norm

maxnorm = norm(a,inf)
print(maxnorm)
3.0

Matrices and Matrix Arithmetic

Defining a Matrix

A = array([[1,2,3],
          [4,5,6]])
print(A)
[[1 2 3]
 [4 5 6]]

Matrix Arithmetic

A = array([[1,2,3],
         [4,5,6]])
print(A)
[[1 2 3]
 [4 5 6]]
B = array([[1,2,3],
          [4,5,6]])
print(B)
[[1 2 3]
 [4 5 6]]

Matrix Addition

C = A + B
print(C)
[[ 2  4  6]
 [ 8 10 12]]

Matrix Subtraction

C = A - B
print(C)
[[0 0 0]
 [0 0 0]]

Matrix Multiplication (Hadamard Product)

C = A *B
print(C)
[[ 1  4  9]
 [16 25 36]]

Matrix Division

C = A / B
print(C)
[[1. 1. 1.]
 [1. 1. 1.]]

Matrix-Matrix Multiplication

A = array([
    [1,2],
    [3,4],
    [5,6]])
print(A)
[[1 2]
 [3 4]
 [5 6]]
B = array([
    [1,2],
    [3,4]])
print(B)
[[1 2]
 [3 4]]
C = A.dot(B)
print(C)
[[ 7 10]
 [15 22]
 [23 34]]
D = A @ B
print(D)
[[ 7 10]
 [15 22]
 [23 34]]

Matrix-Vector Multiplication

A = array([
    [1,2],
    [3,4],
    [5,6]])
print(A)
[[1 2]
 [3 4]
 [5 6]]
b = array([0.5, 0.5])
print(b)
[0.5 0.5]
C = A.dot(b)
print(C)
[1.5 3.5 5.5]

Matrix-Scalar Multiplication

A = array([
    [1,2],
    [3,4],
    [5,6]])
print(A)
[[1 2]
 [3 4]
 [5 6]]
b = 0.5
print(b)
0.5
C = A*b
print(C)
[[0.5 1. ]
 [1.5 2. ]
 [2.5 3. ]]

Types of Matrices

Square Matrix

Symmetric Matrix

Triangular Matrix

M = array([
    [1,2,3],
    [1,2,3],
    [1,2,3]])
print(M)
[[1 2 3]
 [1 2 3]
 [1 2 3]]
lower = tril(M)
print(lower)
[[1 0 0]
 [1 2 0]
 [1 2 3]]
upper = triu(M)
print(upper)
[[1 2 3]
 [0 2 3]
 [0 0 3]]

Diagonal Matrix

M = array([
    [1,2,3],
    [1,2,3],
    [1,2,3]])
print(M)
[[1 2 3]
 [1 2 3]
 [1 2 3]]
d = diag(M)
print(d)
[1 2 3]
D = diag(d)
print(D)
[[1 0 0]
 [0 2 0]
 [0 0 3]]

Identity Matrix

I = identity(3)
print(I)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Orthogonal Matrix

Q = array([
    [1,0],
    [0,-1]])
print(Q)
[[ 1  0]
 [ 0 -1]]
V = inv(Q)
print(V)
[[ 1.  0.]
 [-0. -1.]]
print(Q.T)
[[ 1  0]
 [ 0 -1]]
I = Q.dot(Q.T)
print(I)
[[1 0]
 [0 1]]

Matrix Operations

Transpose

A = array([
    [1,2],
    [3,4],
    [5,6]])
print(A)
[[1 2]
 [3 4]
 [5 6]]
C = A.T
print(C)
[[1 3 5]
 [2 4 6]]

Inverse

A = array([
    [1.0, 2.0],
    [3.0, 4.0]])
print(A)
[[1. 2.]
 [3. 4.]]
B = inv(A)
print(B)
[[-2.   1. ]
 [ 1.5 -0.5]]
I = A.dot(B)
print(I)
[[1.0000000e+00 0.0000000e+00]
 [8.8817842e-16 1.0000000e+00]]

Trace

A = array([
    [1,2,3],
    [4,5,6],
    [7,8,9]])
print(A)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
B = trace(A)
print(B)
15

Determinant

A = array([
    [1,2,3],
    [4,5,6],
    [7,8,99]])
print(A)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8 99]]
B = det(A)
print(B)
-269.99999999999983

Rank

v1 = array([1,2,3])
print(v1)
[1 2 3]
print(matrix_rank(v1))
1
v2 = array([0,0,0,0,0])
print(v2)
[0 0 0 0 0]
print(matrix_rank(v2))
0
M0 = array([
    [0,0],
    [0,0]])
print(M0)
[[0 0]
 [0 0]]
print(matrix_rank(M0))
0
M1 = array([
    [1,2],
    [1,2]])
print(M1)
[[1 2]
 [1 2]]
print(matrix_rank(M1))
1
M2 = array([
    [1,2],
    [3,4]])
print(M2)
[[1 2]
 [3 4]]
print(matrix_rank(M2))
2

Sparse Matrices

Sparse Matrices in Python

A = array([
    [1,0,0,1,0,0],
    [0,0,2,0,0,1],
    [0,0,0,2,0,0]])
print(A)
[[1 0 0 1 0 0]
 [0 0 2 0 0 1]
 [0 0 0 2 0 0]]
S = csr_matrix(A)
print(S)
  (0, 0)    1
  (0, 3)    1
  (1, 2)    2
  (1, 5)    1
  (2, 3)    2
B = S.todense()
print(B)
[[1 0 0 1 0 0]
 [0 0 2 0 0 1]
 [0 0 0 2 0 0]]
sparsity = 1.0 - count_nonzero(A) / A.size
print(sparsity)
0.7222222222222222

Tensors and Tensor Arithmetic

Tensors in Python

T = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])
print(T)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[11 12 13]
  [14 15 16]
  [17 18 19]]

 [[21 22 23]
  [24 25 26]
  [27 28 29]]]
print(T.shape)
(3, 3, 3)

Tensor Arithmetic

Tensor Addition

A = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

B = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

C = A + B
print(C)
[[[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]

 [[22 24 26]
  [28 30 32]
  [34 36 38]]

 [[42 44 46]
  [48 50 52]
  [54 56 58]]]

Tensor Subtraction

A = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

B = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

C = A - B
print(C)
[[[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]]

Tensor Hadamard Product

A = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

B = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

C = A * B
print(C)
[[[  1   4   9]
  [ 16  25  36]
  [ 49  64  81]]

 [[121 144 169]
  [196 225 256]
  [289 324 361]]

 [[441 484 529]
  [576 625 676]
  [729 784 841]]]

Tensor Division

A = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

B = array([
    [[1,2,3], [4,5,6], [7,8,9]],
    [[11,12,13], [14,15,16], [17,18,19]],
    [[21,22,23], [24,25,26], [27,28,29]]])

C = A / B
print(C)
[[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]]

Tensor Product

A = array([1,2])
B = array([3,4])
C = tensordot(A, B, axes=0)
print(C)
[[3 4]
 [6 8]]