From 729941da390cb317feb6772204d1630a21705c12 Mon Sep 17 00:00:00 2001 From: marcandre Date: Thu, 1 Apr 2010 18:05:11 +0000 Subject: [PATCH] * lib/matrix.rb: New Complex instance methods: conjugate, conj, imaginary, imag, real, real?, rectangular, rect [ruby-core:26285] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27159 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 +++++++++ lib/matrix.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2ae023d6d4..3b1f8147ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Fri Apr 2 02:56:56 2010 Marc-Andre Lafortune + + * lib/matrix.rb: : New instance methods: + empty? [ruby-core:26284], each, each_with_index [ruby-core:28400], + conjugate, conj, imaginary, imag, real, real?, rectangular, rect + [ruby-core:26285] + + Removed compare_by*, inspect_org, cf [ruby-core:26268] + + Matrix.empty: raise on negative sizes + + Matrix.determinant: raise on rectangular matrices [ruby-core:28271] + Thu Apr 1 17:17:00 2010 NARUSE, Yui * enc/trans/iso2022.trans: CP50221 supports 8bit JIS. diff --git a/lib/matrix.rb b/lib/matrix.rb index d131426f1a..18f53b7316 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -70,6 +70,7 @@ end # * #minor(*param) # # Properties of a matrix: +# * #real? # * #regular? # * #singular? # * #square? @@ -92,6 +93,15 @@ end # * #transpose # * #t # +# Complex arithmetic: +# * conj +# * conjugate +# * imag +# * imaginary +# * real +# * rect +# * rectangular +# # Conversion to other data types: # * #coerce(other) # * #row_vectors @@ -434,6 +444,13 @@ class Matrix column_size == 0 || row_size == 0 end + # + # Returns +true+ if all entries of the matrix are real. + # + def real? + all?(&:real?) + end + # # Returns +true+ if this is a regular matrix. # @@ -897,6 +914,62 @@ class Matrix end alias t transpose + #-- + # COMPLEX ARITHMETIC -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + #++ + + # + # Returns the conjugate of the matrix. + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] + # => 1+2i i 0 + # 1 2 3 + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].conjugate + # => 1-2i -i 0 + # 1 2 3 + # + def conjugate + collect(&:conjugate) + end + alias conj conjugate + + # + # Returns the imaginary part of the matrix. + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] + # => 1+2i i 0 + # 1 2 3 + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].imaginary + # => 2i i 0 + # 0 0 0 + # + def imaginary + collect(&:imaginary) + end + alias imag imaginary + + # + # Returns the real part of the matrix. + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] + # => 1+2i i 0 + # 1 2 3 + # Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].real + # => 1 0 0 + # 1 2 3 + # + def real + collect(&:real) + end + + # + # Returns an array containing matrices corresponding to the real and imaginary + # parts of the matrix + # + # m.rect == [m.real, m.imag] # ==> true for all matrices m + # + def rect + [real, imag] + end + alias rectangular rect + #-- # CONVERTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++