class 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:
-
A complex literal.
-
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.
Constants
- I
Equivalent to
Complex(0, 1):Complex::I # => (0+1i)
Public Class Methods
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric, or an instance of one of its subclasses: Complex, Float, Integer, Rational. Argument arg is given in radians; see Polar Coordinates:
Complex.polar(3) # => (3+0i) Complex.polar(3, 2.0) # => (-1.2484405096414273+2.727892280477045i) Complex.polar(-3, -2.0) # => (1.2484405096414273+2.727892280477045i)
static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
{
VALUE abs, arg;
argc = rb_scan_args(argc, argv, "11", &abs, &arg);
abs = nucomp_real_check(abs);
if (argc == 2) {
arg = nucomp_real_check(arg);
}
else {
arg = ZERO;
}
return f_complex_polar_real(klass, abs, arg);
}
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric, or an instance of one of its subclasses: Complex, Float, Integer, Rational; see Rectangular Coordinates:
Complex.rect(3) # => (3+0i) Complex.rect(3, Math::PI) # => (3+3.141592653589793i) Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
Complex.rectangular is an alias for Complex.rect.
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
real = nucomp_real_check(real);
imag = ZERO;
break;
default:
real = nucomp_real_check(real);
imag = nucomp_real_check(imag);
break;
}
return nucomp_s_new_internal(klass, real, imag);
}
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric, or an instance of one of its subclasses: Complex, Float, Integer, Rational; see Rectangular Coordinates:
Complex.rect(3) # => (3+0i) Complex.rect(3, Math::PI) # => (3+3.141592653589793i) Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
Complex.rectangular is an alias for Complex.rect.
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
real = nucomp_real_check(real);
imag = ZERO;
break;
default:
real = nucomp_real_check(real);
imag = nucomp_real_check(imag);
break;
}
return nucomp_s_new_internal(klass, real, imag);
}
Public Instance Methods
Returns the product of self and numeric:
Complex(2, 3) * Complex(2, 3) # => (-5+12i) Complex(900) * Complex(1) # => (900+0i) Complex(-2, 9) * Complex(-9, 2) # => (0-85i) Complex(9, 8) *