vector-r.patch

5.5 (5 5), 12/18/2009 07:11 pm

Download (906 Bytes)

refm/api/src/matrix/Vector (working copy)
233 233

  
234 234
--- r -> Float
235 235

  
236
ベクトルの絶対値を返します。
237
ベクトルの絶対値は、各要素の2乗の和の正の平方根を返します。
236
自身の大きさ(ノルム)を返します。
238 237

  
238
ただし、要素の自乗和の平方根(Math.sqrt)を計算しているので、
239
要素が複素数の場合は一般に正しい値(要素の絶対値自乗の和の平方根)
240
を返しません。
241

  
242
 Vector[3, 4].r # => 5.0
243
 Vector[Complex(0, 1), 0].r # => Complex(0.0, 1.0)   正しくは 1.0
244

  
245
要素が複素数の場合にも対応したメソッドは以下のように定義できます。
246

  
247
  class Vector
248
    def norm
249
      Math.sqrt @elements.inject(0){|sum, z| sum+(z*z).abs}
250
    end
251
  end
252
  
253
  Vector[Complex(0, 1), 0].norm # => 1.0
254

  
239 255
--- collect {|x| ... } -> Vector
240 256
--- map {|x| ... } -> Vector
241 257