Hugh Murrell / Aug 15 2019

Chapter 2, Linear Algebra

Linear Algebra with Julia

load the packages

# first make sure the `LinearAlgebra` package is loaded
using LinearAlgebra

Vector operations

# we have seen 1D arrays, aka column vectors
vc = [1, 2, 3]
3-element Array{Int64,1}: 1 2 3
# a (1 x 3) row vector. 
vr = [10 20 30] 
1×3 Array{Int64,2}: 10 20 30
# inner product,
vr * vc
1-element Array{Int64,1}: 140
# outer product
vc * vr
3×3 Array{Int64,2}: 10 20 30 20 40 60 30 60 90
vc = [10; 20; 30] # Create a column vector. 
               # Semi-colon is used to change row.
3-element Array{Int64,1}: 10 20 30

Matrix operations

# Semi-colon is used to change rows.
# Space is used to seperate columns.
M = [1 2 3; 4 5 6; 7 8 9] 
3×3 Array{Int64,2}: 1 2 3 4 5 6 7 8 9
# first index is row, 2nd is col
M[1,2] = 0 
M
3×3 Array{Int64,2}: 1 0 3 4 5 6 7 8 9
# matrix times column vector
M * vc 
3-element Array{Int64,1}: 100 320 500
# row vector times matrix
vr * M 
1×3 Array{Int64,2}: 300 340 420
# Transpose of a matrix using single qoute.
M' 
3×3 Adjoint{Int64,Array{Int64,2}}: 1 4 7 0 5 8 3 6 9
# Inverse of a matrix.
inv(M) 
3×3 Array{Float64,2}: 0.25 -2.0 1.25 -0.5 1.0 -0.5 0.25 0.666667 -0.416667
# matrix times matrix
M * inv(M) 
3×3 Array{Float64,2}: 1.0 0.0 0.0 0.0 1.0 0.0 0.0 -8.88178e-16 1.0
# turn M into a symmetric matrix
M = (M + M') ./ 2 
3×3 Array{Float64,2}: 1.0 2.0 5.0 2.0 5.0 7.0 5.0 7.0 9.0
# eigenvalue decomposition from the Linear Algebra package
w, U = eigen(M) 
Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}} eigenvalues: 3-element Array{Float64,1}: -1.901862138338473 0.8172409872018385 16.084621151136602 eigenvectors: 3×3 Array{Float64,2}: -0.732524 0.596549 -0.327928 -0.368347 -0.752455 -0.546014 0.572476 0.279177 -0.770929
# The columns of U are authonormal eigenvectors
U * U' 
3×3 Array{Float64,2}: 1.0 8.32667e-17 0.0 8.32667e-17 1.0 -1.11022e-16 0.0 -1.11022e-16 1.0
# checking the Eigenvector decomposition theorem
U * Diagonal(w) * U' 
3×3 Array{Float64,2}: 1.0 2.0 5.0 2.0 5.0 7.0 5.0 7.0 9.0
# positive definite matrix
A = [ 2 -1 0; -1 2 -1; 0 -1 2 ]  
3×3 Array{Int64,2}: 2 -1 0 -1 2 -1 0 -1 2
eigen(A)
Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}} eigenvalues: 3-element Array{Float64,1}: 0.5857864376269073 2.0000000000000018 3.414213562373095 eigenvectors: 3×3 Array{Float64,2}: 0.5 -0.707107 -0.5 0.707107 1.09906e-15 0.707107 0.5 0.707107 -0.5
# square root for positive definite matrices
function matSqrt(M)
    w, U = eigen(M)
    return U * Diagonal(w).^(1/2) * U'
end

matSqrt(A)
3×3 Array{Float64,2}: 1.36039 -0.382683 -0.0538253 -0.382683 1.30656 -0.382683 -0.0538253 -0.382683 1.36039
matSqrt(A) * matSqrt(A)
3×3 Array{Float64,2}: 2.0 -1.0 -2.91434e-16 -1.0 2.0 -1.0 -4.16334e-17 -1.0 2.0

using the GPU

using CUDAdrv; CUDAdrv.name(CuDevice(0))
using LinearAlgebra, Flux, CuArrays
function test_gpu(n=1000,m=10)
		M = rand(n,n) |> gpu
    R = zeros(n,n) |> gpu
    for k in 1:m
        # perform matrix multiplication
        R = M * M
    end
    return 1
end
test_gpu (generic function with 3 methods)
@time test_gpu(8192,10)
1
function test_cpu(n=1000,m=10)
		M = rand(n,n) |> cpu
    R = zeros(n,n) |> cpu
    for k in 1:m
        # perform matrix multiplication
        R = M * M
    end
    return 1
end
test_cpu (generic function with 3 methods)
@time test_cpu(8192,10)
1