# frozen_string_literal: false
# 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.
# Returns the lower triangular factor +L+
include Matrix::ConversionHelper
Matrix.build(@row_count, [@column_count, @row_count].min) do |i, j|
# Returns the upper triangular factor +U+
Matrix.build([@column_count, @row_count].min, @column_count) do |i, j|
# Returns the permutation matrix +P+
rows = Array.new(@row_count){Array.new(@row_count, 0)}
@pivots.each_with_index{|p, i| rows[i][p] = 1}
Matrix.send :new, rows, @row_count
# Returns +L+, +U+, +P+ in an array
alias_method :to_a, :to_ary
# Returns the pivoting indices
# Returns +true+ if +U+, and hence +A+, is singular.
@column_count.times do |j|
# Returns the determinant of +A+, calculated efficiently
# from the factorization.
if (@row_count != @column_count)
Matrix.Raise Matrix::ErrDimensionMismatch
@column_count.times do |j|
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
Matrix.Raise Matrix::ErrNotRegular, "Matrix is singular."
if (b.row_count != @row_count)
Matrix.Raise Matrix::ErrDimensionMismatch
# Copy right hand side with pivoting
m = @pivots.map{|row| b.row(row).to_a}
@column_count.times do |k|
(k+1).upto(@column_count-1) do |i|
m[i][j] -= m[k][j]*@lu[i][k]
(@column_count-1).downto(0) do |k|
m[k][j] = m[k][j].quo(@lu[k][k])
m[i][j] -= m[k][j]*@lu[i][k]
else # same algorithm, specialized for simpler case of a vector
if (b.size != @row_count)
Matrix.Raise Matrix::ErrDimensionMismatch
# Copy right hand side with pivoting
m = b.values_at(*@pivots)
@column_count.times do |k|
(k+1).upto(@column_count-1) do |i|
(@column_count-1).downto(0) do |k|
m[k] = m[k].quo(@lu[k][k])
Vector.elements(m, false)
raise TypeError, "Expected Matrix but got #{a.class}" unless a.is_a?(Matrix)
# Use a "left-looking", dot-product, Crout/Doolittle algorithm.
@column_count = a.column_count
@pivots = Array.new(@row_count)
lu_col_j = Array.new(@row_count)
@column_count.times do |j|
# Make a copy of the j-th column to localize references.
# Apply previous transformations.
# Most of the time is spent in the following dot product.
s += lu_row_i[k]*lu_col_j[k]
lu_row_i[j] = lu_col_j[i] -= s
# Find pivot and exchange if necessary.
(j+1).upto(@row_count-1) do |i|
if (lu_col_j[i].abs > lu_col_j[p].abs)
@column_count.times do |k|
t = @lu[p][k]; @lu[p][k] = @lu[j][k]; @lu[j][k] = t
k = @pivots[p]; @pivots[p] = @pivots[j]; @pivots[j] = k
@pivot_sign = -@pivot_sign
if (j < @row_count && @lu[j][j] != 0)
(j+1).upto(@row_count-1) do |i|
@lu[i][j] = @lu[i][j].quo(@lu[j][j])