Class Complex
In: lib/complex.rb
lib/mathn.rb
Parent: Numeric

The complex number class. See complex.rb for an overview.

Methods

%   *   **   +   -   /   <=>   ==   abs   abs2   angle   arg   coerce   conj   conjugate   denominator   hash   inspect   new   new!   numerator   polar   polar   quo   to_s  

Constants

I = Complex(0,1)   I is the imaginary number. It exists at point (0,1) on the complex plane.
Unify = true

External Aliases

image -> imag

Attributes

image  [R]  The imaginary part of a complex number.
real  [R]  The real part of a complex number.

Public Class methods

[Source]

     # File lib/complex.rb, line 128
128:   def initialize(a, b)
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
133:     @real = a
134:     @image = b
135:   end

Creates a Complex number a+bi.

[Source]

     # File lib/complex.rb, line 124
124:   def Complex.new!(a, b=0)
125:     new(a,b)
126:   end

Creates a Complex number in terms of r (radius) and theta (angle).

[Source]

     # File lib/complex.rb, line 117
117:   def Complex.polar(r, theta)
118:     Complex(r*Math.cos(theta), r*Math.sin(theta))
119:   end

Public Instance methods

Remainder after division by a real or complex number.

[Source]

     # File lib/complex.rb, line 251
251:   def % (other)
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)
256:     else
257:       x , y = other.coerce(self)
258:       x % y
259:     end
260:   end

Multiplication with real or complex number.

[Source]

     # File lib/complex.rb, line 172
172:   def * (other)
173:     if other.kind_of?(Complex)
174:       re = @real*other.real - @image*other.image
175:       im = @real*other.image + @image*other.real
176:       Complex(re, im)
177:     elsif Complex.generic?(other)
178:       Complex(@real * other, @image * other)
179:     else
180:       x , y = other.coerce(self)
181:       x * y
182:     end
183:   end

Raise this complex number to the given (real or complex) power.

[Source]

     # File lib/complex.rb, line 206
206:   def ** (other)
207:     if other == 0
208:       return Complex(1)
209:     end
210:     if other.kind_of?(Complex)
211:       r, theta = polar
212:       ore = other.real
213:       oim = other.image
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)
218:       if other > 0
219:         x = self
220:         z = x
221:         n = other - 1
222:         while n != 0
223:           while (div, mod = n.divmod(2)
224:                  mod == 0)
225:             x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
226:             n = div
227:           end
228:           z *= x
229:           n -= 1
230:         end
231:         z
232:       else
233:         if defined? Rational
234:           (Rational(1) / self) ** -other
235:         else
236:           self ** Float(other)
237:         end
238:       end
239:     elsif Complex.generic?(other)
240:       r, theta = polar
241:       Complex.polar(r**other, theta*other)
242:     else
243:       x, y = other.coerce(self)
244:       x**y
245:     end
246:   end

Addition with real or complex number.

[Source]

     # File lib/complex.rb, line 140
140:   def + (other)
141:     if other.kind_of?(Complex)
142:       re = @real + other.real
143:       im = @image + other.image
144:       Complex(re, im)
145:     elsif Complex.generic?(other)
146:       Complex(@real + other, @image)
147:     else
148:       x , y = other.coerce(self)
149:       x + y
150:     end
151:   end

Subtraction with real or complex number.

[Source]

     # File lib/complex.rb, line 156
156:   def - (other)
157:     if other.kind_of?(Complex)
158:       re = @real - other.real
159:       im = @image - other.image
160:       Complex(re, im)
161:     elsif Complex.generic?(other)
162:       Complex(@real - other, @image)
163:     else
164:       x , y = other.coerce(self)
165:       x - y
166:     end
167:   end

Division by real or complex number.

[Source]

     # File lib/complex.rb, line 188
188:   def / (other)
189:     if other.kind_of?(Complex)
190:       self*other.conjugate/other.abs2
191:     elsif Complex.generic?(other)
192:       Complex(@real/other, @image/other)
193:     else
194:       x, y = other.coerce(self)
195:       x/y
196:     end
197:   end

Compares the absolute values of the two numbers.

[Source]

     # File lib/complex.rb, line 318
318:   def <=> (other)
319:     self.abs <=> other.abs
320:   end

Test for numerical equality (a == a + 0i).

[Source]

     # File lib/complex.rb, line 325
325:   def == (other)
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
330:     else
331:       other == self
332:     end
333:   end

Absolute value (aka modulus): distance from the zero point on the complex plane.

[Source]

     # File lib/complex.rb, line 281
281:   def abs
282:     Math.hypot(@real, @image)
283:   end

Square of the absolute value.

[Source]

     # File lib/complex.rb, line 288
288:   def abs2
289:     @real*@real + @image*@image
290:   end
angle()

Alias for arg

Argument (angle from (1,0) on the complex plane).

[Source]

     # File lib/complex.rb, line 295
295:   def arg
296:     Math.atan2!(@image, @real)
297:   end

Attempts to coerce other to a Complex number.

[Source]

     # File lib/complex.rb, line 338
338:   def coerce(other)
339:     if Complex.generic?(other)
340:       return Complex.new!(other), self
341:     else
342:       super
343:     end
344:   end
conj()

Alias for conjugate

Complex conjugate (z + z.conjugate = 2 * z.real).

[Source]

     # File lib/complex.rb, line 310
310:   def conjugate
311:     Complex(@real, -@image)
312:   end

FIXME

[Source]

     # File lib/complex.rb, line 349
349:   def denominator
350:     @real.denominator.lcm(@image.denominator)
351:   end

Returns a hash code for the complex number.

[Source]

     # File lib/complex.rb, line 392
392:   def hash
393:     @real.hash ^ @image.hash
394:   end

Returns "Complex(real, image)".

[Source]

     # File lib/complex.rb, line 399
399:   def inspect
400:     sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
401:   end

FIXME

[Source]

     # File lib/complex.rb, line 356
356:   def numerator
357:     cd = denominator
358:     Complex(@real.numerator*(cd/@real.denominator),
359:             @image.numerator*(cd/@image.denominator))
360:   end

Returns the absolute value and the argument.

[Source]

     # File lib/complex.rb, line 303
303:   def polar
304:     return abs, arg
305:   end

[Source]

     # File lib/complex.rb, line 199
199:   def quo(other)
200:     Complex(@real.quo(1), @image.quo(1)) / other
201:   end

Standard string representation of the complex number.

[Source]

     # File lib/complex.rb, line 365
365:   def to_s
366:     if @real != 0
367:       if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
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:       else
374:         if @image >= 0
375:           @real.to_s+"+"+@image.to_s+"i"
376:         else
377:           @real.to_s+"-"+(-@image).to_s+"i"
378:         end
379:       end
380:     else
381:       if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
382:         "("+@image.to_s+")i"
383:       else
384:         @image.to_s+"i"
385:       end
386:     end
387:   end

[Validate]