class Complex

Parent:
Numeric

A Complex object houses a pair of values, given when the object is created as either rectangular coordinates or polar coordinates.

Rectangular Coordinates

The rectangular coordinates of a complex number are called the real and imaginary parts; see Complex number definition.

You can create a Complex object from rectangular coordinates with:

Note that each of the stored parts may be a an instance one of the classes Complex, Float, Integer, or Rational; they may be retrieved:

The corresponding (computed) polar values may be retrieved:

Polar Coordinates

The polar coordinates of a complex number are called the absolute and argument parts; see Complex polar plane.

In this class, the argument part in expressed radians (not degrees).

You can create a Complex object from polar coordinates with:

Note that each of the stored parts may be a an instance one of the classes Complex, Float, Integer, or Rational; they may be retrieved:

The corresponding (computed) rectangular values may be retrieved:

What’s Here

First, what’s elsewhere:

  • Class Complex inherits (directly or indirectly) from classes Numeric and Object.

  • Includes (indirectly) module Comparable.

Here, class Complex has methods for:

Creating Complex Objects

  • ::polar: Returns a new Complex object based on given polar coordinates.

  • ::rect (and its alias ::rectangular): Returns a new Complex object based on given rectangular coordinates.

Querying

  • abs (and its alias magnitude): Returns the absolute value for self.

  • arg (and its aliases angle and phase): Returns the argument (angle) for self in radians.

  • denominator: Returns the denominator of self.

  • finite?: Returns whether both self.real and self.image are finite.

  • hash: Returns the integer hash value for self.

  • imag (and its alias imaginary): Returns the imaginary value for self.

  • infinite?: Returns whether self.real or self.image is infinite.

  • numerator: Returns the numerator of self.

  • polar: Returns the array [self.abs, self.arg].

  • inspect: Returns a string representation of self.

  • real: Returns the real value for self.

  • real?: Returns false; for compatibility with Numeric#real?.

  • rect (and its alias rectangular): Returns the array [self.real, self.imag].

Comparing

  • <=>: Returns whether self is less than, equal to, or greater than the given argument.

  • ==: Returns whether self is equal to the given argument.

Converting

  • rationalize: Returns a Rational object whose value is exactly or approximately equivalent to that of self.real.

  • to_c: Returns self.

  • to_d: Returns the value as a BigDecimal object.

  • to_f: Returns the value of self.real as a Float, if possible.

  • to_i: Returns the value of self.real as an Integer, if possible.

  • to_r: Returns the value of self.real as a Rational, if possible.

  • to_s: Returns a string representation of self.

Performing Complex Arithmetic

  • *: Returns the product of self and the given numeric.

  • **: Returns self raised to power of the given numeric.

  • +: Returns the sum of self and the given numeric.

  • -: Returns the difference of self and the given numeric.

  • -@: Returns the negation of self.

  • /: Returns the quotient of self and the given numeric.

  • abs2: Returns square of the absolute value (magnitude) for self.

  • conj (and its alias conjugate): Returns the conjugate of self.

  • fdiv: Returns Complex.rect(self.real/numeric, self.imag/numeric).

Working with JSON

  • ::json_create: Returns a new Complex object, deserialized from the given serialized hash.

  • as_json: Returns a serialized hash constructed from self.

  • to_json: Returns a JSON string representing self.

These methods are provided by the JSON gem. To make these methods available:

require 'json/add/complex'

Ruby Core © 1993–2024 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.