Class: Complex
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Numeric
|
|
Instance Chain:
self,
::Numeric,
::Comparable
|
|
Inherits: | Numeric |
Defined in: | complex.c |
Overview
A complex number can be represented as a paired real number with imaginary unit; a+bi. Where a is real part, b is imaginary part and i is imaginary unit. Real a equals complex a+0i mathematically.
Complex
object can be created as literal, and also by using Kernel.Complex, .rect, .polar or to_c method.
2+1i #=> (2+1i)
Complex(1) #=> (1+0i)
Complex(2, 3) #=> (2+3i)
Complex.polar(2, 3) #=> (-1.9799849932008908+0.2822400161197344i)
3.to_c #=> (3+0i)
You can also create complex object from floating-point numbers or strings.
Complex(0.3) #=> (0.3+0i)
Complex('0.3-0.5i') #=> (0.3-0.5i)
Complex('2/3+3/4i') #=> ((2/3)+(3/4)*i)
Complex('1@2') #=> (-0.4161468365471424+0.9092974268256817i)
0.3.to_c #=> (0.3+0i)
'0.3-0.5i'.to_c #=> (0.3-0.5i)
'2/3+3/4i'.to_c #=> ((2/3)+(3/4)*i)
'1@2'.to_c #=> (-0.4161468365471424+0.9092974268256817i)
A complex object is either an exact or an inexact number.
Complex(1, 1) / 2 #=> ((1/2)+(1/2)*i)
Complex(1, 1) / 2.0 #=> (0.5+0.5i)
Constant Summary
-
I =
The imaginary unit.
f_complex_new_bang2(rb_cComplex, ZERO, ONE)
Class Method Summary
-
.polar(abs[, arg]) ⇒ Complex
Returns a complex object which denotes the given polar form.
-
.rect(real[, imag]) ⇒ Complex
(also: .rectangular)
Returns a complex object which denotes the given rectangular form.
-
.rectangular(real[, imag]) ⇒ Complex
Alias for .rect.
Instance Attribute Summary
-
#finite? ⇒ Boolean
readonly
Returns
true
ifcmp
's magnitude is finite number, oterwise returnsfalse
. -
#infinite? ⇒ Boolean
readonly
Returns values corresponding to the value of
cmp
's magnitude: -
#real ⇒ Numeric
readonly
Returns the real part.
-
#real? ⇒ Boolean
readonly
Returns false.
::Numeric - Inherited
#finite? | Return true if |
#infinite? | Returns values corresponding to the value of |
#integer? | Returns |
#negative? | Returns |
#nonzero? | Returns |
#positive? | Returns |
#real | Returns self. |
#real? | Returns |
#zero? | Returns |
Instance Method Summary
- #*
-
#**(numeric) ⇒ Complex
Performs exponentiation.
-
#+(numeric) ⇒ Complex
Performs addition.
-
#-(numeric) ⇒ Complex
Performs subtraction.
-
#- ⇒ Complex
Returns negation of the value.
-
#/(numeric) ⇒ Complex
Performs division.
-
#==(object) ⇒ Boolean
Returns true if cmp equals object numerically.
-
#abs ⇒ Numeric
(also: #magnitude)
Returns the absolute part of its polar form.
-
#abs2 ⇒ Numeric
Returns square of the absolute value.
-
#angle ⇒ Float
Alias for #arg.
-
#arg ⇒ Float
(also: #angle, #phase)
Returns the angle part of its polar form.
-
#conj ⇒ Complex
Alias for #~.
-
#conjugate ⇒ Complex
Alias for #~.
-
#denominator ⇒ Integer
Returns the denominator (lcm of both denominator - real and imag).
-
#fdiv(numeric) ⇒ Complex
Performs division as each part is a float, never returns a float.
-
#imag ⇒ Numeric
(also: #imaginary)
Returns the imaginary part.
-
#imaginary ⇒ Numeric
Alias for #imag.
-
#inspect ⇒ String
Returns the value as a string for inspection.
-
#magnitude ⇒ Numeric
Alias for #abs.
-
#numerator ⇒ Numeric
Returns the numerator.
-
#phase ⇒ Float
Alias for #arg.
-
#polar ⇒ Array
Returns an array; [cmp.abs, cmp.arg].
- #quo
-
#rationalize([eps]) ⇒ Rational
Returns the value as a rational if possible (the imaginary part should be exactly zero).
-
#rect ⇒ Array
(also: #rectangular)
Returns an array; [cmp.real, cmp.imag].
-
#rectangular ⇒ Array
Alias for #rect.
-
#to_c ⇒ self
Returns self.
-
#to_f ⇒ Float
Returns the value as a float if possible (the imaginary part should be exactly zero).
-
#to_i ⇒ Integer
Returns the value as an integer if possible (the imaginary part should be exactly zero).
-
#to_r ⇒ Rational
Returns the value as a rational if possible (the imaginary part should be exactly zero).
-
#to_s ⇒ String
Returns the value as a string.
-
#conj ⇒ Complex
(also: #conjugate, #conj)
Returns the complex conjugate.
::Numeric - Inherited
#% | x.modulo(y) means x-y*(x/y).floor. |
#+@ | Unary Plus—Returns the receiver's value. |
#-@ | Unary Minus—Returns the receiver's value, negated. |
#<=> | Returns zero if |
#abs | Returns the absolute value of |
#abs2 | Returns square of self. |
#angle | Alias for Numeric#arg. |
#arg | Returns 0 if the value is positive, pi otherwise. |
#ceil | Returns the smallest possible ::Integer that is greater than or equal to |
#coerce, | |
#conj | Returns self. |
#conjugate | Alias for Numeric#conj. |
#denominator | Returns the denominator (always positive). |
#div | Uses #/ to perform division, then converts the result to an integer. |
#divmod | Returns an array containing the quotient and modulus obtained by dividing |
#eql? | Returns |
#fdiv | Returns float division. |
#floor | Returns the largest integer less than or equal to |
#i | Returns the corresponding imaginary number. |
#imag | Returns zero. |
#imaginary | Alias for Numeric#imag. |
#initialize_copy | Numerics are immutable values, which should not be copied. |
#magnitude | Alias for Numeric#abs. |
#modulo | Alias for Numeric#%. |
#numerator | Returns the numerator. |
#phase | Alias for Numeric#arg. |
#polar | Returns an array; [num.abs, num.arg]. |
#quo | Returns most exact division (rational for integers, float for floats). |
#rect | Returns an array; [num, 0]. |
#rectangular | Alias for Numeric#rect. |
#remainder | x.remainder(y) means x-y*(x/y).truncate. |
#round | Rounds |
#singleton_method_added | Trap attempts to add methods to ::Numeric objects. |
#step | Invokes the given block with the sequence of numbers starting at |
#to_c | Returns the value as a complex. |
#to_int | Invokes the child class's #to_i method to convert |
#truncate | Returns |
::Comparable - Included
#< | Compares two objects based on the receiver's |
#<= | Compares two objects based on the receiver's |
#== | Compares two objects based on the receiver's |
#> | Compares two objects based on the receiver's |
#>= | Compares two objects based on the receiver's |
#between? | Returns |
#clamp | Returns min if obj |
Class Method Details
.polar(abs[, arg]) ⇒ Complex
.rect(real[, imag]) ⇒ Complex
.rectangular(real[, imag]) ⇒ Complex
Also known as: .rectangular
Complex
.rectangular(real[, imag]) ⇒ Complex
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
.rect(real[, imag]) ⇒ Complex
.rectangular(real[, imag]) ⇒ Complex
Complex
.rectangular(real[, imag]) ⇒ Complex
Alias for .rect.
Instance Attribute Details
#finite? ⇒ Boolean
(readonly)
Returns true
if cmp
's magnitude is finite number, oterwise returns false
.
#infinite? ⇒ Boolean
(readonly)
#real ⇒ Numeric (readonly)
Returns the real part.
Complex(7).real #=> 7
Complex(9, -4).real #=> 9
#real? ⇒ Boolean
(readonly)
Returns false.
Instance Method Details
#*
#**(numeric) ⇒ Complex
Performs exponentiation.
Complex('i') ** 2 #=> (-1+0i)
Complex(-8) ** Rational(1, 3) #=> (1.0000000000000002+1.7320508075688772i)
#+(numeric) ⇒ Complex
Performs addition.
Complex(2, 3) + Complex(2, 3) #=> (4+6i)
Complex(900) + Complex(1) #=> (901+0i)
Complex(-2, 9) + Complex(-9, 2) #=> (-11+11i)
Complex(9, 8) + 4 #=> (13+8i)
Complex(20, 9) + 9.8 #=> (29.8+9i)
#-(numeric) ⇒ Complex
Performs subtraction.
Complex(2, 3) - Complex(2, 3) #=> (0+0i)
Complex(900) - Complex(1) #=> (899+0i)
Complex(-2, 9) - Complex(-9, 2) #=> (7+7i)
Complex(9, 8) - 4 #=> (5+8i)
Complex(20, 9) - 9.8 #=> (10.2+9i)
#- ⇒ Complex
Returns negation of the value.
-Complex(1, 2) #=> (-1-2i)
#/(numeric) ⇒ Complex
#quo(numeric) ⇒ Complex
Complex
#quo(numeric) ⇒ Complex
Performs division.
Complex(2, 3) / Complex(2, 3) #=> ((1/1)+(0/1)*i)
Complex(900) / Complex(1) #=> ((900/1)+(0/1)*i)
Complex(-2, 9) / Complex(-9, 2) #=> ((36/85)-(77/85)*i)
Complex(9, 8) / 4 #=> ((9/4)+(2/1)*i)
Complex(20, 9) / 9.8 #=> (2.0408163265306123+0.9183673469387754i)
#==(object) ⇒ Boolean
Returns true if cmp equals object numerically.
Complex(2, 3) == Complex(2, 3) #=> true
Complex(5) == 5 #=> true
Complex(0) == 0.0 #=> true
Complex('1/3') == 0.33 #=> false
Complex('1/2') == '1/2' #=> false
Also known as: #magnitude
Returns the absolute part of its polar form.
Complex(-1).abs #=> 1
Complex(3.0, -4.0).abs #=> 5.0
#abs2 ⇒ Numeric
Returns square of the absolute value.
Complex(-1).abs2 #=> 1
Complex(3.0, -4.0).abs2 #=> 25.0
Alias for #arg.
Also known as: #angle, #phase
#conj ⇒ Complex
#conjugate ⇒ Complex
Complex
#conjugate ⇒ Complex
Alias for #~.
#conj ⇒ Complex
#conjugate ⇒ Complex
Complex
#conjugate ⇒ Complex
Alias for #~.
#denominator ⇒ Integer
Returns the denominator (lcm of both denominator - real and imag).
See numerator.
#fdiv(numeric) ⇒ Complex
Performs division as each part is a float, never returns a float.
Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
Also known as: #imaginary
Alias for #imag.
#inspect ⇒ String
Alias for #abs.
#numerator ⇒ Numeric
Returns the numerator.
1 2 3+4i <- numerator
- + -i -> ----
2 3 6 <- denominator
c = Complex('1/2+2/3i') #=> ((1/2)+(2/3)*i)
n = c.numerator #=> (3+4i)
d = c.denominator #=> 6
n / d #=> ((1/2)+(2/3)*i)
Complex(Rational(n.real, d), Rational(n.imag, d))
#=> ((1/2)+(2/3)*i)
See denominator.
Alias for #arg.
#polar ⇒ Array
Returns an array; [cmp.abs, cmp.arg].
Complex(1, 2).polar #=> [2.23606797749979, 1.1071487177940904]
#quo
#rationalize([eps]) ⇒ Rational
Returns the value as a rational if possible (the imaginary part should be exactly zero).
Complex(1.0/3, 0).rationalize #=> (1/3)
Complex(1, 0.0).rationalize # RangeError
Complex(1, 2).rationalize # RangeError
See to_r.
Also known as: #rectangular
Returns an array; [cmp.real, cmp.imag].
Complex(1, 2).rectangular #=> [1, 2]
Alias for #rect.
#to_c ⇒ self
Returns self.
Complex(2).to_c #=> (2+0i)
Complex(-8, 6).to_c #=> (-8+6i)
#to_f ⇒ Float
Returns the value as a float if possible (the imaginary part should be exactly zero).
Complex(1, 0).to_f #=> 1.0
Complex(1, 0.0).to_f # RangeError
Complex(1, 2).to_f # RangeError
#to_i ⇒ Integer
Returns the value as an integer if possible (the imaginary part should be exactly zero).
Complex(1, 0).to_i #=> 1
Complex(1, 0.0).to_i # RangeError
Complex(1, 2).to_i # RangeError
#to_r ⇒ Rational
Returns the value as a rational if possible (the imaginary part should be exactly zero).
Complex(1, 0).to_r #=> (1/1)
Complex(1, 0.0).to_r # RangeError
Complex(1, 2).to_r # RangeError
See rationalize.
#to_s ⇒ String
#conj ⇒ Complex
#conjugate ⇒ Complex
Also known as: #conjugate, #conj
Complex
#conjugate ⇒ Complex
Returns the complex conjugate.
Complex(1, 2).conjugate #=> (1-2i)