General Mathematics Review, Linear Algebra Overview, and Using Python for Linear Algebra

Scalars

# Defining scalars (as floats, not ints)
a = 5.0
b = 4.0

# Addition, Subtraction, Multiplication, Division
add = a+b
sub = a-b
mul = a*b 
div = a/b 
print("Scalar Math:", add, sub, mul, div)

# Multiplication with Parentheses
# Multiplication is not implicit.
mul_paren = (a+b)*(a-b)
print("Multiplication with Partentheses:", mul_paren)
Scalar Math: 9 1 20 1.25
Multiplication with Partentheses: 9

Complex Numbers

import numpy as np

# Define a complex number
z = 4+3j
print("Complex Number:", z)

# Find the complex conjugate
z_conj = z.conjugate()
print("Complex Conjugate:", z_conj)

# Find the modulus and modulus squared
# Note that the modulus and modulus squared are still 
# complex numbers
z_mod_squared = z*z_conj
z_mod = np.sqrt(z_mod_squared)
print("Modulus Squared and Modulus:", z_mod_squared, z_mod)

# Basic Arithmetic
x = 1+2j
y = 3-4j
print("Complex Number Math:", x+y, x-y, x*y)
Complex Number: (4+3j)
Complex Conjugate: (4-3j)
Modulus Squared and Modulus: (25+0j) (5+0j)
Complex Number Math: (4-2j) (-2+6j) (11+2j)

Vectors

import numpy as np 

# Define vectors as Numpy arrays
a = np.array([1,2,3])
b = np.array([4,5,6])
c = 10

# Arithmetic with Vectors
add = a+b 
sub = a-b 
mul = c*a
print("Vector Math:", add, sub, mul)

# Dot product two ways
dot1 = np.dot(a,b) #Order is important
dot2 = a.dot(b) # Order is important
print("Dot Product:", dot1, dot2)

# Cross product
cross = np.cross(a,b)
print("Cross Product:", cross)

# Magnitude and unit vector
mag = np.linalg.norm(a)
unit_vector = a/mag
unit_vector_mag = np.linalg.norm(unit_vector)
print("Magnitude, Unit Vector, Magnitude of Unit Vector:", mag, unit_vector, unit_vector_mag)
Vector Math: [5 7 9] [-3 -3 -3] [10 20 30]
Dot Product: 32 32
Cross Product: [-3  6 -3]
Magnitude, Unit Vector, Magnitude of Unit Vector: 3.7416573867739413 [0.26726124 0.53452248 0.80178373] 1.0

Matrices

import numpy as np 
# Matrices are defined as two-dimensional Numpy arrays
A = np.array([[1,2,3], [4,5,6], [7,8,9]])
B = np.array([[9,8,7], [6,5,4], [3,2,1]])
# Define a vector
x = np.array([2,4,6])
# Define a scalar
c = 10

# Matrix addition and subtraction
add = A+B 
sub = A-B
print("Matrix Addition:", add)
print("Matrix Subtraction:", sub)
print()

# Matrix-matrix multiplication
# Do not use the * symbol!!
mul1 = A@B 
mul2 = B@A 
commutator = mul1 - mul2
print("Matrix Multiplication 1:",mul1)
print("Matrix Multiplication 2:", mul2)
print("Commutator:", commutator)
print()

# Matrix-Vector Multiplication
mul3 = A@x
print("Matrix-Vector Multiplication:", mul3)
print()

# Matrix-Scalar Multiplication
# Use the * symbol!!
mul4 = c*B 
print("Matrix-Scalar Multiplixation:", mul4)
print()

# Transpose and Adjoint
transpose = A.T 
adjoint = np.conj(B).T
print("Transpose:", transpose)
print("Adjoint:", adjoint)
print()

# Eigenvalues and Eigenvectors
# Note that the eigenvectors of A are the columns of the matrix
# eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:",eigenvalues)
print("Eigenvectors:",eigenvectors)
Matrix Addition: [[10 10 10]
 [10 10 10]
 [10 10 10]]
Matrix Subtraction: [[-8 -6 -4]
 [-2  0  2]
 [ 4  6  8]]

Matrix Multiplication 1: [[ 30  24  18]
 [ 84  69  54]
 [138 114  90]]
Matrix Multiplication 2: [[ 90 114 138]
 [ 54  69  84]
 [ 18  24  30]]
Commutator: [[ -60  -90 -120]
 [  30    0  -30]
 [ 120   90   60]]

Matrix-Vector Multiplication: [ 28  64 100]

Matrix-Scalar Multiplixation: [[90 80 70]
 [60 50 40]
 [30 20 10]]

Transpose: [[1 4 7]
 [2 5 8]
 [3 6 9]]
Adjoint: [[9 6 3]
 [8 5 2]
 [7 4 1]]

Eigenvalues: [ 1.61168440e+01 -1.11684397e+00 -8.58274334e-16]
Eigenvectors: [[-0.23197069 -0.78583024  0.40824829]
 [-0.52532209 -0.08675134 -0.81649658]
 [-0.8186735   0.61232756  0.40824829]]