# class Complex

## 要約

Complex オブジェクトを作成するには、Kernel.#ComplexComplex.rectComplex.polarNumeric#to_cString#to_c のいずれかを使用します。

```Complex オブジェクトの作り方
```
Complex(1)           # => (1+0i)
Complex(2, 3)        # => (2+3i)
Complex.polar(2, 3)  # => (-1.9799849932008908+0.2822400161197344i)
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)
3.to_c               # => (3+0i)
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)
``````

Complex オブジェクトは有理数の形式も実数の形式も扱う事ができます。

```例
```
Complex(1, 1) / 2    # => ((1/2)+(1/2)*i)
Complex(1, 1) / 2.0  # => (0.5+0.5i)
``````

インスタンスメソッド
privateメソッド

## 継承しているメソッド

Numericから継承しているメソッド

## 特異メソッド

`polar(r, theta = 0) -> Complex`[permalink][rdoc]

[PARAM] r:

[PARAM] theta:

```例
```
Complex.polar(2.0)            # => (2.0+0.0i)
Complex.polar(2.0, 0)         # => (2.0+0.0i)
Complex.polar(2.0, Math::PI)  # => (-2.0+2.4492127076447545e-16i)
``````
`rect(r, i = 0) -> Complex`[permalink][rdoc]
`rectangular(r, i = 0) -> Complex`

[PARAM] r:

[PARAM] i:

```例
```
Complex.rect(1)           # => (1+0i)
Complex.rect(1, 2)        # => (1+2i)
Complex.rectangular(1, 2) # => (1+2i)
``````

[SEE_ALSO] Kernel.#Complex

## インスタンスメソッド

`self * other -> Complex`[permalink][rdoc]

[PARAM] other:

```例
```
Complex(1, 2) * 2              # => (2+4i)
Complex(1, 2) * Complex(2, 3)  # => (-4+7i)
Complex(1, 2) * Rational(1, 2) # => ((1/2)+(1/1)*i)
``````
`self ** other -> Complex`[permalink][rdoc]

[PARAM] other:

```例
```
Complex('i') ** 2             # => (-1+0i)
``````
`self + other -> Complex`[permalink][rdoc]

[PARAM] other:

```例
```
Complex(1, 2) + Complex(2, 3) # => (3+5i)
``````
`self - other -> Complex`[permalink][rdoc]

[PARAM] other:

```例
```
Complex(1, 2) - Complex(2, 3) # => (-1-1i)
``````
`- self -> Complex`[permalink][rdoc]

```例
```
-Complex(1)     # => (-1+0i)
-Complex(-1, 1) # => (1-1i)
``````
`self / other -> Complex`[permalink][rdoc]
`quo(other) -> Complex`

[PARAM] other:

```例
```
Complex(10.0) / 3  # => (3.3333333333333335+(0/1)*i)
Complex(10)   / 3  # => ((10/3)+(0/1)*i)
``````

[SEE_ALSO] Numeric#quo

`self == other -> bool`[permalink][rdoc]

[PARAM] other:

```例
```
Complex(2, 1) == Complex(1) # => false
Complex(1, 0) == Complex(1) # => true
Complex(1, 0) == 1          # => true
``````
`abs -> Numeric`[permalink][rdoc]
`magnitude -> Numeric`

```sqrt(self.real ** 2 + self.imag ** 2)
```
```例
```
Complex(1, 2).abs         # => 2.23606797749979
Complex(3, 4).abs         # => 5.0
Complex('1/2', '1/2').abs # => 0.7071067811865476
``````

[SEE_ALSO] Complex#abs2

`abs2 -> Numeric`[permalink][rdoc]

```self.real ** 2 + self.imag ** 2
```
```例
```
Complex(1, 1).abs2         # => 2
Complex(1.0, 1.0).abs2     # => 2.0
Complex('1/2', '1/2').abs2 # => (1/2)
``````

[SEE_ALSO] Complex#abs

`arg -> Float`[permalink][rdoc]
`angle -> Float`
`phase -> Float`

```例
```
Complex.polar(3, Math::PI/2).arg # => 1.5707963267948966
``````

```例
```
Complex(-1, 0).arg              #=>  3.141592653589793
Complex(-1, -0).arg             #=>  3.141592653589793
Complex(-1, -0.0).arg           #=> -3.141592653589793

Complex(0, 0.0).arg             #=>  0.0
Complex(0, -0.0).arg            #=> -0.0
Complex(-0.0, 0).arg            #=>  3.141592653589793
Complex(-0.0, -0.0).arg         #=> -3.141592653589793
``````

[SEE_ALSO] Numeric#arg

`coerce(other) -> [Complex, Complex]`[permalink][rdoc]

