ruby-changes:26243
From: marcandre <ko1@a...>
Date: Tue, 11 Dec 2012 01:54:08 +0900 (JST)
Subject: [ruby-changes:26243] marcandRe: r38300 (trunk): * lib/matrix: alias {row|column}_size to {row|column}_count and use the latter.
marcandre 2012-12-11 01:53:57 +0900 (Tue, 11 Dec 2012) New Revision: 38300 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38300 Log: * lib/matrix: alias {row|column}_size to {row|column}_count and use the latter. [Bug #7369] [ruby-core:49409] Modified files: trunk/ChangeLog trunk/lib/matrix/eigenvalue_decomposition.rb trunk/lib/matrix/lup_decomposition.rb trunk/lib/matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38299) +++ ChangeLog (revision 38300) @@ -1,3 +1,9 @@ +Tue Dec 11 01:53:37 2012 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix: alias {row|column}_size to {row|column}_count and use + the latter. + [Bug #7369] [ruby-core:49409] + Tue Dec 11 00:26:58 2012 Shugo Maeda <shugo@r...> * fix the behavior when a module is included into a refinement. Index: lib/matrix/eigenvalue_decomposition.rb =================================================================== --- lib/matrix/eigenvalue_decomposition.rb (revision 38299) +++ lib/matrix/eigenvalue_decomposition.rb (revision 38300) @@ -20,7 +20,7 @@ # @v: Array for internal storage of eigenvectors. # @h: Array for internal storage of nonsymmetric Hessenberg form. raise TypeError, "Expected Matrix but got #{a.class}" unless a.is_a?(Matrix) - @size = a.row_size + @size = a.row_count @d = Array.new(@size, 0) @e = Array.new(@size, 0) Index: lib/matrix/lup_decomposition.rb =================================================================== --- lib/matrix/lup_decomposition.rb (revision 38299) +++ lib/matrix/lup_decomposition.rb (revision 38300) @@ -19,7 +19,7 @@ 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 @@ # 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 @@ # 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 @@ # 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 @@ # 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ # 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 @@ # 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 Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 38299) +++ lib/matrix.rb (revision 38300) @@ -36,7 +36,7 @@ # * Matrix.[](*rows) # * Matrix.rows(rows, copy = true) # * Matrix.columns(columns) -# * Matrix.build(row_size, column_size, &block) +# * Matrix.build(row_count, column_count, &block) # * Matrix.diagonal(*values) # * Matrix.scalar(n, value) # * Matrix.identity(n) @@ -48,8 +48,8 @@ # # To access Matrix elements/columns/rows/submatrices/properties: # * #[](i, j) -# * #row_size -# * #column_size +# * #row_count (row_size) +# * #column_count (column_size) # * #row(i) # * #column(j) # * #collect @@ -172,7 +172,7 @@ end # - # Creates a matrix of size +row_size+ x +column_size+. + # Creates a matrix of size +row_count+ x +column_count+. # It fills the values by calling the given block, # passing the current row and column. # Returns an enumerator if no block is given. @@ -182,17 +182,17 @@ # m = Matrix.build(3) { rand } # => a 3x3 matrix with random elements # - def Matrix.build(row_size, column_size = row_size) - row_size = CoercionHelper.coerce_to_int(row_size) - column_size = CoercionHelper.coerce_to_int(column_size) - raise ArgumentError if row_size < 0 || column_size < 0 - return to_enum :build, row_size, column_size unless block_given? - rows = Array.new(row_size) do |i| - Array.new(column_size) do |j| + def Matrix.build(row_count, column_count = row_count) + row_count = CoercionHelper.coerce_to_int(row_count) + column_count = CoercionHelper.coerce_to_int(column_count) + raise ArgumentError if row_count < 0 || column_count < 0 + return to_enum :build, row_count, column_count unless block_given? + rows = Array.new(row_count) do |i| + Array.new(column_count) do |j| yield i, j end end - new rows, column_size + new rows, column_count end # @@ -243,9 +243,9 @@ # => 0 0 # 0 0 # - def Matrix.zero(row_size, column_size = row_size) - rows = Array.new(row_size){Array.new(column_size, 0)} - new rows, column_size + def Matrix.zero(row_count, column_count = row_count) + rows = Array.new(row_count){Array.new(column_count, 0)} + new rows, column_count end # @@ -273,8 +273,8 @@ end # - # Creates a empty matrix of +row_size+ x +column_size+. - # At least one of +row_size+ or +column_size+ must be 0. + # Creates a empty matrix of +row_count+ x +column_count+. + # At least one of +row_count+ or +column_count+ must be 0. # # m = Matrix.empty(2, 0) # m == Matrix[ [], [] ] @@ -285,26 +285,26 @@ # m * n # => Matrix[[0, 0, 0], [0, 0, 0]] # - def Matrix.empty(row_size = 0, column_size = 0) - Matrix.Raise ArgumentError, "One size must be 0" if column_size != 0 && row_size != 0 - Matrix.Raise ArgumentError, "Negative size" if column_size < 0 || row_size < 0 + def Matrix.empty(row_count = 0, column_count = 0) + Matrix.Raise ArgumentError, "One size must be 0" if column_count != 0 && row_count != 0 + Matrix.Raise ArgumentError, "Negative size" if column_count < 0 || row_count < 0 - new([[]]*row_size, column_size) + new([[]]*row_count, column_count) end # # Matrix.new is private; use Matrix.rows, columns, [], etc... to create. # - def initialize(rows, column_size = rows[0].size) + def initialize(rows, column_count = rows[0].size) # No checking is done at this point. rows must be an Array of Arrays. - # column_size must be the size of the first row, if there is one, + # column_count must be the size of the first row, if there is one, # otherwise it *must* be specified and can be any integer >= 0 @rows = rows - @column_size = column_size + @column_count = column_count end - def new_matrix(rows, column_size = rows[0].size) # :nodoc: - self.class.send(:new, rows, column_size) # bypass privacy of Matrix.new + def new_matrix(rows, column_count = rows[0].size) # :nodoc: + self.class.send(:new, rows, column_count) # bypass privacy of Matrix.new end private :new_matrix @@ -327,14 +327,16 @@ # # Returns the number of rows. # - def row_size + def row_count @rows.size end + alias_method :row_size, :row_count # # Returns the number of columns. # - attr_reader :column_size + attr_reader :column_count + alias_method :column_size, :column_count # # Returns row vector number +i+ of the matrix as a Vector (starting at 0 like @@ -356,14 +358,14 @@ # def column(j) # :yield: e if block_given? - return self if j >= column_size || j < -column_size - row_size.times do |i| + return self if j >= column_count || j < -column_count + row_count.times do |i| yield @rows[i][j] end self else - return nil if j >= column_size || j < -column_size - col = Array.new(row_size) {|i| + return nil if j >= column_count || j < -column_count + col = Array.new(row_count) {|i| @rows[i][j] } Vector.elements(col, false) @@ -380,7 +382,7 @@ def collect(&block) # :yield: e return to_enum(:collect) unless block_given? rows = @rows.collect{|row| row.collect(&block)} - new_matrix rows, column_size + new_matrix rows, column_count end alias map collect @@ -402,7 +404,7 @@ # def each(which = :all) # :yield: e return to_enum :each, which unless block_given? - last = column_size - 1 + last = column_count - 1 case which when :all block = Proc.new @@ -415,7 +417,7 @@ end when :off_diagonal @rows.each_with_index do |row, row_index| - column_size.times do |col_index| + column_count.times do |col_index| yield row[col_index] unless row_index == col_index end end @@ -427,7 +429,7 @@ end when :strict_lower @rows.each_with_index do |row, row_index| - [row_index, column_size].min.times do |col_index| + [row_index, column_count].min.times do |col_index| yield row[col_index] end end @@ -463,7 +465,7 @@ # def each_with_index(which = :all) # :yield: e, row, column return to_enum :each_with_index, which unless block_given? - last = column_size - 1 + last = column_count - 1 case which when :all @rows.each_with_index do |row, row_index| @@ -477,7 +479,7 @@ end when :off_diagonal @rows.each_with_index do |row, row_index| - column_size.times do |col_index| + column_count.times do |col_index| yield row[col_index], row_index, col_index unless row_index == col_index end end @@ -489,7 +491,7 @@ end when :strict_lower @rows.each_with_index do |row, row_index| - [row_index, column_size].min.times do |col_index| + [row_index, column_count].min.times do |col_index| yield row[col_index], row_index, col_index end end @@ -552,39 +554,39 @@ # # Like Array#[], negative indices count backward from the end of the # row or column (-1 is the last element). Returns nil if the starting - # row or column is greater than row_size or column_size respectively. + # row or column is greater than row_count or column_count respectively. # def minor(*param) case param.size when 2 row_range, col_range = param from_row = row_range.first - from_row += row_size if from_row < 0 + from_row += row_count if from_row < 0 to_row = row_range.end - to_row += row_size if to_row < 0 + to_row += row_count if to_row < 0 to_row += 1 unless row_range.exclude_end? size_row = to_row - from_row from_col = col_range.first - from_col += column_size if from_col < 0 + from_col += column_count if from_col < 0 to_col = col_range.end - to_col += column_size if to_col < 0 + to_col += column_count if to_col < 0 to_col += 1 unless col_range.exclude_end? size_col = to_col - from_col when 4 from_row, size_row, from_col, size_col = param return nil if size_row < 0 || size_col < 0 - from_row += row_size if from_row < 0 - from_col += column_size if from_col < 0 + from_row += row_count if from_row < 0 + from_col += column_count if from_col < 0 else Matrix.Raise ArgumentError, param.inspect end - return nil if from_row > row_size || from_col > column_size || from_row < 0 || from_col < 0 + return nil if from_row > row_count || from_col > column_count || from_row < 0 || from_col < 0 rows = @rows[from_row, size_row].collect{|row| row[from_col, size_col] } - new_matrix rows, [column_size - from_col, size_col].min + new_matrix rows, [column_count - from_col, size_col].min end #-- @@ -605,7 +607,7 @@ # or the number of columns is 0. # def empty? - column_size == 0 || row_size == 0 + column_count == 0 || row_count == 0 end # @@ -651,9 +653,9 @@ def orthogonal? Matrix.Raise ErrDimensionMismatch unless square? rows.each_with_index do |row, i| - column_size.times do |j| + column_count.times do |j| s = 0 - row_size.times do |k| + row_count.times do |k| s += row[k] * rows[k][j] end return false unless s == (i == j ? 1 : 0) @@ -668,7 +670,7 @@ # def permutation? Matrix.Raise ErrDimensionMismatch unless square? - cols = Array.new(column_size) + cols = Array.new(column_count) rows.each_with_index do |row, i| found = false row.each_with_index do |e, j| @@ -709,7 +711,7 @@ # Returns +true+ is this is a square matrix. # def square? - column_size == row_size + column_count == row_count end # @@ -731,9 +733,9 @@ def unitary? Matrix.Raise ErrDimensionMismatch unless square? rows.each_with_index do |row, i| - column_size.times do |j| + column_count.times do |j| s = 0 - row_size.times do |k| + row_count.times do |k| s += row[k].conj * rows[k][j] end return false unless s == (i == j ? 1 : 0) @@ -765,13 +767,13 @@ # def ==(other) return false unless Matrix === other && - column_size == other.column_size # necessary for empty matrices + column_count == other.column_count # necessary for empty matrices rows == other.rows end def eql?(other) return false unless Matrix === other && - column_size == other.column_size # necessary for empty matrices + column_count == other.column_count # necessary for empty matrices rows.eql? other.rows end @@ -781,7 +783,7 @@ # There should be no good reason to do this since Matrices are immutable. # def clone - new_matrix @rows.map(&:dup), column_size + new_matrix @rows.map(&:dup), column_count end # @@ -807,22 +809,22 @@ rows = @rows.collect {|row| row.collect {|e| e * m } } - return new_matrix rows, column_size + return new_matrix rows, column_count when Vector m = self.class.column_vector(m) r = self * m return r.column(0) when Matrix - Matrix.Raise ErrDimensionMismatch if column_size != m.row_size + Matrix.Raise ErrDimensionMismatch if column_count != m.row_count - rows = Array.new(row_size) {|i| - Array.new(m.column_size) {|j| - (0 ... column_size).inject(0) do |vij, k| + rows = Array.new(row_count) {|i| + Array.new(m.column_count) {|j| + (0 ... column_count).inject(0) do |vij, k| vij + self[i, k] * m[k, j] end } } - return new_matrix rows, m.column_size + return new_matrix rows, m.column_count else return apply_through_coercion(m, __method__) end @@ -845,14 +847,14 @@ return apply_through_coercion(m, __method__) end - Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size + Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count and column_count == m.column_count - rows = Array.new(row_size) {|i| - Array.new(column_size) {|j| + rows = Array.new(row_count) {|i| + Array.new(column_count) {|j| self[i, j] + m[i, j] } } - new_matrix rows, column_size + new_matrix rows, column_count end # @@ -872,14 +874,14 @@ return apply_through_coercion(m, __method__) end - Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size + Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count and column_count == m.column_count - rows = Array.new(row_size) {|i| - Array.new(column_size) {|j| + rows = Array.new(row_count) {|i| + Array.new(column_count) {|j| self[i, j] - m[i, j] } } - new_matrix rows, column_size + new_matrix rows, column_count end # @@ -894,7 +896,7 @@ rows = @rows.collect {|row| row.collect {|e| e / other } } - return new_matrix rows, column_size + return new_matrix rows, column_count when Matrix return self * other.inverse else @@ -910,12 +912,12 @@ # def inverse Matrix.Raise ErrDimensionMismatch unless square? - self.class.I(row_size).send(:inverse_from, self) + self.class.I(row_count).send(:inverse_from, (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/