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 124
124:   def initialize(a, b)
125:     raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
126:     raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
127:     raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
128:     raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
129:     @real = a
130:     @image = b
131:   end

Creates a Complex number a+bi.

[Source]

     # File lib/complex.rb, line 120
120:   def Complex.new!(a, b=0)
121:     new(a,b)
122:   end

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

[Source]

     # File lib/complex.rb, line 113
113:   def Complex.polar(r, theta)
114:     Complex(r*Math.cos(theta), r*Math.sin(theta))
115:   end

Public Instance methods

Remainder after division by a real or complex number.

[Source]

     # File lib/complex.rb, line 247
247:   def % (other)
248:     if other.kind_of?(Complex)
249:       Complex(@real % other.real, @image % other.image)
250:     elsif Complex.generic?(other)
251:       Complex(@real % other, @image % other)
252:     else
253:       x , y = other.coerce(self)
254:       x % y
255:     end
256:   end

Multiplication with real or complex number.

[Source]

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

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

[Source]

     # File lib/complex.rb, line 202
202:   def ** (other)
203:     if other == 0
204:       return Complex(1)
205:     end
206:     if other.kind_of?(Complex)
207:       r, theta = polar
208:       ore = other.real
209:       oim = other.image
210:       nr = Math.exp!(ore*Math.log!(r) - oim * theta)
211:       ntheta = theta*ore + oim*Math.log!(r)
212:       Complex.polar(nr, ntheta)
213:     elsif other.kind_of?(Integer)
214:       if other > 0
215:         x = self
216:         z = x
217:         n = other - 1
218:         while n != 0
219:           while (div, mod = n.divmod(2)
220:                  mod == 0)
221:             x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
222:             n = div
223:           end
224:           z *= x
225:           n -= 1
226:         end
227:         z
228:       else
229:         if defined? Rational
230:           (Rational(1) / self) ** -other
231:         else
232:           self ** Float(other)
233:         end
234:       end
235:     elsif Complex.generic?(other)
236:       r, theta = polar
237:       Complex.polar(r**other, theta*other)
238:     else
239:       x, y = other.coerce(self)
240:       x**y
241:     end
242:   end

Addition with real or complex number.

[Source]

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

Subtraction with real or complex number.

[Source]

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

Division by real or complex number.

[Source]

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

Compares the absolute values of the two numbers.

[Source]

     # File lib/complex.rb, line 314
314:   def <=> (other)
315:     self.abs <=> other.abs
316:   end

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

[Source]

     # File lib/complex.rb, line 321
321:   def == (other)
322:     if other.kind_of?(Complex)
323:       @real == other.real and @image == other.image
324:     elsif Complex.generic?(other)
325:       @real == other and @image == 0
326:     else
327:       other == self
328:     end
329:   end

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

[Source]

     # File lib/complex.rb, line 277
277:   def abs
278:     Math.hypot(@real, @image)
279:   end

Square of the absolute value.

[Source]

     # File lib/complex.rb, line 284
284:   def abs2
285:     @real*@real + @image*@image
286:   end
angle()

Alias for arg

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

[Source]

     # File lib/complex.rb, line 291
291:   def arg
292:     Math.atan2!(@image, @real)
293:   end

Attempts to coerce other to a Complex number.

[Source]

     # File lib/complex.rb, line 334
334:   def coerce(other)
335:     if Complex.generic?(other)
336:       return Complex.new!(other), self
337:     else
338:       super
339:     end
340:   end
conj()

Alias for conjugate

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

[Source]

     # File lib/complex.rb, line 306
306:   def conjugate
307:     Complex(@real, -@image)
308:   end

FIXME

[Source]

     # File lib/complex.rb, line 345
345:   def denominator
346:     @real.denominator.lcm(@image.denominator)
347:   end

Returns a hash code for the complex number.

[Source]

     # File lib/complex.rb, line 388
388:   def hash
389:     @real.hash ^ @image.hash
390:   end

Returns "Complex(real, image)".

[Source]

     # File lib/complex.rb, line 395
395:   def inspect
396:     sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
397:   end

FIXME

[Source]

     # File lib/complex.rb, line 352
352:   def numerator
353:     cd = denominator
354:     Complex(@real.numerator*(cd/@real.denominator),
355:             @image.numerator*(cd/@image.denominator))
356:   end

Returns the absolute value and the argument.

[Source]

     # File lib/complex.rb, line 299
299:   def polar
300:     return abs, arg
301:   end

[Source]

     # File lib/complex.rb, line 195
195:   def quo(other)
196:     Complex(@real.quo(1), @image.quo(1)) / other
197:   end

Standard string representation of the complex number.

[Source]

     # File lib/complex.rb, line 361
361:   def to_s
362:     if @real != 0
363:       if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
364:         if @image >= 0
365:           @real.to_s+"+("+@image.to_s+")i"
366:         else
367:           @real.to_s+"-("+(-@image).to_s+")i"
368:         end
369:       else
370:         if @image >= 0
371:           @real.to_s+"+"+@image.to_s+"i"
372:         else
373:           @real.to_s+"-"+(-@image).to_s+"i"
374:         end
375:       end
376:     else
377:       if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
378:         "("+@image.to_s+")i"
379:       else
380:         @image.to_s+"i"
381:       end
382:     end
383:   end

[Validate]