2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-07-01 02:23:12 -04:00
|
|
|
class Matrix
|
|
|
|
# Adapted from JAMA: http://math.nist.gov/javanumerics/jama/
|
|
|
|
|
|
|
|
#
|
|
|
|
# For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
|
|
|
|
# unit lower triangular matrix L, an n-by-n upper triangular matrix U,
|
|
|
|
# and a m-by-m permutation matrix P so that L*U = P*A.
|
|
|
|
# If m < n, then L is m-by-m and U is m-by-n.
|
|
|
|
#
|
|
|
|
# The LUP decomposition with pivoting always exists, even if the matrix is
|
|
|
|
# singular, so the constructor will never fail. The primary use of the
|
|
|
|
# LU decomposition is in the solution of square systems of simultaneous
|
|
|
|
# linear equations. This will fail if singular? returns true.
|
|
|
|
#
|
|
|
|
|
|
|
|
class LUPDecomposition
|
|
|
|
# Returns the lower triangular factor +L+
|
|
|
|
|
|
|
|
include Matrix::ConversionHelper
|
|
|
|
|
|
|
|
def l
|
2013-01-13 17:13:12 -05:00
|
|
|
Matrix.build(@row_count, [@column_count, @row_count].min) do |i, j|
|
2011-07-01 02:23:12 -04:00
|
|
|
if (i > j)
|
|
|
|
@lu[i][j]
|
|
|
|
elsif (i == j)
|
|
|
|
1
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the upper triangular factor +U+
|
|
|
|
|
|
|
|
def u
|
2013-01-13 17:13:12 -05:00
|
|
|
Matrix.build([@column_count, @row_count].min, @column_count) do |i, j|
|
2011-07-01 02:23:12 -04:00
|
|
|
if (i <= j)
|
|
|
|
@lu[i][j]
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the permutation matrix +P+
|
|
|
|
|
|
|
|
def p
|
2013-01-13 17:13:12 -05:00
|
|
|
rows = Array.new(@row_count){Array.new(@row_count, 0)}
|
2011-07-01 02:23:12 -04:00
|
|
|
@pivots.each_with_index{|p, i| rows[i][p] = 1}
|
2013-01-13 17:13:12 -05:00
|
|
|
Matrix.send :new, rows, @row_count
|
2011-07-01 02:23:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns +L+, +U+, +P+ in an array
|
|
|
|
|
|
|
|
def to_ary
|
|
|
|
[l, u, p]
|
|
|
|
end
|
|
|
|
alias_method :to_a, :to_ary
|
|
|
|
|
|
|
|
# Returns the pivoting indices
|
|
|
|
|
|
|
|
attr_reader :pivots
|
|
|
|
|
|
|
|
# Returns +true+ if +U+, and hence +A+, is singular.
|
|
|
|
|
2016-11-29 11:06:54 -05:00
|
|
|
def singular?
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |j|
|
2011-07-01 02:23:12 -04:00
|
|
|
if (@lu[j][j] == 0)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the determinant of +A+, calculated efficiently
|
|
|
|
# from the factorization.
|
|
|
|
|
|
|
|
def det
|
2012-12-10 11:53:57 -05:00
|
|
|
if (@row_count != @column_count)
|
2019-11-29 03:29:21 -05:00
|
|
|
raise Matrix::ErrDimensionMismatch
|
2011-07-01 02:23:12 -04:00
|
|
|
end
|
|
|
|
d = @pivot_sign
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |j|
|
2011-07-01 02:23:12 -04:00
|
|
|
d *= @lu[j][j]
|
|
|
|
end
|
|
|
|
d
|
|
|
|
end
|
|
|
|
alias_method :determinant, :det
|
|
|
|
|
|
|
|
# Returns +m+ so that <tt>A*m = b</tt>,
|
|
|
|
# or equivalently so that <tt>L*U*m = P*b</tt>
|
|
|
|
# +b+ can be a Matrix or a Vector
|
|
|
|
|
|
|
|
def solve b
|
|
|
|
if (singular?)
|
2019-11-29 03:29:21 -05:00
|
|
|
raise Matrix::ErrNotRegular, "Matrix is singular."
|
2011-07-01 02:23:12 -04:00
|
|
|
end
|
|
|
|
if b.is_a? Matrix
|
2012-12-10 11:53:57 -05:00
|
|
|
if (b.row_count != @row_count)
|
2019-11-29 03:29:21 -05:00
|
|
|
raise Matrix::ErrDimensionMismatch
|
2011-07-01 02:23:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Copy right hand side with pivoting
|
2012-12-10 11:53:57 -05:00
|
|
|
nx = b.column_count
|
2011-07-01 02:23:12 -04:00
|
|
|
m = @pivots.map{|row| b.row(row).to_a}
|
|
|
|
|
|
|
|
# Solve L*Y = P*b
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |k|
|
|
|
|
(k+1).upto(@column_count-1) do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
nx.times do |j|
|
|
|
|
m[i][j] -= m[k][j]*@lu[i][k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# Solve U*m = Y
|
2012-12-10 11:53:57 -05:00
|
|
|
(@column_count-1).downto(0) do |k|
|
2011-07-01 02:23:12 -04:00
|
|
|
nx.times do |j|
|
|
|
|
m[k][j] = m[k][j].quo(@lu[k][k])
|
|
|
|
end
|
|
|
|
k.times do |i|
|
|
|
|
nx.times do |j|
|
|
|
|
m[i][j] -= m[k][j]*@lu[i][k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Matrix.send :new, m, nx
|
|
|
|
else # same algorithm, specialized for simpler case of a vector
|
|
|
|
b = convert_to_array(b)
|
2012-12-10 11:53:57 -05:00
|
|
|
if (b.size != @row_count)
|
2019-11-29 03:29:21 -05:00
|
|
|
raise Matrix::ErrDimensionMismatch
|
2011-07-01 02:23:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Copy right hand side with pivoting
|
|
|
|
m = b.values_at(*@pivots)
|
|
|
|
|
|
|
|
# Solve L*Y = P*b
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |k|
|
|
|
|
(k+1).upto(@column_count-1) do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
m[i] -= m[k]*@lu[i][k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# Solve U*m = Y
|
2012-12-10 11:53:57 -05:00
|
|
|
(@column_count-1).downto(0) do |k|
|
2011-07-01 02:23:12 -04:00
|
|
|
m[k] = m[k].quo(@lu[k][k])
|
|
|
|
k.times do |i|
|
|
|
|
m[i] -= m[k]*@lu[i][k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Vector.elements(m, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize a
|
|
|
|
raise TypeError, "Expected Matrix but got #{a.class}" unless a.is_a?(Matrix)
|
|
|
|
# Use a "left-looking", dot-product, Crout/Doolittle algorithm.
|
|
|
|
@lu = a.to_a
|
2012-12-10 11:53:57 -05:00
|
|
|
@row_count = a.row_count
|
|
|
|
@column_count = a.column_count
|
|
|
|
@pivots = Array.new(@row_count)
|
|
|
|
@row_count.times do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
@pivots[i] = i
|
|
|
|
end
|
|
|
|
@pivot_sign = 1
|
2012-12-10 11:53:57 -05:00
|
|
|
lu_col_j = Array.new(@row_count)
|
2011-07-01 02:23:12 -04:00
|
|
|
|
|
|
|
# Outer loop.
|
|
|
|
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |j|
|
2011-07-01 02:23:12 -04:00
|
|
|
|
|
|
|
# Make a copy of the j-th column to localize references.
|
|
|
|
|
2012-12-10 11:53:57 -05:00
|
|
|
@row_count.times do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
lu_col_j[i] = @lu[i][j]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Apply previous transformations.
|
|
|
|
|
2012-12-10 11:53:57 -05:00
|
|
|
@row_count.times do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
lu_row_i = @lu[i]
|
|
|
|
|
|
|
|
# Most of the time is spent in the following dot product.
|
|
|
|
|
|
|
|
kmax = [i, j].min
|
|
|
|
s = 0
|
|
|
|
kmax.times do |k|
|
|
|
|
s += lu_row_i[k]*lu_col_j[k]
|
|
|
|
end
|
|
|
|
|
|
|
|
lu_row_i[j] = lu_col_j[i] -= s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find pivot and exchange if necessary.
|
|
|
|
|
|
|
|
p = j
|
2012-12-10 11:53:57 -05:00
|
|
|
(j+1).upto(@row_count-1) do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
if (lu_col_j[i].abs > lu_col_j[p].abs)
|
|
|
|
p = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if (p != j)
|
2012-12-10 11:53:57 -05:00
|
|
|
@column_count.times do |k|
|
2011-07-01 02:23:12 -04:00
|
|
|
t = @lu[p][k]; @lu[p][k] = @lu[j][k]; @lu[j][k] = t
|
|
|
|
end
|
|
|
|
k = @pivots[p]; @pivots[p] = @pivots[j]; @pivots[j] = k
|
|
|
|
@pivot_sign = -@pivot_sign
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compute multipliers.
|
|
|
|
|
2012-12-10 11:53:57 -05:00
|
|
|
if (j < @row_count && @lu[j][j] != 0)
|
|
|
|
(j+1).upto(@row_count-1) do |i|
|
2011-07-01 02:23:12 -04:00
|
|
|
@lu[i][j] = @lu[i][j].quo(@lu[j][j])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|