From 729941da390cb317feb6772204d1630a21705c12 Mon Sep 17 00:00:00 2001 From: marcandre Date: Thu, 1 Apr 2010 18:05:11 +0000 Subject: * 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 --- lib/matrix.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'lib/matrix.rb') 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ -- cgit v1.2.1