Skip to content

Latest commit

 

History

History
167 lines (115 loc) · 3.09 KB

File metadata and controls

167 lines (115 loc) · 3.09 KB

Linear Algebra with Numpy

1 Summary

1.1 Scalars, Vectors, Matrices and Tensors (I)

img/scalars-vectors-matrices-tensors.png

1.2 Scalars, Vectors, Matrices and Tensors (II)

x = 1 # scalar
x = [1, 2] # vector
x = [[1, 2], [3, 4]] # matrix
x = [[[1, 2], [3, 2]], [[1, 7], [5, 4]]] # tensor

1.3 Multiplying Matrices and Vectors (I)

file:img/multiplying-matrices-vectors.png

1.4 Multiplying Matrices and Vectors (II)

x = np.array([[3, 6, 7], [5, -3, 0]])
y = np.array([[1, 1], [2, 1], [3, -3]])
z = x.dot(y)

1.5 Identity and Inverse Matrices (I)

file:img/identity.png

1.6 Identity and Inverse Matrices (II)

np.identity(3)

1.7 Linear Dependence

file:img/linear-dependence.png

1.8 Linear Algebra

  • Rank, determinant, trace, etc. of an array.
  • Eigen values of matrices
  • Matrix and vector products (dot, inner, outer,etc. product), matrix exponentiation
  • Solve linear or tensor equations

1.9 Linear Algebra (II)

# Importing numpy as np
import numpy as np

A = np.array([[6, 1, 1],
              [4, -2, 5],
              [2, 8, 7]])

# Rank of a matrix
print("Rank of A:", np.linalg.matrix_rank(A))

# Trace of matrix A
print("\nTrace of A:", np.trace(A))

# Determinant of a matrix
print("\nDeterminant of A:", np.linalg.det(A))

# Inverse of matrix A
print("\nInverse of A:\n", np.linalg.inv(A))

print("\nMatrix A raised to power 3:\n",
           np.linalg.matrix_power(A, 3))

1.10 Linear Algebra (III). Matrix eigenvalues functions

from numpy import linalg as geek

# Creating an array using array
# function
a = np.array([[1, -2j], [2j, 5]])

print("Array is :",a)

# calculating an eigen value
# using eigh() function
c, d = geek.eigh(a)

print("Eigen value is :", c)
print("Eigen value is :", d)

1.11 Linear Algebra (IV). Eigen value

from numpy import linalg as geek

# Creating an array using diag
# function
a = np.diag((1, 2, 3))

print("Array is :",a)

# calculating an eigen value
# using eig() function
c, d = geek.eig(a)

print("Eigen value is :",c)
print("Eigen value is :",d)

1.12 Linear Algebra (V)

import numpy as geek

# Scalars
product = geek.dot(5, 4)
print("Dot Product of scalar values  : ", product)

# 1D array
vector_a = 2 + 3j
vector_b = 4 + 5j

product = geek.dot(vector_a, vector_b)
print("Dot Product  : ", product)

1.13 Linear Algebra (VI). Solve

import numpy as np

# Creating an array using array
# function
a = np.array([[1, 2], [3, 4]])

# Creating an array using array
# function
b = np.array([8, 18])

print(("Solution of linear equations:",
      np.linalg.solve(a, b)))

1.14 Trace (I)

file:img/trace.png

1.15 Trace (II)

import numpy as np

np.trace(np.eye(3))
a = np.arange(8).reshape((2,2,2))
np.trace(a)