other を Complex に変換して [変換後の other, self] の配列を返します。

[EXCEPTION] TypeError:

```例
```
Complex(1).coerce(2) # => [(2+0i), (1+0i)]
``````
`conjugate -> Complex`[permalink][rdoc]
`conj -> Complex`

```例
```
Complex(1, 2).conj # => (1-2i)
``````
`denominator -> Integer`[permalink][rdoc]

```1   2       3+4i  <-  numerator(分子)
- + -i  ->  ----
2   3        6    <-  denominator(分母)
```
```例
```
Complex('1/2+2/3i').denominator # => 6
Complex(3).numerator            # => 1
``````

[SEE_ALSO] Complex#numerator

`fdiv(other) -> Complex`[permalink][rdoc]

self を other で割った商を返します。実部と虚部が共に Float の値になります。

[PARAM] other:

```例
```
Complex(11, 22).fdiv(3) # => (3.6666666666666665+7.333333333333333i)
Complex(11, 22).quo(3)  # => ((11/3)+(22/3)*i)
``````

[SEE_ALSO] Complex#quo

`finite? -> bool`[permalink][rdoc]

```例
```
(1 + 1i).finite?                 # => true
(Float::INFINITY + 1i).finite?   # => false
``````

[SEE_ALSO] Complex#infinite?

`imag -> Numeric`[permalink][rdoc]
`imaginary -> Numeric`

```例
```
Complex(3, 2).imag # => 2
``````

[SEE_ALSO] Numeric#imag

`infinite? -> nil | 1`[permalink][rdoc]

```例
```
(1+1i).infinite?                   # => nil
(Float::INFINITY + 1i).infinite?   # => 1
``````

[SEE_ALSO] Complex#finite?

`inspect -> String`[permalink][rdoc]

```例
```
Complex(2).inspect                       # => "(2+0i)"
Complex('-8/6').inspect                  # => "((-4/3)+0i)"
Complex('1/2i').inspect                  # => "(0+(1/2)*i)"
Complex(0, Float::INFINITY).inspect      # => "(0+Infinity*i)"
Complex(Float::NAN, Float::NAN).inspect  # => "(NaN+NaN*i)"
``````
`numerator -> Complex`[permalink][rdoc]

```例
```
Complex('1/2+2/3i').numerator # => (3+4i)
Complex(3).numerator          # => (3+0i)
``````

[SEE_ALSO] Complex#denominator

`polar -> [Numeric, Numeric]`[permalink][rdoc]

```例
```
Complex.polar(1, 2).polar # => [1, 2]
``````

[SEE_ALSO] Numeric#polar

`to_r -> Rational`[permalink][rdoc]
`rationalize -> Rational`
`rationalize(eps) -> Rational`

[PARAM] eps:

[EXCEPTION] RangeError:

```例
```
Complex(3).to_r    # => (3/1)
Complex(3, 2).to_r # => RangeError
``````
`real -> Numeric`[permalink][rdoc]

```例
```
Complex(3, 2).real # => 3
``````
`real? -> false`[permalink][rdoc]

```例
```
(2+3i).real?   # => false
(2+0i).real?   # => false
``````

[SEE_ALSO] Numeric#real?

`rect -> [Numeric, Numeric]`[permalink][rdoc]
`rectangular -> [Numeric, Numeric]`

```例
```
Complex(3).rect    # => [3, 0]
Complex(3.5).rect  # => [3.5, 0]
Complex(3, 2).rect # => [3, 2]
``````

[SEE_ALSO] Numeric#rect

`to_c -> self`[permalink][rdoc]

self を返します。

```例
```
Complex(2).to_c      # => (2+0i)
Complex(-8, 6).to_c  # => (-8+6i)
``````
`to_f -> Float`[permalink][rdoc]

[EXCEPTION] RangeError:

```例
```
Complex(3).to_f    # => 3.0
Complex(3.5).to_f  # => 3.5
Complex(3, 2).to_f # => RangeError
``````
`to_i -> Integer`[permalink][rdoc]

[EXCEPTION] RangeError:

```例
```
Complex(3).to_i    # => 3
Complex(3.5).to_i  # => 3
Complex(3, 2).to_i # => RangeError
``````
`to_s -> String`[permalink][rdoc]

```例
```
Complex(2).to_s                       # => "2+0i"
Complex('-8/6').to_s                  # => "-4/3+0i"
Complex('1/2i').to_s                  # => "0+1/2i"
Complex(0, Float::INFINITY).to_s      # => "0+Infinity*i"
Complex(Float::NAN, Float::NAN).to_s  # => "NaN+NaN*i"
``````

## privateメソッド

`marshal_dump -> Array`[permalink][rdoc]

`I -> Complex`[permalink][rdoc]