このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 3.2 リファレンスマニュアル

class Complex

[edit]

要約

複素数を扱うクラスです。

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

Complex オブジェクトの作り方
p Complex(1)         # => (1+0i)
p Complex(2, 3)      # => (2+3i)
p Complex.polar(2, 3)  # => (-1.9799849932008908+0.2822400161197344i)
p Complex(0.3)       # => (0.3+0i)
p Complex('0.3-0.5i')  # => (0.3-0.5i)
p Complex('2/3+3/4i')  # => ((2/3)+(3/4)*i)
p Complex('1@2')     # => (-0.4161468365471424+0.9092974268256817i)
p 3.to_c             # => (3+0i)
p 0.3.to_c           # => (0.3+0i)
p '0.3-0.5i'.to_c    # => (0.3-0.5i)
p '2/3+3/4i'.to_c    # => ((2/3)+(3/4)*i)
p '1@2'.to_c         # => (-0.4161468365471424+0.9092974268256817i)

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

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

目次

特異メソッド
インスタンスメソッド
privateメソッド
追加されるメソッド
定数

継承しているメソッド

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

特異メソッド

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

絶対値が r、偏角が theta である Complex クラスのオブジェクトを生成します。

[PARAM] r:
生成する複素数の絶対値。
[PARAM] theta:
生成する複素数の偏角。単位はラジアンです。省略した場合は 0 です。
p Complex.polar(2.0)          # => (2.0+0.0i)
p Complex.polar(2.0, 0)       # => (2.0+0.0i)
p Complex.polar(2.0, Math::PI)  # => (-2.0+2.4492127076447545e-16i)
rect(r, i = 0) -> ComplexRuby 1.9.3 から[permalink][rdoc][edit]
rectangular(r, i = 0) -> Complex

実部が r、虚部が i である Complex クラスのオブジェクトを生成します。

[PARAM] r:
生成する複素数の実部。
[PARAM] i:
生成する複素数の虚部。省略した場合は 0 です。
p Complex.rect(1)         # => (1+0i)
p Complex.rect(1, 2)      # => (1+2i)
p Complex.rectangular(1, 2) # => (1+2i)

[SEE_ALSO] Kernel.#Complex

インスタンスメソッド

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

積を計算します。

[PARAM] other:
自身に掛ける数
p Complex(1, 2) * 2            # => (2+4i)
p Complex(1, 2) * Complex(2, 3)  # => (-4+7i)
p Complex(1, 2) * Rational(1, 2) # => ((1/2)+(1/1)*i)
self ** other -> Complex[permalink][rdoc][edit]

冪(べき)乗を計算します。

[PARAM] other:
自身を other 乗する数
p Complex('i') ** 2           # => (-1+0i)
self + other -> Complex[permalink][rdoc][edit]

和を計算します。

[PARAM] other:
自身に足す数
p Complex(1, 2) + Complex(2, 3) # => (3+5i)
self - other -> Complex[permalink][rdoc][edit]

差を計算します。

[PARAM] other:
自身から引く数
p Complex(1, 2) - Complex(2, 3) # => (-1-1i)
- self -> ComplexRuby 1.9.3 から[permalink][rdoc][edit]

自身の符号を反転させたものを返します。

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

商を計算します。

[PARAM] other:
自身を割る数
p Complex(10.0) / 3  # => (3.3333333333333335+(0/1)*i)
p Complex(10)   / 3  # => ((10/3)+(0/1)*i)

[SEE_ALSO] Numeric#quo

self <=> other -> -1 | 0 | 1 | nil[permalink][rdoc][edit]

self の虚部がゼロで other が実数の場合、 self の実部の <=> メソッドで other と比較した結果を返します。 other が Complex で虚部がゼロの場合も同様です。

その他の場合は nil を返します。

[PARAM] other:
自身と比較する数値
p Complex(2, 3)  <=> Complex(2, 3) #=> nil
p Complex(2, 3)  <=> 1           #=> nil
p Complex(2)     <=> 1           #=> 1
p Complex(2)     <=> 2           #=> 0
p Complex(2)     <=> 3           #=> -1
self == other -> bool[permalink][rdoc][edit]

数値として等しいか判定します。

[PARAM] other:
自身と比較する数値
p Complex(2, 1) == Complex(1) # => false
p Complex(1, 0) == Complex(1) # => true
p Complex(1, 0) == 1        # => true
abs -> Numeric[permalink][rdoc][edit]
magnitude -> NumericRuby 1.9.3 から

自身の絶対値を返します。

以下の計算の結果を Float オブジェクトで返します。

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

[SEE_ALSO] Complex#abs2

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

自身の絶対値の 2 乗を返します。

以下の計算の結果を返します。

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

[SEE_ALSO] Complex#abs

arg -> Float[permalink][rdoc][edit]
angle -> Float
phase -> FloatRuby 1.9.3 から

自身の偏角を[-π,π]の範囲で返します。

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

非正の実軸付近での挙動に注意してください。以下の例のように虚部が 0.0 と -0.0 では値が変わります。

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

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

[SEE_ALSO] Numeric#arg

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

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

