solve(b)
public
Returns m so that A*m = b, or equivalently so that
L*U*m = P*b b can be a Matrix
or a Vector
Show source
def solve b
if (singular?)
Matrix.Raise Matrix::ErrNotRegular, "Matrix is singular."
end
if b.is_a? Matrix
if (b.row_count != @row_count)
Matrix.Raise Matrix::ErrDimensionMismatch
end
nx = b.column_count
m = @pivots.map{|row| b.row(row).to_a}
@column_count.times do |k|
(k+1).upto(@column_count-1) do |i|
nx.times do |j|
m[i][j] -= m[k][j]*@lu[i][k]
end
end
end
(@column_count-1).downto(0) do |k|
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
b = convert_to_array(b)
if (b.size != @row_count)
Matrix.Raise Matrix::ErrDimensionMismatch
end
m = b.values_at(*@pivots)
@column_count.times do |k|
(k+1).upto(@column_count-1) do |i|
m[i] -= m[k]*@lu[i][k]
end
end
(@column_count-1).downto(0) do |k|
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