Module: Stick::Matrix::LU

Defined in:
lib/stick/matrix/lu.rb

Class Method Summary collapse

Class Method Details

.factorization(mat) ⇒ Object

LU factorization: A = LU where L is lower triangular and U is upper triangular



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stick/matrix/lu.rb', line 30

def LU.factorization(mat)
  u = mat.clone
  n = u.column_size
  i = Matrix.I(n)
  l = i.clone
  (n-1).times {|k|
    mk = gauss(u, k)
    u = mk * u  # M_{n-1} * ... * M_1 * A = U
    l += i - mk  # L = M_1^{-1} * ... * M_{n-1}^{-1} = I + sum_{k=1}^{n-1} tau * e
  }
  return l, u
end

.gauss(mat, k) ⇒ Object

Return the Gauss transformation matrix: M_k = I - tau * e_k^T



20
21
22
23
24
25
# File 'lib/stick/matrix/lu.rb', line 20

def LU.gauss(mat, k)
  i = Matrix.I(mat.column_size)
  tau = gauss_vector(mat, k)
  e = i.row2matrix(k)
  i - tau * e
end

.gauss_vector(mat, k) ⇒ Object

Return the Gauss vector, MC, Golub, 3.2.1 Gauss Transformation, p94



9
10
11
12
13
14
15
16
# File 'lib/stick/matrix/lu.rb', line 9

def LU.gauss_vector(mat, k)
  t = mat.column2matrix(k)
  tk = t[k, 0]
  (0..k).each{|i| t[i, 0] = 0}
  return t if tk == 0
  (k+1...mat.row_size).each{|i| t[i, 0] = t[i, 0].to_f / tk}
  t
end