summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-01 18:05:11 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-04-01 18:05:11 +0000
commit729941da390cb317feb6772204d1630a21705c12 (patch)
tree7298f5326a91b03d364c1ce93cb2bc42c446edcb /lib
parent277cb36b21f8b5cb93e6b4373b23449bea917416 (diff)
* 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
Diffstat (limited to 'lib')
-rw-r--r--lib/matrix.rb73
1 files changed, 73 insertions, 0 deletions
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
# * <tt> #minor(*param) </tt>
#
# Properties of a matrix:
+# * <tt> #real? </tt>
# * <tt> #regular? </tt>
# * <tt> #singular? </tt>
# * <tt> #square? </tt>
@@ -92,6 +93,15 @@ end
# * <tt> #transpose </tt>
# * <tt> #t </tt>
#
+# Complex arithmetic:
+# * <tt> conj </tt>
+# * <tt> conjugate </tt>
+# * <tt> imag </tt>
+# * <tt> imaginary </tt>
+# * <tt> real </tt>
+# * <tt> rect </tt>
+# * <tt> rectangular </tt>
+#
# Conversion to other data types:
# * <tt> #coerce(other) </tt>
# * <tt> #row_vectors </tt>
@@ -435,6 +445,13 @@ class Matrix
end
#
+ # Returns +true+ if all entries of the matrix are real.
+ #
+ def real?
+ all?(&:real?)
+ end
+
+ #
# Returns +true+ if this is a regular matrix.
#
def regular?
@@ -898,6 +915,62 @@ class Matrix
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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++