Class: Complex

Class | Complex
In: | complex.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 to_s

### Constants

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

### 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

new(a, b)

[Source]

    # File complex.rb, line 126
      def initialize(a, b)
        raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
        raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
        raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
        raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
        @real = a
        @image = b
      end

new!(a, b=0)

Creates a `Complex` number `a`+`b`i.

[Source]

    # File complex.rb, line 122
      def Complex.new!(a, b=0)
        new(a,b)
      end

polar(r, theta)

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

[Source]

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

### Public Instance methods

%(other)

Remainder after division by a real or complex number.

[Source]

    # File complex.rb, line 245
      def % (other)
        if other.kind_of?(Complex)
          Complex(@real % other.real, @image % other.image)
        elsif Complex.generic?(other)
          Complex(@real % other, @image % other)
        else
          x , y = other.coerce(self)
          x % y
        end
      end

*(other)

Multiplication with real or complex number.

[Source]

    # File complex.rb, line 170
      def * (other)
        if other.kind_of?(Complex)
          re = @real*other.real - @image*other.image
          im = @real*other.image + @image*other.real
          Complex(re, im)
        elsif Complex.generic?(other)
          Complex(@real * other, @image * other)
        else
          x , y = other.coerce(self)
          x * y
        end
      end

**(other)

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

[Source]

    # File complex.rb, line 200
      def ** (other)
        if other == 0
          return Complex(1)
        end
        if other.kind_of?(Complex)
          r, theta = polar
          ore = other.real
          oim = other.image
          nr = Math.exp!(ore*Math.log!(r) - oim * theta)
          ntheta = theta*ore + oim*Math.log!(r)
          Complex.polar(nr, ntheta)
        elsif other.kind_of?(Integer)
          if other > 0
            x = self
            z = x
            n = other - 1
            while n != 0
              while (div, mod = n.divmod(2)
                     mod == 0)
                x = Complex(x.real*x.real - x.image*x.image, 2*x.real*x.image)
                n = div
              end
              z *= x
              n -= 1
            end
            z
          else
            if defined? Rational
              (Rational(1) / self) ** -other
            else
              self ** Float(other)
            end
          end
        elsif Complex.generic?(other)
          r, theta = polar
          Complex.polar(r**other, theta*other)
        else
          x, y = other.coerce(self)
          x**y
        end
      end

+(other)

Addition with real or complex number.

[Source]

    # File complex.rb, line 138
      def + (other)
        if other.kind_of?(Complex)
          re = @real + other.real
          im = @image + other.image
          Complex(re, im)
        elsif Complex.generic?(other)
          Complex(@real + other, @image)
        else
          x , y = other.coerce(self)
          x + y
        end
      end

-(other)

Subtraction with real or complex number.

[Source]

    # File complex.rb, line 154
      def - (other)
        if other.kind_of?(Complex)
          re = @real - other.real
          im = @image - other.image
          Complex(re, im)
        elsif Complex.generic?(other)
          Complex(@real - other, @image)
        else
          x , y = other.coerce(self)
          x - y
        end
      end

/(other)

Division by real or complex number.

[Source]

    # File complex.rb, line 186
      def / (other)
        if other.kind_of?(Complex)
          self*other.conjugate/other.abs2
        elsif Complex.generic?(other)
          Complex(@real/other, @image/other)
        else
          x, y = other.coerce(self)
          x/y
        end
      end

<=>(other)

Compares the absolute values of the two numbers.

[Source]

    # File complex.rb, line 312
      def <=> (other)
        self.abs <=> other.abs
      end

==(other)

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

[Source]

    # File complex.rb, line 319
      def == (other)
        if other.kind_of?(Complex)
          @real == other.real and @image == other.image
        elsif Complex.generic?(other)
          @real == other and @image == 0
        else
          other == self
        end
      end

abs()

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

[Source]

    # File complex.rb, line 275
      def abs
        Math.hypot(@real, @image)
      end

abs2()

Square of the absolute value.

[Source]

    # File complex.rb, line 282
      def abs2
        @real*@real + @image*@image
      end

angle()

Alias for arg

arg()

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

[Source]

    # File complex.rb, line 289
      def arg
        Math.atan2!(@image, @real)
      end

coerce(other)

Attempts to coerce `other` to a Complex number.

[Source]

    # File complex.rb, line 332
      def coerce(other)
        if Complex.generic?(other)
          return Complex.new!(other), self
        else
          super
        end
      end

conj()

Alias for conjugate

conjugate()

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

[Source]

    # File complex.rb, line 304
      def conjugate
        Complex(@real, -@image)
      end

denominator()

FIXME

[Source]

    # File complex.rb, line 343
      def denominator
        @real.denominator.lcm(@image.denominator)
      end

hash()

Returns a hash code for the complex number.

[Source]

    # File complex.rb, line 386
      def hash
        @real.hash ^ @image.hash
      end

inspect()

Returns "`Complex(real, image)`".

[Source]

    # File complex.rb, line 393
      def inspect
        sprintf("Complex(%s, %s)", @real.inspect, @image.inspect)
      end

numerator()

FIXME

[Source]

    # File complex.rb, line 350
      def numerator
        cd = denominator
        Complex(@real.numerator*(cd/@real.denominator),
                @image.numerator*(cd/@image.denominator))
      end

polar()

Returns the absolute value and the argument.

[Source]

    # File complex.rb, line 297
      def polar
        return abs, arg
      end

to_s()

Standard string representation of the complex number.

[Source]

    # File complex.rb, line 359
      def to_s
        if @real != 0
          if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
            if @image >= 0
              @real.to_s+"+("+@image.to_s+")i"
            else
              @real.to_s+"-("+(-@image).to_s+")i"
            end
          else
            if @image >= 0
              @real.to_s+"+"+@image.to_s+"i"
            else
              @real.to_s+"-"+(-@image).to_s+"i"
            end
          end
        else
          if defined?(Rational) and @image.kind_of?(Rational) and @image.denominator != 1
            "("+@image.to_s+")i"
          else
            @image.to_s+"i"
          end
        end
      end

[Validate]