11. Basic linear algebra
Basic linear algebra in Julia¶
Author: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/) (with edits from Jane Herriman)
Reference¶
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)
Topics:
Series¶
- http://deepstat.tistory.com/45 (01. Getting started)(in English)
- http://deepstat.tistory.com/46 (01. Getting started(한글))
- http://deepstat.tistory.com/47 (02. Strings)(in English)
- http://deepstat.tistory.com/48 (02. Strings(한글))
- http://deepstat.tistory.com/49 (03. Data structures)(in English)
- http://deepstat.tistory.com/50 (03. Data structures(한글))
- http://deepstat.tistory.com/51 (04. Loops)(in English)
- http://deepstat.tistory.com/52 (04. Loops(한글))
- http://deepstat.tistory.com/53 (05. Conditionals)(in English)
- http://deepstat.tistory.com/54 (05. Conditionals(한글))
- http://deepstat.tistory.com/55 (06. Functions)(in English)
- http://deepstat.tistory.com/56 (06. Functions(한글))
- http://deepstat.tistory.com/57 (07. Packages)(in English)
- http://deepstat.tistory.com/58 (07. Packages(한글))
- http://deepstat.tistory.com/59 (08. Plotting)(in English)
- http://deepstat.tistory.com/60 (08. Plotting(한글))
- http://deepstat.tistory.com/61 (09. Julia is fast)(in English)
- http://deepstat.tistory.com/62 (09. Julia is fast(한글))
- http://deepstat.tistory.com/63 (10. Multiple dispatch)(in English)
- http://deepstat.tistory.com/64 (10. Multiple dispatch(한글))
- http://deepstat.tistory.com/66 (11. Basic linear algebra in Julia(한글))
First let's define a random matrix
A = rand(1:4,4,4)
Define a vector of ones
x = fill(1.0, (4,)) # = fill(1.0, 4)
Notice that A has type Array{Int64,2} but x has type Array{Float64,1}. Julia defines the aliases Vector{Type}=Array{Type,1} and Matrix{Type}=Array{Type,2}.
Many of the basic operations are the same as in other languages
Multiplication¶
b = A*x
Transposition¶
As in other languages A'
is the conjugate transpose, or adjoint
A'
and we can get the transpose with
transpose(A)
Transposed multiplication¶
Julia allows us to write this without *
A'A
Solving linear systems¶
The problem Ax = b for square A is solved by the \ function.
A\b
A^-1 * b
A\b
gives us the least squares solution if we have an overdetermined linear system (a "tall" matrix)
Atall = rand(4, 2)
Atall\b
(Atall'Atall)^-1 * Atall'b
and the minimum norm least squares solution if we have a rank-deficient least squares problem
v1 = rand(4)
v2 = rand(4)
rankdef = hcat(v1, v1, v2)
rankdef\b
Julia also gives us the minimum norm solution when we have an underdetermined system (a "short" matrix)
bshort = rand(2)
Ashort = rand(2, 3)
Ashort\bshort
The LinearAlgebra library
While much of linear algebra is available in Julia by default (as shown above), there's a standard library named LinearAlgebra
that brings in many more relevant names and functions. In particular, it provides factorizations and some structured matrix types. As with all packages, you can bring these additional features into your session with a using LinearAlgebra
.
Exercises
10.1¶
Take the inner product (or "dot" product) of a vector v
with itself and assign it to variable dot_v
.
v = [1,2,3]
dot_v = v'v
using LinearAlgebra
dot_v = dot(v,v)
dot_v = v⋅v # \cdot<tab>
dot_v = ⋅(v,v) # \cdot<tab>
@assert dot_v == 14
10.2¶
Take the outer product of a vector v with itself and assign it to variable cross_v
using LinearAlgebra
cross_v = cross(v,v)
cross_v = v×v # \times<tab>
cross_v = ×(v,v) # \times<tab>
@assert cross_v == [0, 0, 0]