On this page
Complex
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:
-
Method
Complex.rect. -
Method
Kernel#Complex, either with numeric arguments or with certain string arguments. -
Method
String#to_c, for certain strings.
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:
-
Separately, with methods
Complex#realandComplex#imaginary. -
Together, with method
Complex#rect.
The corresponding (computed) polar values may be retrieved:
-
Separately, with methods
Complex#absandComplex#arg. -
Together, with method
Complex#polar.
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:
-
Method
Complex.polar. -
Method
Kernel#Complex, with certain string arguments. -
Method
String#to_c, for certain strings.
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:
-
Separately, with methods
Complex#absandComplex#arg. -
Together, with method
Complex#polar.
The corresponding (computed) rectangular values may be retrieved:
-
Separately, with methods
Complex#realandComplex#imag. -
Together, with method
Complex#rect.
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 aliasmagnitude): Returns the absolute value forself. -
arg(and its aliasesangleandphase): Returns the argument (angle) forselfin radians. -
denominator: Returns the denominator ofself. -
finite?: Returns whether bothself.realandself.imageare finite. -
hash: Returns the integer hash value forself. -
imag(and its aliasimaginary): Returns the imaginary value forself. -
infinite?: Returns whetherself.realorself.imageis infinite. -
numerator: Returns the numerator ofself. -
polar: Returns the array[self.abs, self.arg]. -
inspect: Returns a string representation ofself. -
real: Returns the real value forself. -
real?: Returnsfalse; for compatibility withNumeric#real?. -
rect(and its aliasrectangular): Returns the array[self.real, self.imag].
Comparing
-
<=>: Returns whetherselfis less than, equal to, or greater than the given argument. -
==: Returns whetherselfis equal to the given argument.
Converting
-
rationalize: Returns aRationalobject whose value is exactly or approximately equivalent to that ofself.real. -
to_c: Returnsself. -
to_d: Returns the value as a
BigDecimalobject. -
to_f: Returns the value ofself.realas aFloat, if possible. -
to_i: Returns the value ofself.realas anInteger, if possible. -
to_r: Returns the value ofself.realas aRational, if possible. -
to_s: Returns a string representation ofself.
Performing Complex Arithmetic
-
*: Returns the product ofselfand the given numeric. -
**: Returnsselfraised to power of the given numeric. -
+: Returns the sum ofselfand the given numeric. -
-: Returns the difference ofselfand the given numeric. -
-@: Returns the negation ofself. -
/: Returns the quotient ofselfand the given numeric. -
abs2: Returns square of the absolute value (magnitude) forself. -
conj(and its aliasconjugate): Returns the conjugate ofself. -
fdiv: ReturnsComplex.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 fromself.
These methods are provided by the JSON gem. To make these methods available:
require 'json/add/complex'