mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/matrix: alias {row|column}_size to {row|column}_count and use the latter.
[Bug #7369] [ruby-core:49409] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38300 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4c02cff191
commit
8aac5f48fc
4 changed files with 124 additions and 116 deletions
|
@ -19,7 +19,7 @@ class Matrix
|
|||
include Matrix::ConversionHelper
|
||||
|
||||
def l
|
||||
Matrix.build(@row_size, @col_size) do |i, j|
|
||||
Matrix.build(@row_count, @column_count) do |i, j|
|
||||
if (i > j)
|
||||
@lu[i][j]
|
||||
elsif (i == j)
|
||||
|
@ -33,7 +33,7 @@ class Matrix
|
|||
# Returns the upper triangular factor +U+
|
||||
|
||||
def u
|
||||
Matrix.build(@col_size, @col_size) do |i, j|
|
||||
Matrix.build(@column_count, @column_count) do |i, j|
|
||||
if (i <= j)
|
||||
@lu[i][j]
|
||||
else
|
||||
|
@ -45,9 +45,9 @@ class Matrix
|
|||
# Returns the permutation matrix +P+
|
||||
|
||||
def p
|
||||
rows = Array.new(@row_size){Array.new(@col_size, 0)}
|
||||
rows = Array.new(@row_count){Array.new(@column_count, 0)}
|
||||
@pivots.each_with_index{|p, i| rows[i][p] = 1}
|
||||
Matrix.send :new, rows, @col_size
|
||||
Matrix.send :new, rows, @column_count
|
||||
end
|
||||
|
||||
# Returns +L+, +U+, +P+ in an array
|
||||
|
@ -64,7 +64,7 @@ class Matrix
|
|||
# Returns +true+ if +U+, and hence +A+, is singular.
|
||||
|
||||
def singular? ()
|
||||
@col_size.times do |j|
|
||||
@column_count.times do |j|
|
||||
if (@lu[j][j] == 0)
|
||||
return true
|
||||
end
|
||||
|
@ -76,11 +76,11 @@ class Matrix
|
|||
# from the factorization.
|
||||
|
||||
def det
|
||||
if (@row_size != @col_size)
|
||||
if (@row_count != @column_count)
|
||||
Matrix.Raise Matrix::ErrDimensionMismatch unless square?
|
||||
end
|
||||
d = @pivot_sign
|
||||
@col_size.times do |j|
|
||||
@column_count.times do |j|
|
||||
d *= @lu[j][j]
|
||||
end
|
||||
d
|
||||
|
@ -96,24 +96,24 @@ class Matrix
|
|||
Matrix.Raise Matrix::ErrNotRegular, "Matrix is singular."
|
||||
end
|
||||
if b.is_a? Matrix
|
||||
if (b.row_size != @row_size)
|
||||
if (b.row_count != @row_count)
|
||||
Matrix.Raise Matrix::ErrDimensionMismatch
|
||||
end
|
||||
|
||||
# Copy right hand side with pivoting
|
||||
nx = b.column_size
|
||||
nx = b.column_count
|
||||
m = @pivots.map{|row| b.row(row).to_a}
|
||||
|
||||
# Solve L*Y = P*b
|
||||
@col_size.times do |k|
|
||||
(k+1).upto(@col_size-1) do |i|
|
||||
@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
|
||||
# Solve U*m = Y
|
||||
(@col_size-1).downto(0) do |k|
|
||||
(@column_count-1).downto(0) do |k|
|
||||
nx.times do |j|
|
||||
m[k][j] = m[k][j].quo(@lu[k][k])
|
||||
end
|
||||
|
@ -126,7 +126,7 @@ class Matrix
|
|||
Matrix.send :new, m, nx
|
||||
else # same algorithm, specialized for simpler case of a vector
|
||||
b = convert_to_array(b)
|
||||
if (b.size != @row_size)
|
||||
if (b.size != @row_count)
|
||||
Matrix.Raise Matrix::ErrDimensionMismatch
|
||||
end
|
||||
|
||||
|
@ -134,13 +134,13 @@ class Matrix
|
|||
m = b.values_at(*@pivots)
|
||||
|
||||
# Solve L*Y = P*b
|
||||
@col_size.times do |k|
|
||||
(k+1).upto(@col_size-1) do |i|
|
||||
@column_count.times do |k|
|
||||
(k+1).upto(@column_count-1) do |i|
|
||||
m[i] -= m[k]*@lu[i][k]
|
||||
end
|
||||
end
|
||||
# Solve U*m = Y
|
||||
(@col_size-1).downto(0) do |k|
|
||||
(@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]
|
||||
|
@ -154,28 +154,28 @@ class Matrix
|
|||
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
|
||||
@row_size = a.row_size
|
||||
@col_size = a.column_size
|
||||
@pivots = Array.new(@row_size)
|
||||
@row_size.times do |i|
|
||||
@row_count = a.row_count
|
||||
@column_count = a.column_count
|
||||
@pivots = Array.new(@row_count)
|
||||
@row_count.times do |i|
|
||||
@pivots[i] = i
|
||||
end
|
||||
@pivot_sign = 1
|
||||
lu_col_j = Array.new(@row_size)
|
||||
lu_col_j = Array.new(@row_count)
|
||||
|
||||
# Outer loop.
|
||||
|
||||
@col_size.times do |j|
|
||||
@column_count.times do |j|
|
||||
|
||||
# Make a copy of the j-th column to localize references.
|
||||
|
||||
@row_size.times do |i|
|
||||
@row_count.times do |i|
|
||||
lu_col_j[i] = @lu[i][j]
|
||||
end
|
||||
|
||||
# Apply previous transformations.
|
||||
|
||||
@row_size.times do |i|
|
||||
@row_count.times do |i|
|
||||
lu_row_i = @lu[i]
|
||||
|
||||
# Most of the time is spent in the following dot product.
|
||||
|
@ -192,13 +192,13 @@ class Matrix
|
|||
# Find pivot and exchange if necessary.
|
||||
|
||||
p = j
|
||||
(j+1).upto(@row_size-1) do |i|
|
||||
(j+1).upto(@row_count-1) do |i|
|
||||
if (lu_col_j[i].abs > lu_col_j[p].abs)
|
||||
p = i
|
||||
end
|
||||
end
|
||||
if (p != j)
|
||||
@col_size.times do |k|
|
||||
@column_count.times do |k|
|
||||
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
|
||||
|
@ -207,8 +207,8 @@ class Matrix
|
|||
|
||||
# Compute multipliers.
|
||||
|
||||
if (j < @row_size && @lu[j][j] != 0)
|
||||
(j+1).upto(@row_size-1) do |i|
|
||||
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])
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue