General Mathematics Review, Linear Algebra Overview, and Using Python for Linear Algebra
Scalars
# Defining scalars (as floats, not ints)a =5.0b =4.0# Addition, Subtraction, Multiplication, Divisionadd = a+bsub = a-bmul = 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 numberz =4+3jprint("Complex Number:", z)# Find the complex conjugatez_conj = z.conjugate()print("Complex Conjugate:", z_conj)# Find the modulus and modulus squared# Note that the modulus and modulus squared are still # complex numbersz_mod_squared = z*z_conjz_mod = np.sqrt(z_mod_squared)print("Modulus Squared and Modulus:", z_mod_squared, z_mod)# Basic Arithmeticx =1+2jy =3-4jprint("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 arraysa = np.array([1,2,3])b = np.array([4,5,6])c =10# Arithmetic with Vectorsadd = a+b sub = a-b mul = c*aprint("Vector Math:", add, sub, mul)# Dot product two waysdot1 = np.dot(a,b) #Order is importantdot2 = a.dot(b) # Order is importantprint("Dot Product:", dot1, dot2)# Cross productcross = np.cross(a,b)print("Cross Product:", cross)# Magnitude and unit vectormag = np.linalg.norm(a)unit_vector = a/magunit_vector_mag = np.linalg.norm(unit_vector)print("Magnitude, Unit Vector, Magnitude of Unit Vector:", mag, unit_vector, unit_vector_mag)
import numpy as np # Matrices are defined as two-dimensional Numpy arraysA = 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 vectorx = np.array([2,4,6])# Define a scalarc =10# Matrix addition and subtractionadd = A+B sub = A-Bprint("Matrix Addition:", add)print("Matrix Subtraction:", sub)print()# Matrix-matrix multiplication# Do not use the * symbol!!mul1 = A@B mul2 = B@A commutator = mul1 - mul2print("Matrix Multiplication 1:",mul1)print("Matrix Multiplication 2:", mul2)print("Commutator:", commutator)print()# Matrix-Vector Multiplicationmul3 = A@xprint("Matrix-Vector Multiplication:", mul3)print()# Matrix-Scalar Multiplication# Use the * symbol!!mul4 = c*B print("Matrix-Scalar Multiplixation:", mul4)print()# Transpose and Adjointtranspose = A.T adjoint = np.conj(B).Tprint("Transpose:", transpose)print("Adjoint:", adjoint)print()# Eigenvalues and Eigenvectors# Note that the eigenvectors of A are the columns of the matrix# eigenvectorseigenvalues, eigenvectors = np.linalg.eig(A)print("Eigenvalues:",eigenvalues)print("Eigenvectors:",eigenvectors)