Linear Algebra Functions
This appendix will help readers translate the linear algebra they know into Julia code. This material is based on the official documentation for the LinearAlgebra
1 package and Eldén (2007).
Some common vector operations in Julia are given in Table E.1.
Note that the Julia operations given in Table E.1 should not be considered an exclusive list. For example, both dot(x,y)
and x
·y
are quoted for the inner product but ·(x,y)
also works. The following code block illustrates some of the operations in Table E.1.
x = [1, 2]
y = [3, 4]
c = 8
c*x
# 2-element Array{Int64,1}:
# 8
# 16
dot(x,y)
# 11
x + c*ones(2)
# 2-element Array{Float64,1}:
# 9.0
# 10.0
normalize(x)
# 2-element Array{Float64,1}:
# 0.4472135954999579
# 0.8944271909999159
Table E.1 Common vector operations in Julia, using LinearAlgebra, for n-dimensional vectors x and y.
Operation |
Notation |
Julia |
Addition |
x + y |
|
Subtraction |
x − y |
|
Scalar-vector addition |
x + c1 |
|
Scalar-vector multiplication |
cx |
|
Transpose |
x′ or x⊤ |
|
Inner product |
x′y |
|
Cross product |
x × y |
|
Norm |
|| x || |
|
Distance |
|| x − y || |
|
Normalize |
x/|| x|| |
|
Sum |
|
|
Mean |
|
Some common matrix operations in Julia are given in Table E.2; again, this is not intended to be an exhaustive list.
The following code block illustrates some of the operations in Table E.2.
X = [5 1; 2 4]
Y = [2 0; 1 5]
y = [3, 4]
X*y
# 2-element Array{Int64,1}:
# 19
# 22
*(Y,X)
# 2x2 Array{Int64,2}:
# 10 2
# 15 21
## Row sums
sum(X,dims=1)
# 1x2 Array{Int64,2}:
# 7 5
Table E.2 Common matrix operations in Julia, where all operations assume compatible matrix and vector dimensions.
Operation |
Notation |
Julia |
Addition |
X + Y |
|
Subtraction |
X − Y |
|
Transpose |
X′ or X⊤ |
|
Inverse |
X−1 |
|
Moore-Penrose pseudo-inv. |
X† |
|
Scalar-matrix multiplication |
cX, c ∈ ℝ |
|
Vector-matrix multiplication |
Xy |
|
Matrix-matrix multiplication |
XY |
|
Matrix raised to a power p |
Xp |
|
Determinant |
|X| |
|
Log-determinant |
log(|X|) |
|
Log absolute value det. |
log(||X||) |
|
Column sum |
|
|
Row sum |
|
Some common matrix decompositions in Julia are given in Table E.3.
The following code block illustrates how to use the decompositions in Table E.3.
using LinearAlgebra
X = [5 1 3; 0 8 2; 3 1 6]
Y = [8 0 1; 0 3 2; 1 2 5] # symmetric positive-definite
## Eigenvalue decomposition
evX=eigen(X)
# eigenvectors
evX.vectors
# 3x3 Array{Float64,2}:
# 0.7024 -0.384529 -0.497977
# 0.242773 0.7843 -0.652046
# -0.6691 -0.486837 -0.571712
# eigenvalues
evX.values
# 3-element Array{Float64,1}:
# 2.487860053322798
# 6.758543755579095
# 9.753596191098106
## Singular value decomposition
svdX=svd(X)
# matrix U
svdX.U
# 3x3 Array{Float64,2}:
# -0.476574 0.457265 -0.750857
# -0.655574 -0.753904 -0.0430237
# -0.585747 0.471739 0.659062
# matrix V
svdX.V
# 3x3 Adjoint{Float64,Array{Float64,2}}:
# -0.422445 0.539959 -0.728
# -0.643539 -0.744284 -0.178604
# -0.638278 0.393046 0.661904
# matrix Sigma
diagm(0 => svdX.S)
# 3x3 Array{Float64,2}:
# 9.80036 0.0 0.0
# 0.0 6.85522 0.0
# 0.0 0.0 2.44107
## Cholesky decomposition
cholY=cholesky(Y)
# matrix L
cholY.L
# 3x3 LowerTriangular{Float64,Array{Float64,2}}:
# 2.82843 . .
# 0.0 1.73205 .
# 0.353553 1.1547 1.88193
# note that cholY.U gives transpose(cholY.L)
## QR decomposition
qrX = qr(X)
# matrix Q
qrX.Q
# 3x3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
# -0.857493 0.0220386 -0.514024
# 0.0 -0.999082 -0.0428353
# -0.514496 -0.036731 0.856706
#matrix R
qrX.R
# 3x3 Array{Float64,2}:
# -5.83095 -1.37199 -5.65945
# 0.0 -8.00735 -2.15243
# 0.0 0.0 3.51249
Table E.3 Common matrix decompositions in Julia, where Notation is intended to help in understanding the subsequent code block.
Decomposition |
Notation |
Julia |
Eigenvalue |
Xm×m = PDP−1 |
|
Singular value |
Xm×n = Um×n∑m×n V′n×n, m = n |
|
Cholesky |
Xm×m = LL′, X positive definite |
|
QR |
Xm×n = Qm×mRm×n, m ≥n |
|
1http://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html