type=class
superclass=Object
included=Enumerable
extended=
dynamically_included=
dynamically_extended=
library=matrix
aliases=
aliasof=

数 [[c:Numeric]] を要素とするベクトルを扱うクラスです。
[[c:Vector]] オブジェクトは [[c:Matrix]] オブジェクトとの演算においては列ベクトルとして扱われます。

ベクトルの要素のインデックスは 0 から始まることに注意してください。





=== Complexクラスとの併用 Working with Complex class

require 'complex' することによって、
Vector オブジェクトの要素は [[c: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 クラスで、
期待通りに動作しないメソッドもあります。
例えば、ベクトルの絶対値を求める [[m:Vector#r]] メソッドは、
各要素の2乗和の平方根 [[m: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'
