3 # $Release Version: 0.5 $
5 # $Date: 2002/02/01 06:35:39 $
6 # by Keiju ISHITSUKA(SHL Japan Inc.)
10 # class Complex < Numeric
12 # Complex(x, y) --> x + yi
23 # Complex::divmod -- obsolete
52 if a.kind_of?(Complex) and b == 0
54 elsif b.kind_of?(Complex)
55 if a.kind_of?(Complex)
56 Complex(a.real-b.image, a.image + b.real)
58 Complex(a-b.image, b.real)
60 elsif b == 0 and defined? Complex::Unify
67 class Complex < Numeric
68 @RCS_ID='-$Id: complex.rb,v 1.1.1.1 2002/02/01 06:35:39 tacyas Exp $-'
70 def Complex.generic?(other)
71 other.kind_of?(Integer) or
72 other.kind_of?(Float) or
73 (defined?(Rational) and other.kind_of?(Rational))
76 def Complex.polar(r, theta)
77 Complex(r*Math.cos(theta), r*Math.sin(theta))
80 def initialize(a, b = 0)
81 raise "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
82 raise "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
88 if other.kind_of?(Complex)
89 re = @real + other.real
90 im = @image + other.image
92 elsif Complex.generic?(other)
93 Complex(@real + other, @image)
95 x , y = other.coerce(self)
101 if other.kind_of?(Complex)
102 re = @real - other.real
103 im = @image - other.image
105 elsif Complex.generic?(other)
106 Complex(@real - other, @image)
108 x , y = other.coerce(self)
114 if other.kind_of?(Complex)
115 re = @real*other.real - @image*other.image
116 im = @real*other.image + @image*other.real
118 elsif Complex.generic?(other)
119 Complex(@real * other, @image * other)
121 x , y = other.coerce(self)
127 if other.kind_of?(Complex)
128 self*other.conjugate/other.abs2
129 elsif Complex.generic?(other)
130 Complex(@real/other, @image/other)
132 x, y = other.coerce(self)
141 if other.kind_of?(Complex)
145 nr = Math.exp!(ore*Math.log!(r) - oim * theta)
146 ntheta = theta*ore + oim*Math.log!(r)
147 Complex.polar(nr, ntheta)
148 elsif other.kind_of?(Integer)
154 while (div, mod = n.divmod(2)
156 x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
165 (Rational(1) / self) ** -other
170 elsif Complex.generic?(other)
172 Complex.polar(r.power!(other), theta * other)
174 x, y = other.coerce(self)
180 if other.kind_of?(Complex)
181 Complex(@real % other.real, @image % other.image)
182 elsif Complex.generic?(other)
183 Complex(@real % other, @image % other)
185 x , y = other.coerce(self)
191 # if other.kind_of?(Complex)
192 # rdiv, rmod = @real.divmod(other.real)
193 # idiv, imod = @image.divmod(other.image)
194 # return Complex(rdiv, idiv), Complex(rmod, rmod)
195 # elsif Complex.generic?(other)
196 # Complex(@real.divmod(other), @image.divmod(other))
198 # x , y = other.coerce(self)
204 Math.sqrt!((@real*@real + @image*@image).to_f)
208 @real*@real + @image*@image
212 Math.atan2(@image.to_f, @real.to_f)
220 Complex(@real, -@image)
224 self.abs <=> other.abs
228 if other.kind_of?(Complex)
229 @real == other.real and @image == other.image
230 elsif Complex.generic?(other)
231 @real == other and @image == 0
233 x , y = other.coerce(self)
239 if Complex.generic?(other)
240 return Complex.new(other), self
247 Complex(@real.to_i, @image.to_i)
251 Complex(@real.to_f, @image.to_f)
255 Complex(@real.to_r, @image.to_r)
259 @real.denominator.lcm(@image.denominator)
264 Complex(@real.numerator*(cd/@real.denominator),
265 @image.numerator*(cd/@image.denominator))
270 if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
272 @real.to_s+"+("+@image.to_s+")i"
274 @real.to_s+"-("+(-@image).to_s+")i"
278 @real.to_s+"+"+@image.to_s+"i"
280 @real.to_s+"-"+(-@image).to_s+"i"
284 if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
297 sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
325 return Math.atan2(1,1)*4
339 if not defined? Rational
345 Complex.new(self) ** other
351 Rational.new!(self,1)**other
361 if not defined? Rational
381 if Complex.generic?(z)
393 if Complex.generic?(z)
396 Complex(exp!(z.real) * cos!(z.image), exp!(z.real) * sin!(z.image))
401 (exp!(x) + exp!(-x))/2.0
405 (exp!(x) - exp!(-x))/2.0
409 if Complex.generic?(z)
412 Complex(cos!(z.real)*cosh!(z.image),
413 -sin!(z.real)*sinh!(z.image))
418 if Complex.generic?(z)
421 Complex(sin!(z.real)*cosh!(z.image),
422 cos!(z.real)*sinh!(z.image))
427 if Complex.generic?(z)
435 if Complex.generic?(z) and z >= 0
439 Complex(log!(r.abs), theta)
444 if Complex.generic?(z)
452 if Complex.generic?(x) and Complex.generic?(y)
455 fail "Not yet implemented."
460 log((1.0 + x.to_f) / ( 1.0 - x.to_f)) / 2.0
464 if Complex.generic?(z)
472 c = (a*a + b*b - 1.0)
473 d = (a*a + b*b + 1.0)
475 Complex(atan2!((c + sqrt(c*c + 4.0*a*a)), 2.0*a),
476 atanh!((-d + sqrt(d*d - 4.0*b*b))/(2.0*b)))
480 module_function :sqrt
481 module_function :sqrt!
482 module_function :exp!
484 module_function :cosh!
485 module_function :cos!
487 module_function :sinh!
488 module_function :sin!
490 module_function :tan!
492 module_function :log!
494 module_function :log10!
496 module_function :atan2!
497 module_function :atan2
498 # module_function :atan!
499 module_function :atan
500 module_function :atanh!