3 # $Release Version: 0.5 $
5 # $Date: 1998/07/08 10:05:28 $
6 # by Keiju ISHITSUKA(SHL Japan Inc.)
10 # complex.rb implements the Complex class for complex numbers. Additionally,
11 # some methods in other Numeric classes are redefined or added to allow greater
12 # interoperability with Complex numbers.
14 # Complex numbers can be created in the following manner:
15 # - <tt>Complex(a, b)</tt>
16 # - <tt>Complex.polar(radius, theta)</tt>
18 # Additionally, note the following:
19 # - <tt>Complex::I</tt> (the mathematical constant <i>i</i>)
20 # - <tt>Numeric#im</tt> (e.g. <tt>5.im -> 0+5i</tt>)
22 # The following +Math+ module methods are redefined to handle Complex arguments.
23 # They will work as normal with non-Complex arguments.
24 # sqrt exp cos sin tan log log10
25 # cosh sinh tanh acos asin atan atan2 acosh asinh atanh
30 # Numeric is a built-in class on which Fixnum, Bignum, etc., are based. Here
31 # some methods are added so that all number types can be treated to some extent
36 # Returns a Complex number <tt>(0,<i>self</i>)</tt>.
43 # The real part of a complex number, i.e. <i>self</i>.
50 # The imaginary part of a complex number, i.e. 0.
77 # See Complex#conjugate (short answer: returns <i>self</i>).
87 # Creates a Complex number. +a+ and +b+ should be Numeric. The result will be
91 if b == 0 and (a.kind_of?(Complex) or defined? Complex::Unify)
94 Complex.new( a.real-b.imag, a.imag+b.real )
99 # The complex number class. See complex.rb for an overview.
101 class Complex < Numeric
102 @RCS_ID='-$Id: complex.rb,v 1.3 1998/07/08 10:05:28 keiju Exp keiju $-'
106 undef floor, truncate, ceil, round
108 def Complex.generic?(other) # :nodoc:
109 other.kind_of?(Integer) or
110 other.kind_of?(Float) or
111 (defined?(Rational) and other.kind_of?(Rational))
115 # Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle).
117 def Complex.polar(r, theta)
118 Complex(r*Math.cos(theta), r*Math.sin(theta))
122 # Creates a +Complex+ number <tt>a</tt>+<tt>b</tt><i>i</i>.
124 def Complex.new!(a, b=0)
129 raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
130 raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
131 raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
132 raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
138 # Addition with real or complex number.
141 if other.kind_of?(Complex)
142 re = @real + other.real
143 im = @image + other.image
145 elsif Complex.generic?(other)
146 Complex(@real + other, @image)
148 x , y = other.coerce(self)
154 # Subtraction with real or complex number.
157 if other.kind_of?(Complex)
158 re = @real - other.real
159 im = @image - other.image
161 elsif Complex.generic?(other)
162 Complex(@real - other, @image)
164 x , y = other.coerce(self)
170 # Multiplication with real or complex number.
173 if other.kind_of?(Complex)
174 re = @real*other.real - @image*other.image
175 im = @real*other.image + @image*other.real
177 elsif Complex.generic?(other)
178 Complex(@real * other, @image * other)
180 x , y = other.coerce(self)
186 # Division by real or complex number.
189 if other.kind_of?(Complex)
190 self*other.conjugate/other.abs2
191 elsif Complex.generic?(other)
192 Complex(@real/other, @image/other)
194 x, y = other.coerce(self)
200 Complex(@real.quo(1), @image.quo(1)) / other
204 # Raise this complex number to the given (real or complex) power.
210 if other.kind_of?(Complex)
214 nr = Math.exp!(ore*Math.log!(r) - oim * theta)
215 ntheta = theta*ore + oim*Math.log!(r)
216 Complex.polar(nr, ntheta)
217 elsif other.kind_of?(Integer)
223 while (div, mod = n.divmod(2)
225 x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
234 (Rational(1) / self) ** -other
239 elsif Complex.generic?(other)
241 Complex.polar(r**other, theta*other)
243 x, y = other.coerce(self)
249 # Remainder after division by a real or complex number.
252 if other.kind_of?(Complex)
253 Complex(@real % other.real, @image % other.image)
254 elsif Complex.generic?(other)
255 Complex(@real % other, @image % other)
257 x , y = other.coerce(self)
264 # if other.kind_of?(Complex)
265 # rdiv, rmod = @real.divmod(other.real)
266 # idiv, imod = @image.divmod(other.image)
267 # return Complex(rdiv, idiv), Complex(rmod, rmod)
268 # elsif Complex.generic?(other)
269 # Complex(@real.divmod(other), @image.divmod(other))
271 # x , y = other.coerce(self)
278 # Absolute value (aka modulus): distance from the zero point on the complex
282 Math.hypot(@real, @image)
286 # Square of the absolute value.
289 @real*@real + @image*@image
293 # Argument (angle from (1,0) on the complex plane).
296 Math.atan2!(@image, @real)
301 # Returns the absolute value _and_ the argument.
308 # Complex conjugate (<tt>z + z.conjugate = 2 * z.real</tt>).
311 Complex(@real, -@image)
316 # Compares the absolute values of the two numbers.
319 self.abs <=> other.abs
323 # Test for numerical equality (<tt>a == a + 0<i>i</i></tt>).
326 if other.kind_of?(Complex)
327 @real == other.real and @image == other.image
328 elsif Complex.generic?(other)
329 @real == other and @image == 0
336 # Attempts to coerce +other+ to a Complex number.
339 if Complex.generic?(other)
340 return Complex.new!(other), self
350 @real.denominator.lcm(@image.denominator)
358 Complex(@real.numerator*(cd/@real.denominator),
359 @image.numerator*(cd/@image.denominator))
363 # Standard string representation of the complex number.
367 if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
369 @real.to_s+"+("+@image.to_s+")i"
371 @real.to_s+"-("+(-@image).to_s+")i"
375 @real.to_s+"+"+@image.to_s+"i"
377 @real.to_s+"-"+(-@image).to_s+"i"
381 if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
390 # Returns a hash code for the complex number.
393 @real.hash ^ @image.hash
397 # Returns "<tt>Complex(<i>real</i>, <i>image</i>)</tt>".
400 sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
405 # +I+ is the imaginary number. It exists at point (0,1) on the complex plane.
409 # The real part of a complex number.
412 # The imaginary part of a complex number.
420 unless defined?(1.numerator)
421 def numerator() self end
422 def denominator() 1 end
436 if self.zero? or other.zero?
439 (self.div(self.gcd(other)) * other).abs
466 # Redefined to handle a Complex argument.
468 if Complex.generic?(z)
476 sqrt(z.conjugate).conjugate
480 Complex( sqrt!((r+x)/2), sqrt!((r-x)/2) )
485 # Redefined to handle a Complex argument.
487 if Complex.generic?(z)
490 Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image))
494 # Redefined to handle a Complex argument.
496 if Complex.generic?(z)
499 Complex(cos!(z.real)*cosh!(z.image),
500 -sin!(z.real)*sinh!(z.image))
504 # Redefined to handle a Complex argument.
506 if Complex.generic?(z)
509 Complex(sin!(z.real)*cosh!(z.image),
510 cos!(z.real)*sinh!(z.image))
514 # Redefined to handle a Complex argument.
516 if Complex.generic?(z)
524 if Complex.generic?(z)
527 Complex( sinh!(z.real)*cos!(z.image), cosh!(z.real)*sin!(z.image) )
532 if Complex.generic?(z)
535 Complex( cosh!(z.real)*cos!(z.image), sinh!(z.real)*sin!(z.image) )
540 if Complex.generic?(z)
547 # Redefined to handle a Complex argument.
549 if Complex.generic?(z) and z >= 0
553 Complex(log!(r.abs), theta)
557 # Redefined to handle a Complex argument.
559 if Complex.generic?(z)
567 if Complex.generic?(z) and z >= -1 and z <= 1
570 -1.0.im * log( z + 1.0.im * sqrt(1.0-z*z) )
575 if Complex.generic?(z) and z >= -1 and z <= 1
578 -1.0.im * log( 1.0.im * z + sqrt(1.0-z*z) )
583 if Complex.generic?(z)
586 1.0.im * log( (1.0.im+z) / (1.0.im-z) ) / 2.0
591 if Complex.generic?(y) and Complex.generic?(x)
594 -1.0.im * log( (x+1.0.im*y) / sqrt(x*x+y*y) )
599 if Complex.generic?(z) and z >= 1
602 log( z + sqrt(z*z-1.0) )
607 if Complex.generic?(z)
610 log( z + sqrt(1.0+z*z) )
615 if Complex.generic?(z) and z >= -1 and z <= 1
618 log( (1.0+z) / (1.0-z) ) / 2.0
622 module_function :sqrt!
623 module_function :sqrt
624 module_function :exp!
626 module_function :log!
628 module_function :log10!
629 module_function :log10
630 module_function :cosh!
631 module_function :cosh
632 module_function :cos!
634 module_function :sinh!
635 module_function :sinh
636 module_function :sin!
638 module_function :tan!
640 module_function :tanh!
641 module_function :tanh
642 module_function :acos!
643 module_function :acos
644 module_function :asin!
645 module_function :asin
646 module_function :atan!
647 module_function :atan
648 module_function :atan2!
649 module_function :atan2
650 module_function :acosh!
651 module_function :acosh
652 module_function :asinh!
653 module_function :asinh
654 module_function :atanh!
655 module_function :atanh
659 # Documentation comments:
660 # - source: original (researched from pickaxe)
661 # - a couple of fixme's
662 # - RDoc output for Bignum etc. is a bit short, with nothing but an
663 # (undocumented) alias. No big deal.