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](en.wikipedia.org/wiki/Complex_number#Definition_and_basic_ operations).
You can create a Complex object from rectangular coordinates with:
-
A complex literal.
-
MethodKernel#Complex, either with numeric arguments or with certain string arguments. -
MethodString#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:
-
MethodKernel#Complex, with certain string arguments. -
MethodString#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:
-
ClassComplexinherits (directly or indirectly) from classes Numeric and Object. -
Includes (indirectly) module Comparable.
Here, class Complex has methods for:
Creating Complex Objects
-
::polar: Returns a newComplexobject based on given polar coordinates. -
::rect(and its alias::rectangular): Returns a newComplexobject 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 aBigDecimalobject. -
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 newComplexobject, 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'
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/complex.rb, line 9 def self.json_create(object) Complex(object['r'], object['i']) end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 155
def self.polar: (Numeric, ?Numeric) -> Complex
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)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 172
def self.rect: (Numeric, ?Numeric) -> Complex
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.
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.
Public Instance Methods
(BigDecimal) → Complex
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1470
def *: (BigDecimal) -> Complex
| ...
Returns the product of self and numeric:
Complex.rect(2, 3) * Complex.rect(2, 3) # => (-5+12i) Complex.rect(900) * Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) * Complex.rect(-9, 2) # => (0-85i) Complex.rect(9, 8) * 4 # => (36+32i) Complex.rect(20, 9) * 9.8 # => (196.0+88.2i)
Returns the numeric product of self and other:
Complex.rect(9, 8) * 4 # => (36+32i) Complex.rect(20, 9) * 9.8 # => (196.0+88.2i) Complex.rect(2, 3) * Complex.rect(2, 3) # => (-5+12i) Complex.rect(900) * Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) * Complex.rect(-9, 2) # => (0-85i) Complex.rect(9, 8) * Rational(2, 3) # => ((6/1)+(16/3)*i)
Returns the numeric product of self and other:
Complex.rect(9, 8) * 4 # => (36+32i) Complex.rect(20, 9) * 9.8 # => (196.0+88.2i) Complex.rect(2, 3) * Complex.rect(2, 3) # => (-5+12i) Complex.rect(900) * Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) * Complex.rect(-9, 2) # => (0-85i) Complex.rect(9, 8) * Rational(2, 3) # => ((6/1)+(16/3)*i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 215
def **: (Numeric) -> Complex
Returns self raised to the power exponent:
Complex.rect(0, 1) ** 2 # => (-1+0i) Complex.rect(-8) ** Rational(1, 3) # => (1.0000000000000002+1.7320508075688772i)
(BigDecimal) → Complex
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1485
def +: (BigDecimal) -> Complex
| ...
Returns the sum of self and numeric:
Complex.rect(2, 3) + Complex.rect(2, 3) # => (4+6i) Complex.rect(900) + Complex.rect(1) # => (901+0i) Complex.rect(-2, 9) + Complex.rect(-9, 2) # => (-11+11i) Complex.rect(9, 8) + 4 # => (13+8i) Complex.rect(20, 9) + 9.8 # => (29.8+9i)
Returns the sum of self and other:
Complex(1, 2) + 0 # => (1+2i) Complex(1, 2) + 1 # => (2+2i) Complex(1, 2) + -1 # => (0+2i) Complex(1, 2) + 1.0 # => (2.0+2i) Complex(1, 2) + Complex(2, 1) # => (3+3i) Complex(1, 2) + Complex(2.0, 1.0) # => (3.0+3.0i) Complex(1, 2) + Rational(1, 1) # => ((2/1)+2i) Complex(1, 2) + Rational(1, 2) # => ((3/2)+2i)
For a computation involving Floats, the result may be inexact (see Float#+):
Complex(1, 2) + 3.14 # => (4.140000000000001+2i)
Returns the sum of self and other:
Complex(1, 2) + 0 # => (1+2i) Complex(1, 2) + 1 # => (2+2i) Complex(1, 2) + -1 # => (0+2i) Complex(1, 2) + 1.0 # => (2.0+2i) Complex(1, 2) + Complex(2, 1) # => (3+3i) Complex(1, 2) + Complex(2.0, 1.0) # => (3.0+3.0i) Complex(1, 2) + Rational(1, 1) # => ((2/1)+2i) Complex(1, 2) + Rational(1, 2) # => ((3/2)+2i)
For a computation involving Floats, the result may be inexact (see Float#+):
Complex(1, 2) + 3.14 # => (4.140000000000001+2i)
(BigDecimal) → Complex
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1500
def -: (BigDecimal) -> Complex
| ...
Returns the difference of self and numeric:
Complex.rect(2, 3) - Complex.rect(2, 3) # => (0+0i) Complex.rect(900) - Complex.rect(1) # => (899+0i) Complex.rect(-2, 9) - Complex.rect(-9, 2) # => (7+7i) Complex.rect(9, 8) - 4 # => (5+8i) Complex.rect(20, 9) - 9.8 # => (10.2+9i)
Returns the difference of self and other:
Complex.rect(2, 3) - Complex.rect(2, 3) # => (0+0i) Complex.rect(900) - Complex.rect(1) # => (899+0i) Complex.rect(-2, 9) - Complex.rect(-9, 2) # => (7+7i) Complex.rect(9, 8) - 4 # => (5+8i) Complex.rect(20, 9) - 9.8 # => (10.2+9i)
Returns the difference of self and other:
Complex.rect(2, 3) - Complex.rect(2, 3) # => (0+0i) Complex.rect(900) - Complex.rect(1) # => (899+0i) Complex.rect(-2, 9) - Complex.rect(-9, 2) # => (7+7i) Complex.rect(9, 8) - 4 # => (5+8i) Complex.rect(20, 9) - 9.8 # => (10.2+9i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 264
def -@: () -> Complex
Returns self, negated, which is the negation of each of its parts:
-Complex.rect(1, 2) # => (-1-2i) -Complex.rect(-1, -2) # => (1+2i)
(BigDecimal) → Complex
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1455
def /: (BigDecimal) -> Complex
| ...
Returns the quotient of self and numeric:
Complex.rect(2, 3) / Complex.rect(2, 3) # => (1+0i) Complex.rect(900) / Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) / Complex.rect(-9, 2) # => ((36/85)-(77/85)*i) Complex.rect(9, 8) / 4 # => ((9/4)+2i) Complex.rect(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
Returns the quotient of self and other:
Complex.rect(2, 3) / Complex.rect(2, 3) # => (1+0i) Complex.rect(900) / Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) / Complex.rect(-9, 2) # => ((36/85)-(77/85)*i) Complex.rect(9, 8) / 4 # => ((9/4)+2i) Complex.rect(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
Returns the quotient of self and other:
Complex.rect(2, 3) / Complex.rect(2, 3) # => (1+0i) Complex.rect(900) / Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) / Complex.rect(-9, 2) # => ((36/85)-(77/85)*i) Complex.rect(9, 8) / 4 # => ((9/4)+2i) Complex.rect(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 280
def <: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 282
def <=: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 312
def <=>: (untyped) -> Integer?
Compares self and other.
Returns:
-
self.real <=> other.realif both of the following are true:-
self.imag == 0. -
other.imag == 0(always true ifotheris numeric but not complex).
-
-
nilotherwise.
Examples:
Complex.rect(2) <=> 3 # => -1 Complex.rect(2) <=> 2 # => 0 Complex.rect(2) <=> 1 # => 1 Complex.rect(2, 1) <=> 1 # => nil # self.imag not zero. Complex.rect(1) <=> Complex.rect(1, 1) # => nil # object.imag not zero. Complex.rect(1) <=> 'Foo' # => nil # object.imag not defined.
Class Complex includes module Comparable, each of whose methods uses Complex#<=> for comparison.
(untyped) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 323
def ==: (untyped) -> bool
Returns true if self.real == object.real and self.imag == object.imag:
Complex.rect(2, 3) == Complex.rect(2.0, 3.0) # => true
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 325
def >: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 327
def >=: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 344
def abs: () -> Numeric
Returns the absolute value (magnitude) for self; see polar coordinates:
Complex.polar(-1, 0).abs # => 1.0
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 361
def abs2: () -> Numeric
Returns square of the absolute value (magnitude) for self; see polar coordinates:
Complex.polar(2, 2).abs2 # => 4.0
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rectangular(1.0/3, 1.0/3).abs2 # => 0.2222222222222222
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 375
def angle: () -> Float
Returns the argument (angle) for self in radians; see polar coordinates:
Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
Returns the argument (angle) for self in radians; see polar coordinates:
Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/complex.rb, line 29 def as_json(*) { JSON.create_id => self.class.name, 'r' => real, 'i' => imag, } end
Methods Complex#as_json and Complex.json_create may be used to serialize and deserialize a Complex object; see Marshal.
Method Complex#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/complex' x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0} y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
Method JSON.create deserializes such a hash, returning a Complex object:
Complex.json_create(x) # => (2+0i) Complex.json_create(y) # => (2.0+4i)
Methods Complex#as_json and Complex.json_create may be used to serialize and deserialize a Complex object; see Marshal.
Method Complex#as_json serializes self, returning a 2-element hash representing self:
require 'json/add/complex' x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0} y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
Method JSON.create deserializes such a hash, returning a Complex object:
Complex.json_create(x) # => (2+0i) Complex.json_create(y) # => (2.0+4i)
(*untyped) → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 394
def ceil: (*untyped) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 396
def coerce: (Numeric) -> [ Complex, Complex ]
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 404
def conj: () -> self
Returns the conjugate of self, Complex.rect(self.imag, self.real):
Complex.rect(1, 2).conj # => (1-2i)
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 415
def conjugate: () -> self
Returns the conjugate of self, Complex.rect(self.imag, self.real):
Complex.rect(1, 2).conj # => (1-2i)
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 431
def denominator: () -> Integer
Returns the denominator of self, which is the least common multiple of self.real.denominator and self.imag.denominator:
Complex.rect(Rational(1, 2), Rational(2, 3)).denominator # => 6
Note that n.denominator of a non-rational numeric is 1.
Related: Complex#numerator.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 433
def div: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 435
def divmod: (Numeric) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 445
def fdiv: (Numeric) -> Complex
Returns Complex.rect(self.real/numeric, self.imag/numeric):
Complex.rect(11, 22).fdiv(3) # => (3.6666666666666665+7.333333333333333i)
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 459
def finite?: () -> bool
Returns true if both self.real.finite? and self.imag.finite? are true, false otherwise:
Complex.rect(1, 1).finite? # => true Complex.rect(Float::INFINITY, 0).finite? # => false
Related: Numeric#finite?, Float#finite?.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 461
def floor: (?Integer) -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 474
def hash: () -> Integer
() → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 489
def i: () -> bot
Returns Complex(0, self):
2.i # => (0+2i) -2.i # => (0-2i) 2.0.i # => (0+2.0i) Rational(1, 2).i # => (0+(1/2)*i) Complex(3, 4).i # Raises NoMethodError.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 503
def imag: () -> Numeric
Returns the imaginary value for self:
Complex.rect(7).imag # => 0 Complex.rect(9, -4).imag # => -4
If self was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 520
def imaginary: () -> Numeric
Returns the imaginary value for self:
Complex.rect(7).imag # => 0 Complex.rect(9, -4).imag # => -4
If self was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 534
def infinite?: () -> Integer?
Returns 1 if either self.real.infinite? or self.imag.infinite? is true, nil otherwise:
Complex.rect(Float::INFINITY, 0).infinite? # => 1 Complex.rect(1, 1).infinite? # => nil
Related: Numeric#infinite?, Float#infinite?.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 548
def inspect: () -> String
Returns a string representation of self:
Complex.rect(2).inspect # => "(2+0i)" Complex.rect(-8, 6).inspect # => "(-8+6i)" Complex.rect(0, Rational(1, 2)).inspect # => "(0+(1/2)*i)" Complex.rect(0, Float::INFINITY).inspect # => "(0+Infinity*i)" Complex.rect(Float::NAN, Float::NAN).inspect # => "(NaN+NaN*i)"
Returns the absolute value (magnitude) for self; see polar coordinates:
Complex.polar(-1, 0).abs # => 1.0
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 564
def modulo: (Numeric) -> bot
() → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 566
def negative?: () -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 587
def numerator: () -> Complex
Returns the Complex object created from the numerators of the real and imaginary parts of self, after converting each part to the lowest common denominator of the two:
c = Complex.rect(Rational(2, 3), Rational(3, 4)) # => ((2/3)+(3/4)*i) c.numerator # => (8+9i)
In this example, the lowest common denominator of the two parts is 12; the two converted parts may be thought of as Rational(8, 12) and Rational(9, 12), whose numerators, respectively, are 8 and 9; so the returned value of c.numerator is Complex.rect(8, 9).
Related: Complex#denominator.
Returns the argument (angle) for self in radians; see polar coordinates:
Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 619
def polar: () -> [ Numeric, Float ]
Returns the array [self.abs, self.arg]:
Complex.polar(1, 2).polar # => [1.0, 2.0]
See Polar Coordinates.
If self was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rect(1, 1).polar # => [1.4142135623730951, 0.7853981633974483]
() → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 621
def positive?: () -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 635
def quo: (Numeric) -> Complex
Returns the quotient of self and other:
Complex.rect(2, 3) / Complex.rect(2, 3) # => (1+0i) Complex.rect(900) / Complex.rect(1) # => (900+0i) Complex.rect(-2, 9) / Complex.rect(-9, 2) # => ((36/85)-(77/85)*i) Complex.rect(9, 8) / 4 # => ((9/4)+2i) Complex.rect(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 669
def rationalize: (?Numeric eps) -> Rational
Returns a Rational object whose value is exactly or approximately equivalent to that of self.real.
With no argument epsilon given, returns a Rational object whose value is exactly equal to that of self.real.rationalize:
Complex.rect(1, 0).rationalize # => (1/1) Complex.rect(1, Rational(0, 1)).rationalize # => (1/1) Complex.rect(3.14159, 0).rationalize # => (314159/100000)
With argument epsilon given, returns a Rational object whose value is exactly or approximately equal to that of self.real to the given precision:
Complex.rect(3.14159, 0).rationalize(0.1) # => (16/5) Complex.rect(3.14159, 0).rationalize(0.01) # => (22/7) Complex.rect(3.14159, 0).rationalize(0.001) # => (201/64) Complex.rect(3.14159, 0).rationalize(0.0001) # => (333/106) Complex.rect(3.14159, 0).rationalize(0.00001) # => (355/113) Complex.rect(3.14159, 0).rationalize(0.000001) # => (7433/2366) Complex.rect(3.14159, 0).rationalize(0.0000001) # => (9208/2931) Complex.rect(3.14159, 0).rationalize(0.00000001) # => (47460/15107) Complex.rect(3.14159, 0).rationalize(0.000000001) # => (76149/24239) Complex.rect(3.14159, 0).rationalize(0.0000000001) # => (314159/100000) Complex.rect(3.14159, 0).rationalize(0.0) # => (3537115888337719/1125899906842624)
Related: Complex#to_r.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 686
def real: () -> Numeric
Returns the real value for self:
Complex.rect(7).real # => 7 Complex.rect(9, -4).real # => 9
If self was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).real # => 0.7071067811865476 # Square root of 2.
() → false
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 694
def real?: () -> false
Returns false; for compatibility with Numeric#real?.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 708
def rect: () -> [ Numeric, Numeric ]
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.
Returns the array [self.real, self.imag]:
Complex.rect(1, 2).rect # => [1, 2]
If self was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1.0, 1.0).rect # => [0.5403023058681398, 0.8414709848078965]
Complex#rectangular is an alias for Complex#rect.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 730
def reminder: (Numeric) -> bot
(*untyped) → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 732
def round: (*untyped) -> bot
(*untyped) ?{ (*untyped) → untyped } → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 734
def step: (*untyped) ?{ (*untyped) -> untyped } -> bot
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 742
def to_c: () -> Complex
Returns self.
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/lib/bigdecimal/util.rb, line 164 def to_d(precision=0) BigDecimal(self) unless self.imag.zero? # to raise error BigDecimal(self.real, precision) end
Returns the value as a BigDecimal. If the imaginary part is not 0, an error is raised
The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' Complex(0.1234567, 0).to_d(4) # => 0.1235e0 Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1 Complex(1, 1).to_d # raises ArgumentError
See also Kernel.BigDecimal.
Returns the value as a BigDecimal. If the imaginary part is not 0, an error is raised
The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' Complex(0.1234567, 0).to_d(4) # => 0.1235e0 Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1 Complex(1, 1).to_d # raises ArgumentError
See also Kernel.BigDecimal.
Returns the value as a BigDecimal.
The precision parameter is required for a rational complex number. This parameter is used to determine the number of significant digits for the result.
require 'bigdecimal' require 'bigdecimal/util' Complex(0.1234567, 0).to_d(4) # => 0.1235e0 Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
See also Kernel.BigDecimal.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 756
def to_f: () -> Float
Returns the value of self.real as a Float, if possible:
Complex.rect(1, 0).to_f # => 1.0 Complex.rect(1, Rational(0, 1)).to_f # => 1.0
Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, _n_)).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 770
def to_i: () -> Integer
Returns the value of self.real as an Integer, if possible:
Complex.rect(1, 0).to_i # => 1 Complex.rect(1, Rational(0, 1)).to_i # => 1
Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, _n_)).
(?JSON::State? state) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/json-2.21.2/lib/json/add/complex.rb, line 48 def to_json(*args) as_json.to_json(*args) end
Returns a JSON string representing self:
require 'json/add/complex' puts Complex(2).to_json puts Complex(2.0, 4).to_json
Output:
{"json_class":"Complex","r":2,"i":0}
{"json_class":"Complex","r":2.0,"i":4}
Returns a JSON string representing self:
require 'json/add/complex' puts Complex(2).to_json puts Complex(2.0, 4).to_json
Output:
{"json_class":"Complex","r":2,"i":0}
{"json_class":"Complex","r":2.0,"i":4}
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 788
def to_r: () -> Rational
Returns the value of self.real as a Rational, if possible:
Complex.rect(1, 0).to_r # => (1/1) Complex.rect(1, Rational(0, 1)).to_r # => (1/1) Complex.rect(1, 0.0).to_r # => (1/1)
Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, _n_)) and self.imag.to_r is not exactly zero.
Related: Complex#rationalize.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/complex.rbs, line 802
def to_s: () -> String
Returns a string representation of self:
Complex.rect(2).to_s # => "2+0i" Complex.rect(-8, 6).to_s # => "-8+6i" Complex.rect(0, Rational(1, 2)).to_s # => "0+1/2i" Complex.rect(0, Float::INFINITY).to_s # => "0+Infinity*i" Complex.rect(Float::NAN, Float::NAN).to_s # => "NaN+NaN*i"