diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-04-16 21:51:01 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-04-16 21:51:01 +0000 |
commit | 75c1cac7f306627ce2a96faa0ca17ffb039bd762 (patch) | |
tree | 9bbde912ce8a99ce68eec4a5258b75f3a686e408 /lib/matrix.rb | |
parent | c89882197ed679e89b120dd328cd053d7ad56094 (diff) |
* {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@27366 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r-- | lib/matrix.rb | 170 |
1 files changed, 85 insertions, 85 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index 93ae3a9909..2f0f1b9d68 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1,6 +1,6 @@ #!/usr/local/bin/ruby #-- -# matrix.rb - +# matrix.rb - # $Release Version: 1.0$ # $Revision: 1.11 $ # $Date: 1999/10/06 11:01:53 $ @@ -14,9 +14,9 @@ # An implementation of Matrix and Vector classes. # # Author:: Keiju ISHITSUKA -# Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly)) +# Documentation:: Gavin Sinclair (sourced from <i>Ruby in a Nutshell</i> (Matsumoto, O'Reilly)) # -# See classes Matrix and Vector for documentation. +# See classes Matrix and Vector for documentation. # @@ -26,7 +26,7 @@ module ExceptionForMatrix # :nodoc: extend Exception2MessageMapper def_e2message(TypeError, "wrong argument type %s (expected %s)") def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)") - + def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch") def_exception("ErrNotRegular", "Not Regular Matrix") def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined") @@ -60,7 +60,7 @@ end # * <tt> Matrix.row_vector(row) </tt> # * <tt> Matrix.column_vector(column) </tt> # -# To access Matrix elements/columns/rows/submatrices/properties: +# To access Matrix elements/columns/rows/submatrices/properties: # * <tt> [](i, j) </tt> # * <tt> #row_size </tt> # * <tt> #column_size </tt> @@ -105,13 +105,13 @@ end # class Matrix @RCS_ID='-$Id: matrix.rb,v 1.11 1999/10/06 11:01:53 keiju Exp keiju $-' - + # extend Exception2MessageMapper include ExceptionForMatrix - + # instance creations private_class_method :new - + # # Creates a matrix where each argument is a row. # Matrix[ [25, 93], [-1, 66] ] @@ -121,7 +121,7 @@ class Matrix def Matrix.[](*rows) new(:init_rows, rows, false) end - + # # Creates a matrix where +rows+ is an array of arrays, each of which is a row # of the matrix. If the optional argument +copy+ is false, use the given @@ -133,7 +133,7 @@ class Matrix def Matrix.rows(rows, copy = true) new(:init_rows, rows, copy) end - + # # Creates a matrix using +columns+ as an array of column vectors. # Matrix.columns([[25, 93], [-1, 66]]) @@ -148,7 +148,7 @@ class Matrix } Matrix.rows(rows, false) end - + # # Creates a matrix where the diagonal elements are composed of +values+. # Matrix.diagonal(9, 5, -3) @@ -165,7 +165,7 @@ class Matrix } rows(rows, false) end - + # # Creates an +n+ by +n+ diagonal matrix where each diagonal element is # +value+. @@ -186,11 +186,11 @@ class Matrix def Matrix.identity(n) Matrix.scalar(n, 1) end - class << Matrix + class << Matrix alias unit identity alias I identity end - + # # Creates an +n+ by +n+ zero matrix. # Matrix.zero(2) @@ -200,7 +200,7 @@ class Matrix def Matrix.zero(n) Matrix.scalar(n, 0) end - + # # Creates a single-row matrix where the values of that row are as given in # +row+. @@ -217,7 +217,7 @@ class Matrix Matrix.rows([[row]], false) end end - + # # Creates a single-column matrix where the values of that column are as given # in +column+. @@ -244,7 +244,7 @@ class Matrix def initialize(init_method, *argv) self.send(init_method, *argv) end - + def init_rows(rows, copy) if copy @rows = rows.collect{|row| row.dup} @@ -254,7 +254,7 @@ class Matrix self end private :init_rows - + # # Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+. # @@ -268,7 +268,7 @@ class Matrix def row_size @rows.size end - + # # Returns the number of columns. Note that it is possible to construct a # matrix with uneven columns (e.g. Matrix[ [1,2,3], [4,5] ]), but this is @@ -308,7 +308,7 @@ class Matrix Vector.elements(col, false) end end - + # # Returns a matrix that is the result of iteration of the given block over all # elements of the matrix. @@ -321,7 +321,7 @@ class Matrix Matrix.rows(rows, false) end alias map collect - + # # Returns a section of the matrix. The parameters are either: # * start_row, nrows, start_col, ncols; OR @@ -345,14 +345,14 @@ class Matrix else Matrix.Raise ArgumentError, param.inspect end - + rows = @rows[from_row, size_row].collect{ |row| row[from_col, size_col] } Matrix.rows(rows, false) end - + #-- # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ @@ -363,7 +363,7 @@ class Matrix def regular? square? and rank == column_size end - + # # Returns +true+ is this is a singular (i.e. non-regular) matrix. # @@ -378,7 +378,7 @@ class Matrix def square? column_size == row_size end - + #-- # OBJECT METHODS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ @@ -388,15 +388,15 @@ class Matrix # def ==(other) return false unless Matrix === other - + other.compare_by_row_vectors(@rows) end def eql?(other) return false unless Matrix === other - + other.compare_by_row_vectors(@rows, :eql?) end - + # # Not really intended for general consumption. # @@ -408,7 +408,7 @@ class Matrix end true end - + # # Returns a clone of the matrix, so that the contents of each do not reference # identical objects. @@ -416,18 +416,18 @@ class Matrix def clone Matrix.rows(@rows) end - + # # Returns a hash-code for the matrix. # def hash @rows.hash end - + #-- # ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Matrix multiplication. # Matrix[[2,4], [6,8]] * Matrix.identity(2) @@ -465,7 +465,7 @@ class Matrix return x * y end end - + # # Matrix addition. # Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]] @@ -483,7 +483,7 @@ class Matrix x, y = m.coerce(self) return x + y end - + Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size rows = (0 ... row_size).collect {|i| @@ -511,7 +511,7 @@ class Matrix x, y = m.coerce(self) return x - y end - + Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size rows = (0 ... row_size).collect {|i| @@ -521,7 +521,7 @@ class Matrix } Matrix.rows(rows, false) end - + # # Matrix division (multiplication by the inverse). # Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]] @@ -606,7 +606,7 @@ class Matrix self end #alias reciprocal inverse - + # # Matrix exponentiation. Defined for integer powers only. Equivalent to # multiplying the matrix by itself N times. @@ -640,11 +640,11 @@ class Matrix Matrix.Raise ErrOperationNotDefined, "**" end end - + #-- # MATRIX FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Returns the determinant of the matrix. If the matrix is not square, the # result is 0. @@ -656,7 +656,7 @@ class Matrix size = row_size a = to_a - + det = 1 size.times do |k| if (akk = a[k][k]) == 0 @@ -741,7 +741,7 @@ class Matrix end end alias tr trace - + # # Returns the transpose of the matrix. # Matrix[[1,2], [3,4], [5,6]] @@ -756,11 +756,11 @@ class Matrix Matrix.columns(@rows) end alias t transpose - + #-- # CONVERTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # FIXME: describe #coerce. # @@ -781,7 +781,7 @@ class Matrix row(i) } end - + # # Returns an array of the column vectors of the matrix. See Vector. # @@ -790,18 +790,18 @@ class Matrix column(i) } end - + # # Returns an array of arrays that describe the rows of the matrix. # def to_a @rows.collect{|row| row.dup} end - + #-- # PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Overrides Object#to_s # @@ -811,23 +811,23 @@ class Matrix "[" + row.collect{|e| e.to_s}.join(", ") + "]" }.join(", ")+"]" end - + # # Overrides Object#inspect # def inspect "Matrix"[email protected] end - + # Private CLASS - + class Scalar < Numeric # :nodoc: include ExceptionForMatrix - + def initialize(value) @value = value end - + # ARITHMETIC def +(other) case other @@ -842,7 +842,7 @@ class Matrix x + y end end - + def -(other) case other when Numeric @@ -856,7 +856,7 @@ class Matrix x - y end end - + def *(other) case other when Numeric @@ -868,7 +868,7 @@ class Matrix x * y end end - + def / (other) case other when Numeric @@ -882,7 +882,7 @@ class Matrix x / y end end - + def ** (other) case other when Numeric @@ -941,9 +941,9 @@ end # class Vector include ExceptionForMatrix - + #INSTANCE CREATION - + private_class_method :new # @@ -953,7 +953,7 @@ class Vector def Vector.[](*array) new(:init_elements, array, copy = false) end - + # # Creates a vector from an Array. The optional second argument specifies # whether the array itself or a copy is used internally. @@ -961,14 +961,14 @@ class Vector def Vector.elements(array, copy = true) new(:init_elements, array, copy) end - + # # For internal use. # def initialize(method, array, copy) self.send(method, array, copy) end - + # # For internal use. # @@ -979,23 +979,23 @@ class Vector @elements = array end end - + # ACCESSING - + # # Returns element number +i+ (starting at zero) of the vector. # def [](i) @elements[i] end - + # # Returns the number of elements in the vector. # def size @elements.size end - + #-- # ENUMERATIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ @@ -1009,7 +1009,7 @@ class Vector yield @elements[i], v[i] end end - + # # Collects (as in Enumerable#collect) over the elements of this vector and +v+ # in conjunction. @@ -1030,40 +1030,40 @@ class Vector # def ==(other) return false unless Vector === other - + other.compare_by(@elements) end def eql?(other) return false unless Vector === other - + other.compare_by(@elements, :eql?) end - + # # For internal use. # def compare_by(elements, comparison = :==) @elements.send(comparison, elements) end - + # # Return a copy of the vector. # def clone Vector.elements(@elements) end - + # # Return a hash-code for the vector. # def hash @elements.hash end - + #-- # ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Multiplies the vector by +x+, where +x+ is a number or another vector. # @@ -1119,18 +1119,18 @@ class Vector s - x end end - + #-- # VECTOR FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Returns the inner product of this vector with the other. # Vector[4,7].inner_product Vector[10,1] => 47 # def inner_product(v) Vector.Raise ErrDimensionMismatch if size != v.size - + p = 0 each2(v) { |v1, v2| @@ -1138,7 +1138,7 @@ class Vector } p end - + # # Like Array#collect. # @@ -1147,7 +1147,7 @@ class Vector Vector.elements(els, false) end alias map collect - + # # Like Vector#collect2, but returns a Vector instead of an Array. # @@ -1155,7 +1155,7 @@ class Vector els = collect2(v, &block) Vector.elements(els, false) end - + # # Returns the modulus (Pythagorean distance) of the vector. # Vector[5,8,2].r => 9.643650761 @@ -1163,7 +1163,7 @@ class Vector def r Math.sqrt(@elements.inject(0) {|v, e| v + e*e}) end - + #-- # CONVERTING #++ @@ -1174,14 +1174,14 @@ class Vector def covector Matrix.row_vector(self) end - + # # Returns the elements of the vector in an array. # def to_a @elements.dup end - + # # FIXME: describe Vector#coerce. # @@ -1193,18 +1193,18 @@ class Vector raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end - + #-- # PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ - + # # Overrides Object#to_s # def to_s "Vector[" + @elements.join(", ") + "]" end - + # # Overrides Object#inspect # |