| Class | Complex |
| In: |
lib/complex.rb
|
| Parent: | Numeric |
The complex number class. See complex.rb for an overview.
| I | = | Complex(0,1) | I is the imaginary number. It exists at point (0,1) on the complex plane. |
| image | -> | imag |
| image | [R] | The imaginary part of a complex number. |
| real | [R] | The real part of a complex number. |
# File lib/complex.rb, line 126
126: def initialize(a, b)
127: raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
128: raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
129: raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
130: raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
131: @real = a
132: @image = b
133: end
Remainder after division by a real or complex number.
# File lib/complex.rb, line 245
245: def % (other)
246: if other.kind_of?(Complex)
247: Complex(@real % other.real, @image % other.image)
248: elsif Complex.generic?(other)
249: Complex(@real % other, @image % other)
250: else
251: x , y = other.coerce(self)
252: x % y
253: end
254: end
Multiplication with real or complex number.
# File lib/complex.rb, line 170
170: def * (other)
171: if other.kind_of?(Complex)
172: re = @real*other.real - @image*other.image
173: im = @real*other.image + @image*other.real
174: Complex(re, im)
175: elsif Complex.generic?(other)
176: Complex(@real * other, @image * other)
177: else
178: x , y = other.coerce(self)
179: x * y
180: end
181: end
Raise this complex number to the given (real or complex) power.
# File lib/complex.rb, line 200
200: def ** (other)
201: if other == 0
202: return Complex(1)
203: end
204: if other.kind_of?(Complex)
205: r, theta = polar
206: ore = other.real
207: oim = other.image
208: nr = Math.exp!(ore*Math.log!(r) - oim * theta)
209: ntheta = theta*ore + oim*Math.log!(r)
210: Complex.polar(nr, ntheta)
211: elsif other.kind_of?(Integer)
212: if other > 0
213: x = self
214: z = x
215: n = other - 1
216: while n != 0
217: while (div, mod = n.divmod(2)
218: mod == 0)
219: x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
220: n = div
221: end
222: z *= x
223: n -= 1
224: end
225: z
226: else
227: if defined? Rational
228: (Rational(1) / self) ** -other
229: else
230: self ** Float(other)
231: end
232: end
233: elsif Complex.generic?(other)
234: r, theta = polar
235: Complex.polar(r**other, theta*other)
236: else
237: x, y = other.coerce(self)
238: x**y
239: end
240: end
Addition with real or complex number.
# File lib/complex.rb, line 138
138: def + (other)
139: if other.kind_of?(Complex)
140: re = @real + other.real
141: im = @image + other.image
142: Complex(re, im)
143: elsif Complex.generic?(other)
144: Complex(@real + other, @image)
145: else
146: x , y = other.coerce(self)
147: x + y
148: end
149: end
Subtraction with real or complex number.
# File lib/complex.rb, line 154
154: def - (other)
155: if other.kind_of?(Complex)
156: re = @real - other.real
157: im = @image - other.image
158: Complex(re, im)
159: elsif Complex.generic?(other)
160: Complex(@real - other, @image)
161: else
162: x , y = other.coerce(self)
163: x - y
164: end
165: end
Division by real or complex number.
# File lib/complex.rb, line 186
186: def / (other)
187: if other.kind_of?(Complex)
188: self*other.conjugate/other.abs2
189: elsif Complex.generic?(other)
190: Complex(@real/other, @image/other)
191: else
192: x, y = other.coerce(self)
193: x/y
194: end
195: end
Compares the absolute values of the two numbers.
# File lib/complex.rb, line 312
312: def <=> (other)
313: self.abs <=> other.abs
314: end
Test for numerical equality (a == a + 0i).
# File lib/complex.rb, line 319
319: def == (other)
320: if other.kind_of?(Complex)
321: @real == other.real and @image == other.image
322: elsif Complex.generic?(other)
323: @real == other and @image == 0
324: else
325: other == self
326: end
327: end
Absolute value (aka modulus): distance from the zero point on the complex plane.
# File lib/complex.rb, line 275
275: def abs
276: Math.hypot(@real, @image)
277: end
Square of the absolute value.
# File lib/complex.rb, line 282
282: def abs2
283: @real*@real + @image*@image
284: end
Argument (angle from (1,0) on the complex plane).
# File lib/complex.rb, line 289
289: def arg
290: Math.atan2!(@image, @real)
291: end
FIXME
# File lib/complex.rb, line 343
343: def denominator
344: @real.denominator.lcm(@image.denominator)
345: end
Returns a hash code for the complex number.
# File lib/complex.rb, line 386
386: def hash
387: @real.hash ^ @image.hash
388: end
FIXME
# File lib/complex.rb, line 350
350: def numerator
351: cd = denominator
352: Complex(@real.numerator*(cd/@real.denominator),
353: @image.numerator*(cd/@image.denominator))
354: end
Returns the absolute value and the argument.
# File lib/complex.rb, line 297
297: def polar
298: return abs, arg
299: end
Standard string representation of the complex number.
# File lib/complex.rb, line 359
359: def to_s
360: if @real != 0
361: if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
362: if @image >= 0
363: @real.to_s+"+("+@image.to_s+")i"
364: else
365: @real.to_s+"-("+(-@image).to_s+")i"
366: end
367: else
368: if @image >= 0
369: @real.to_s+"+"+@image.to_s+"i"
370: else
371: @real.to_s+"-"+(-@image).to_s+"i"
372: end
373: end
374: else
375: if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
376: "("+@image.to_s+")i"
377: else
378: @image.to_s+"i"
379: end
380: end
381: end