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

class Complex

クラス・モジュールの継承リスト: Complex < Numeric < Comparable < Object < Kernel < BasicObject

要約

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

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)

特異メソッド

定義 説明
polar(r, theta = 0) -> Complex

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

rect(r, i = 0) -> Complex
rectangular(r, i = 0) -> Complex

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

インスタンスメソッド

定義 説明
self * other -> Complex

積を計算します。

self ** other -> Complex

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

self + other -> Complex

和を計算します。

self - other -> Complex

差を計算します。

- self -> Complex

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

self / other -> Complex
quo(other) -> Complex

商を計算します。

self == other -> bool

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

abs -> Numeric
magnitude -> Numeric

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

abs2 -> Numeric

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

arg -> Float
angle -> Float
phase -> Float

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

coerce(other) -> [Complex, Complex]

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

conjugate -> Complex
conj -> Complex

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

denominator -> Integer

分母を返します。

fdiv(other) -> Complex

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

finite? -> bool

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

imag -> Numeric
imaginary -> Numeric

自身の虚部を返します。

infinite? -> nil | 1

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

inspect -> String

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

numerator -> Complex

分子を返します。

polar -> [Numeric, Numeric]

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

to_r -> Rational
rationalize -> Rational
rationalize(eps) -> Rational

自身を Rational に変換します。

real -> Numeric

自身の実部を返します。

real? -> false

常に false を返します。

rect -> [Numeric, Numeric]
rectangular -> [Numeric, Numeric]

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

to_c -> self

self を返します。

to_f -> Float

自身を Float に変換します。

to_i -> Integer

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

to_s -> String

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

privateメソッド

定義 説明
marshal_dump -> Array

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

定数

定義 説明
I -> Complex

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

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined % +@ <=> ceil div divmod eql? floor i integer? negative? nonzero? positive? remainder round step to_int truncate zero? !~ === =~ _dump class clone define_singleton_method display enum_for equal? extend freeze frozen? hash initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap then to_a to_ary to_hash to_io to_proc to_regexp to_str trust untaint untrust untrusted? .new .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE

追加されるメソッド

定義 説明 ライブラリ
json_create(hash) -> Complex

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

json/add/complex
to_json(*args) -> String

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

json/add/complex