[EXCEPTION] TypeError:
変換できないオブジェクトを指定した場合に発生します。
p Complex(1).coerce(2) # => [(2+0i), (1+0i)]
conjugate -> Complex[permalink][rdoc][edit]
conj -> Complex

自身の共役複素数を返します。

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

分母を返します。

以下のように、実部と虚部の分母の最小公倍数を整数で返します。

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

[SEE_ALSO] Complex#numerator

fdiv(other) -> ComplexRuby 1.9.3 から[permalink][rdoc][edit]

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

[PARAM] other:
自身を割る数
p Complex(11, 22).fdiv(3) # => (3.6666666666666665+7.333333333333333i)
p Complex(11, 22).quo(3)  # => ((11/3)+(22/3)*i)

[SEE_ALSO] Complex#quo

finite? -> boolRuby 2.4.0 から[permalink][rdoc][edit]

実部と虚部の両方が有限値の場合に true を、そうでない場合に false を返します。

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

[SEE_ALSO] Complex#infinite?

imag -> Numeric[permalink][rdoc][edit]
imaginary -> NumericRuby 1.9.3 から

自身の虚部を返します。

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

[SEE_ALSO] Numeric#imag

infinite? -> nil | 1Ruby 2.4.0 から[permalink][rdoc][edit]

実部と虚部のどちらも無限大ではない場合に nil を、そうでない場合に 1 を返します。

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

[SEE_ALSO] Complex#finite?

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

自身を人間が読みやすい形の文字列表現にして返します。

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

分子を返します。

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

[SEE_ALSO] Complex#denominator

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

自身の絶対値と偏角を配列にして返します。

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

[SEE_ALSO] Numeric#polar

to_r -> RationalRuby 1.9.3 から[permalink][rdoc][edit]
rationalize -> Rational
rationalize(eps) -> Rational

自身を Rational に変換します。

[PARAM] eps:
許容する誤差。常に無視されます。
[EXCEPTION] RangeError:
虚部が実数か、0 ではない場合に発生します。
p Complex(3).to_r  # => (3/1)
Complex(3, 2).to_r # ~> RangeError
real -> Numeric[permalink][rdoc][edit]

自身の実部を返します。

p Complex(3, 2).real # => 3
real? -> falseRuby 1.9.3 から[permalink][rdoc][edit]

常に false を返します。

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

[SEE_ALSO] Numeric#real?

rect -> [Numeric, Numeric]Ruby 1.9.3 から[permalink][rdoc][edit]
rectangular -> [Numeric, Numeric]

実部と虚部を配列にして返します。

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

[SEE_ALSO] Numeric#rect

to_c -> selfRuby 1.9.1 から[permalink][rdoc][edit]

self を返します。

p Complex(2).to_c    # => (2+0i)
p Complex(-8, 6).to_c  # => (-8+6i)
to_f -> FloatRuby 1.9.3 から[permalink][rdoc][edit]

自身を Float に変換します。

[EXCEPTION] RangeError:
虚部が実数か、0 ではない場合に発生します。
p Complex(3).to_f  # => 3.0
p Complex(3.5).to_f  # => 3.5
Complex(3, 2).to_f # ~> RangeError
to_i -> IntegerRuby 1.9.3 から[permalink][rdoc][edit]

自身を整数に変換します。

[EXCEPTION] RangeError:
虚部が実数か、0 ではない場合に発生します。
p Complex(3).to_i  # => 3
p Complex(3.5).to_i  # => 3
Complex(3, 2).to_i # ~> RangeError
to_s -> String[permalink][rdoc][edit]

自身を "実部 + 虚部i" 形式の文字列にして返します。

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

privateメソッド

marshal_dump -> ArrayRuby 1.9.3 から[permalink][rdoc][edit]

Marshal.#load のためのメソッドです。 Complex::compatible#marshal_load で復元可能な配列を返します。

2.0 以降では Marshal.#load で 1.8 系の Complex オブジェクトを保存した文字列も復元できます。

[注意] Complex::compatible は通常の方法では参照する事ができません。

追加されるメソッド

json_create(hash) -> ComplexRuby 2.1.0 から[permalink][rdoc][edit] [added by json/add/complex]

JSON のオブジェクトから Complex のオブジェクトを生成して返します。

[PARAM] hash:
実部をキー 'r'、虚部をキー 'i' に持つハッシュを指定します。
to_json(*args) -> StringRuby 2.1.0 から[permalink][rdoc][edit] [added by json/add/complex]

自身を JSON 形式の文字列に変換して返します。

内部的にはハッシュにデータをセットしてから JSON::Generator::GeneratorMethods::Hash#to_json を呼び出しています。

[PARAM] args:
引数はそのまま JSON::Generator::GeneratorMethods::Hash#to_json に渡されます。
require 'json/add/complex'
p (2+3i).to_json # => "{\"json_class\":\"Complex\",\"r\":2,\"i\":3}"

[SEE_ALSO] JSON::Generator::GeneratorMethods::Hash#to_json

定数

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

虚数単位です。(0+1i) を返します。