Ruby 1.9.2 リファレンスマニュアル > ライブラリ一覧 > matrixライブラリ > Vectorクラス
クラスの継承リスト: Vector < Enumerable < Object < Kernel < BasicObject
数 Numeric を要素とするベクトルを扱うクラスです。
require 'complex' することによって、 Vector オブジェクトの要素は Complex クラスに拡張されます。 多くのメソッドは、この拡張されたVectorクラスでも、期待通りに動作します。
次の例は、各要素を共役複素数に置換するメソッド (Vector#conjugate)です。
require 'matrix' require 'complex' class Vector def conjugate collect{|e| e.conjugate } end end v1 = Vector[Complex(1,1),Complex(2,2),Complex(3,3)] v2 = v1.conjugate p v2 #=> Vector[Complex(1,-1),Complex(2,-2),Complex(3,-3)] v3 = v1+v2 p v3 #=> Vector[Complex(1,0),Complex(2,0),Complex(3,0)]
しかし、Complex 要素に拡張された Vector クラスで、 期待通りに動作しないメソッドもあります。 例えば、ベクトルの絶対値を求める Vector#r メソッドは、 各要素の2乗和の平方根 Math.#sqrt を求めますが、 このとき例外を発生させる可能性があります。
複素数を要素とするベクトルの絶対値を求めるためには、 各要素の絶対値の2乗和をとらなくてはなりません(次の例 Vector#absメソッド)。
require 'matrix' require 'complex' class Vector def abs r=0 @elements.each{|e| r += e.abs2 } Math.sqrt(r) end end v = Vector[Complex(1,1),Complex(2,2),Complex(3,3)} p v.abs #=> 5.291502622 # Math.sqrt(28) p v.r #=> 'sqrt': undefined method `Rational'
定義 | 説明 | |
---|---|---|
self[*a] -> Vector
|
可変個引数を要素とするベクトルを生成します。 |
|
elements(a, copy = true) -> Vector
|
配列 a を要素とするベクトルを生成します。 ただし、オプション引数 copy が偽 (false) ならば、複製を行いません。 |
定義 | 説明 | |
---|---|---|
self * a -> Vector
|
数 a を各要素に乗じたベクトルを返します。 |
|
self * m -> Matrix
|
自分自身を列ベクトル(行列)に変換して (実際には Matrix.column_vector(self) を適用) から、行列 m を右から乗じた行列 (Matrix クラス) を返します。 |
|
self + v -> Vector
|
ベクトル v を加えたベクトルを返します。 |
|
self - v -> Vector
|
ベクトル v を減じたベクトルを返します。 |
|
self == v -> bool
|
自分自身と引数 v を比較し、true/false を返します。 |
|
self[i] -> object | nil
|
i 番目の要素を返します。インデックスは 0 から開始します。 要素が存在しないインデックスを指定した時には nil を返します。 |
|
clone() -> Vector
|
自分自身をコピーしたベクトルを返します。 |
|
coerce(other) -> Array
|
self と other が同じクラスになるよう、自分自身か other を変換し [other, self] という配列にして返します。 |
|
collect {|x| ... } -> Vector
|
ベクトルの各要素に対してブロックを評価した結果を、要素として持つベクトルを生成します。 |
|
collect2(v) {|x, y| ... } -> Array
|
ベクトルの各要素と引数 v の要素との組に対してブロックを評価し、その結果を要素として持つ配列を返します。 |
|
compare_by(elements) -> bool
|
自分自身と引数 elements を配列として比較します。 |
|
covector -> Matrix
|
Matrix オブジェクトへ変換します。 |
|
each2(v) {|x, y| ... }
|
ベクトルの各要素と、それに対応するインデックスを持つ引数 v の要素との組に対して (2引数の) ブロックを繰返し評価します。`v' は size メソッドと [] メソッドを持つオブジェクトです。 |
|
elements_to_f -> Vector
|
ベクトルの各成分をFloatに変換したベクトルを返します。 |
|
elements_to_i -> Vector
|
ベクトルの各成分をIntegerに変換したベクトルを返します。 |
|
elements_to_r -> Vector
|
ベクトルの各成分をRationalに変換したベクトルを返します。 |
|
hash() -> Fixnum
|
自分自身のハッシュ値を返します。 |
|
init_elements(array, copy) -> Array
|
ベクトルの要素を引数 array で初期化します。 |
|
inner_product(v) -> Float
|
ベクトル v との内積を返します。 |
|
inspect() -> String
|
オブジェクトの内容を文字列化します。 |
|
map2(v) {|x, y| ... } -> Vector
|
ベクトルの各要素と引数 v の要素との組に対してブロックを評価し、その結果を要素として持つベクトルを返します。 |
|
r -> Float
|
自身の大きさ(ノルム)を返します。 ただし、要素の自乗和の平方根(Math.sqrt)を計算しているので、 要素が複素数の場合は一般に正しい値(要素の絶対値自乗の和の平方根) を返しません。 |
|
size() -> Fixnum
|
ベクトルの要素数(次元)を返します。 |
|
to_a -> Array
|
||
to_s -> String
|
!
!=
all?
any?
chunk
collect_concat
count
cycle
detect
drop
drop_while
each_cons
each_entry
each_slice
each_with_index
each_with_object
entries
find_all
find_index
first
grep
group_by
include?
inject
max
max_by
min
min_by
minmax
minmax_by
none?
one?
partition
reject
reverse_each
slice_before
sort
sort_by
take
take_while
to_set
zip
===
=~
__id__
__send__
_dump
class
clone
dclone
display
enum_for
eql?
equal?
extend
freeze
frozen?
initialize
initialize_copy
instance_eval
instance_exec
instance_of?
instance_variable_defined?
instance_variable_get
instance_variable_set
instance_variables
is_a?
marshal_dump
marshal_load
method
method_missing
methods
must_be
must_be_close_to
must_be_empty
must_be_instance_of
must_be_kind_of
must_be_nil
must_be_same_as
must_be_within_epsilon
must_equal
must_include
must_match
must_raise
must_respond_to
must_send
must_throw
nil?
pretty_inspect
pretty_print
pretty_print_cycle
pretty_print_inspect
pretty_print_instance_variables
private_methods
protected_methods
public_methods
remove_instance_variable
respond_to?
respond_to_missing?
singleton_class
singleton_method_added
singleton_method_removed
singleton_method_undefined
singleton_methods
taint
tainted?
tap
to_ary
to_hash
to_int
to_io
to_proc
to_regexp
to_str
to_yaml
to_yaml_properties
to_yaml_style
trust
untaint
untrust
untrusted?
wont_be
wont_be_close_to
wont_be_empty
wont_be_instance_of
wont_be_kind_of
wont_be_nil
wont_be_same_as
wont_be_within_epsilon
wont_equal
wont_include
wont_match
wont_respond_to
.new