class Complex
クラスの継承リスト: Complex < Numeric < Comparable < Object < Kernel < BasicObject
要約
複素数を扱うクラスです。
Complex オブジェクトを作成するには、Kernel.#Complex、 Complex.rect、Complex.polar、Numeric#to_c、 String#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(r, theta = 0) -> Complex
-
絶対値が r、偏角が theta である Complex クラスのオブジェクトを生成します。
- [PARAM] r:
- 生成する複素数の絶対値。
- [PARAM] theta:
- 生成する複素数の偏角。単位はラジアンです。省略した場合は 0 です。
例:
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
rectangular(r, i = 0) -> Complex
-
実部が r、虚部が i である Complex クラスのオブジェクトを生成します。
- [PARAM] r:
- 生成する複素数の実部。
- [PARAM] i:
- 生成する複素数の虚部。省略した場合は 0 です。
例:
Complex.rect(1) # => (1+0i) Complex.rect(1, 2) # => (1+2i) Complex.rectangular(1, 2) # => (1+2i)
[SEE_ALSO] Kernel.#Complex
インスタンスメソッド
self * other -> Complex
-
積を計算します。
- [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
-
冪(べき)乗を計算します。
- [PARAM] other:
- 自身を other 乗する数
例:
Complex('i') ** 2 # => (-1+0i)
self + other -> Complex
-
和を計算します。
- [PARAM] other:
- 自身に足す数
例:
Complex(1, 2) + Complex(2, 3) # => (3+5i)
self - other -> Complex
-
差を計算します。
- [PARAM] other:
- 自身から引く数
例:
Complex(1, 2) - Complex(2, 3) # => (-1-1i)
- -> Complex
-
自身の符号を反転させたものを返します。
例:
-Complex(1) # => (-1+0i) -Complex(-1, 1) # => (1-1i)
self / other -> Complex
quo(other) -> Complex
-
商を計算します。
- [PARAM] other:
- 自身を割る数
例:
Complex(10.0) / 3 # => (3.3333333333333335+(0/1)*i) Complex(10) / 3 # => ((10/3)+(0/1)*i)
self == other -> bool
-
数値として等しいか判定します。
- [PARAM] other:
- 自身と比較する数値
例:
Complex(2, 1) == Complex(1) # => false Complex(1, 0) == Complex(1) # => true Complex(1, 0) == 1 # => true
abs -> Float
magnitude -> Float
-
自身の絶対値を返します。
以下の計算の結果を 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
-
自身の絶対値の 2 乗を返します。
以下の計算の結果を返します。
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
angle -> Float
phase -> Float
-
自身の偏角を[-π,π]の範囲で返します。
例:
Complex.polar(3, Math::PI/2).arg # => 1.5707963267948966
非正の実軸付近での挙動に注意してください。以下の例のように虚部が 0.0 と -0.0 では値が変わります。
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
coerce(other) -> [Complex, Complex]
-
other を Complex に変換して [self, 変換後の other] の配列を返します。
- [EXCEPTION] TypeError:
- 変換できないオブジェクトを指定した場合に発生します。
例:
Complex(1).coerce(2) # => [(2+0i), (1+0i)]
conjugate -> Complex
conj -> Complex
-
自身の共役複素数を返します。
例:
Complex(1, 2).conj # => (1-2i)
denominator -> Integer
-
分母を返します。
以下のように、実部と虚部の分母の最小公倍数を整数で返します。
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
-
自身と other の実部と虚部のクラスが等しく、それぞれが数値として等しい場 合に true を返します。そうでない場合に false を返します。
- [PARAM] other:
- 自身と比較する数値
fdiv(other) -> Complex
-
自身の実部と虚部をそれぞれ実数として other で割った商を返します。
- [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
-
自身のハッシュ値を返します。
imag -> Numeric
imaginary -> Numeric
-
自身の虚部を返します。
例:
Complex(3, 2).imag # => 2
inspect -> String
-
自身を人間が読みやすい形の文字列表現にして返します。
"(1+1i)", "(1-1i)" のような文字列を返します。
marshal_dump -> Array
-
Complex#marshal_load で復元可能な配列を返します。
[SEE_ALSO] Complex#marshal_load
marshal_load -> Complex
-
Complex#marshal_dump で得られた配列を基に、Complex オブジェ クトを復元します。
[SEE_ALSO] Complex#marshal_dump
numerator -> Complex
-
分子を返します。
例:
Complex('1/2+2/3i').numerator # => (3+4i) Complex(3).numerator # => (3+0i)
[SEE_ALSO] Complex#denominator
polar -> [Numeric, Numeric]
-
自身の絶対値と偏角を配列にして返します。
例:
Complex.polar(1, 2).polar # => [1, 2]
to_r -> Rational
rationalize -> Rational
rationalize(eps) -> Rational
-
自身を Rational に変換します。
- [PARAM] eps:
- 許容する誤差。常に無視されます。
- [EXCEPTION] RangeError:
- 虚部が 0 ではない場合に発生します。
例:
Complex(3).to_r # => (3/1) Complex(3, 2).to_r # => RangeError
real -> Numeric
-
自身の実部を返します。
例:
Complex(3, 2).real # => 3
real? -> false
-
常に false を返します。
[SEE_ALSO] Numeric#real?
rect -> [Numeric, Numeric]
rectangular -> [Numeric, Numeric]
-
実部と虚部を配列にして返します。
例:
Complex(3).rect # => [3, 0] Complex(3.5).rect # => [3.5, 0] Complex(3, 2).rect # => [3, 2]
to_f -> Float
-
自身を Float に変換します。
- [EXCEPTION] RangeError:
- 虚部が 0 ではない場合に発生します。
例:
Complex(3).to_f # => 3.0 Complex(3.5).to_f # => 3.5 Complex(3, 2).to_f # => RangeError
to_i -> Integer
-
自身を整数に変換します。
- [EXCEPTION] RangeError:
- 虚部が 0 ではない場合に発生します。
例:
Complex(3).to_i # => 3 Complex(3).to_i # => 3 Complex(3.5).to_i # => 3 Complex(3, 2).to_i # => RangeError
to_s -> String
-
自身を "実部 + 虚部i" 形式の文字列にして返します。
定数
I -> Complex
-
虚数単位です。(0+1i) を返します。
- Ruby 1.9.3 リファレンスマニュアル
- はじめに
- コマンド
- Rubyの起動
- 環境変数
- Ruby言語仕様
- 組み込みライブラリ
- クラス
- Array
- BasicObject
- Bignum
- Binding
- Class
- Complex
- Data
- Dir
- Encoding
- Encoding::
Converter - Enumerator
- FalseClass
- Fiber
- File
- File::Stat
- Fixnum
- Float
- Hash
- IO
- Integer
- MatchData
- Method
- Module
- Mutex
- NilClass
- Numeric
- Object
- Proc
- Process::Status
- Random
- Range
- Rational
- Regexp
- String
- Struct
- Struct::Tms
- Symbol
- Thread
- ThreadGroup
- Time
- TrueClass
- UnboundMethod
- モジュール
- オブジェクト
- 例外クラス
- 組み込み関数
(Kernelモジュール関数) - 組み込み定数
(Kernel定数) - 組み込み特殊変数
- クラス
- 標準添付ライブラリ
- pack テンプレート文字列
- sprintf フォーマット
- Ruby用語集
- Rubyで使われる記号の意味
- 疑似BNFによるRubyの文法
- バージョン情報 / このリファレンスについて