Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Complexクラス

# class Complex

クラスの継承リスト: Complex < Numeric < Comparable < Object < Kernel < BasicObject

## 要約

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

```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)
```

## 目次

polar rect rectangular
インスタンスメソッド
* ** + - -@ / quo == abs magnitude abs2 angle arg phase coerce conj conjugate denominator eql? fdiv hash imag imaginary inspect marshal_dump marshal_load numerator polar rationalize to_r real real? rect rectangular to_f to_i to_s

I

## 特異メソッド

`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+4i)
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 -> Float`[permalink][rdoc]
`magnitude -> Float`

```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 に変換して [self, 変換後の other] の配列を返します。

[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

`eql?(other) -> bool`[permalink][rdoc]

[PARAM] other:

`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

`hash -> Integer`[permalink][rdoc]

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

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

[SEE_ALSO] Numeric#imag

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

"(1+1i)", "(1-1i)" のような文字列を返します。

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

[注意] このメソッドは 2.0 で private メソッドに変更されました。

`marshal_load(ary) -> Complex`[permalink][rdoc]

Complex#marshal_dump で得られた配列を基に、Complex オブジェ クトを復元します。

[PARAM] ary:
2 要素の数値の配列を指定します。
[EXCEPTION] ArgumentError:

[注意] このメソッドは 2.0 で Complex::compatible#marshal_load に移動さ れました。

[SEE_ALSO] Complex#marshal_dump

`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]

[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_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]

## 定数

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