In [1]:
eye(5)


Out[1]:
5×5 Array{Float64,2}:
 1.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0
 0.0  0.0  1.0  0.0  0.0
 0.0  0.0  0.0  1.0  0.0
 0.0  0.0  0.0  0.0  1.0

In [8]:
# 1-indexed!!!
eye(5)[1, 1]


Out[8]:
1.0

In [12]:
eye(5)[1:2, 1:2]


Out[12]:
2×2 Array{Float64,2}:
 1.0  0.0
 0.0  1.0

In [15]:
eye(5)[1:3, :]


Out[15]:
3×5 Array{Float64,2}:
 1.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0
 0.0  0.0  1.0  0.0  0.0

In [16]:
eye(5)[1:3, 4]


Out[16]:
3-element Array{Float64,1}:
 0.0
 0.0
 0.0

In [22]:
x = repeat([1], outer=[3])


Out[22]:
3-element Array{Int64,1}:
 1
 1
 1

In [28]:
*(x, transpose(x))


Out[28]:
3×3 Array{Int64,2}:
 1  1  1
 1  1  1
 1  1  1

In [33]:
e = eye(4) + 3 * *([0, 0, 0, 1], transpose([1, 0, 0, 0]))
e


Out[33]:
4×4 Array{Float64,2}:
 1.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0
 0.0  0.0  1.0  0.0
 3.0  0.0  0.0  1.0

Numerical Algorithms by Justin Solomon

https://people.csail.mit.edu/jsolomon/share/book/numerical_book.pdf

Example 3.3 Page 54


In [96]:
A = [[0 1 -1]; [3 -1 1]; [1 1 -2]]


Out[96]:
3×3 Array{Int64,2}:
 0   1  -1
 3  -1   1
 1   1  -2

In [87]:
# permute
P = [[0 0 1]; [1 0 0]; [0 1 0]]


Out[87]:
3×3 Array{Int64,2}:
 0  0  1
 1  0  0
 0  1  0

In [88]:
e3_ = repeat([0], outer=[3, 1])
e3_[3, 1] = 1
e1_T = repeat([0], outer=[1, 3])
e1_T[1, 1] = 1
E1 = eye(3) - 3 * *(e3_, e1_T)


Out[88]:
3×3 Array{Float64,2}:
  1.0  0.0  0.0
  0.0  1.0  0.0
 -3.0  0.0  1.0

In [64]:
e2_T = repeat([0], outer=[1, 3])
e2_T[1, 2] = 1
E2 = eye(3) + 4 * *(e3_, e2_T)


Out[64]:
3×3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  4.0  1.0

In [66]:
S = Diagonal([1,1,1/3])


Out[66]:
3×3 Diagonal{Float64}:
 1.0   ⋅    ⋅      
  ⋅   1.0   ⋅      
  ⋅    ⋅   0.333333

In [72]:
e1_ = repeat([0], outer=[3, 1])
e1_[1, 1] = 1
e3_T = repeat([0], outer=[1, 3])
e3_T[1, 3] = 1
E3 = eye(3) + 2 * *(e1_, e3_T)


Out[72]:
3×3 Array{Float64,2}:
 1.0  0.0  2.0
 0.0  1.0  0.0
 0.0  0.0  1.0

In [73]:
e2_ = repeat([0], outer=[3, 1])
e2_[2, 1] = 1
E4 = eye(3) + *(e2_, e3_T)


Out[73]:
3×3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  1.0
 0.0  0.0  1.0

In [76]:
e3_T


Out[76]:
1×3 Array{Int64,2}:
 0  0  1

In [77]:
*(e1_, e3_T)


Out[77]:
3×3 Array{Int64,2}:
 0  0  1
 0  0  0
 0  0  0

In [89]:
E5 = eye(3) - *(e1_, e2_T)


Out[89]:
3×3 Array{Float64,2}:
 1.0  -1.0  0.0
 0.0   1.0  0.0
 0.0   0.0  1.0

In [91]:
A_inv = E5 * E4 * E3 * S * E2 * E1 * P


Out[91]:
3×3 Array{Float64,2}:
 0.333333  0.333333   0.0
 2.33333   0.333333  -1.0
 1.33333   0.333333  -1.0

In [102]:
A_inv


Out[102]:
3×3 Array{Float64,2}:
 0.333333  0.333333   0.0
 2.33333   0.333333  -1.0
 1.33333   0.333333  -1.0

In [103]:
b = [-1, 4, -3]


Out[103]:
3-element Array{Int64,1}:
 -1
  4
 -3

In [106]:
# Solution
A_inv * b


Out[106]:
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

In [105]:
# Identity
A_inv * A


Out[105]:
3×3 Array{Float64,2}:
 1.0  -5.55112e-17  5.55112e-17
 0.0   1.0          2.22045e-16
 0.0   0.0          1.0        

LU factorization


In [114]:
m = [[1 2 3]; [4 5 6]; [7 8 9]]


Out[114]:
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

In [115]:
l, u, p = lu(A)


Out[115]:
([1.0 0.0 0.0; 0.333333 1.0 0.0; 0.0 0.75 1.0], [3.0 -1.0 1.0; 0.0 1.33333 -2.33333; 0.0 0.0 0.75], [2, 3, 1])

In [116]:
l


Out[116]:
3×3 Array{Float64,2}:
 1.0       0.0   0.0
 0.333333  1.0   0.0
 0.0       0.75  1.0

In [117]:
u


Out[117]:
3×3 Array{Float64,2}:
 3.0  -1.0       1.0    
 0.0   1.33333  -2.33333
 0.0   0.0       0.75   

In [118]:
p


Out[118]:
3-element Array{Int64,1}:
 2
 3
 1