在 Ruby 1.9.3 中,一个向量不能与另一个向量相乘?

zis*_*ign 5 ruby vector

我正在 Ruby 1.9.3 中实现基本的机器学习算法。

我尝试使用类 Matrix 和 Vector 进行算术运算。但是当我尝试将一个向量乘以另一个向量时,它会显示“ExceptionForMatrix::ErrOperationNotDefined: Operation(*) can't be defined: Vector op Vector”。

但是 Ruby 的文档 1.9,3

*(x) 将向量乘以 x,其中 x 是一个数字或另一个向量。

我的代码和输出在这里:

> a = Vector[1,2,3]
=> Vector[1, 2, 3]
> b = Vector[1,2,3]
=> Vector[1, 2, 3]
> a * b
ExceptionForMatrix::ErrOperationNotDefined: Operation(*) can't be defined: Vector op Vector
Run Code Online (Sandbox Code Playgroud)

und*_*gor 2

文档是错误的。当您查看文档中链接的代码时,有

def *(x)
  case x
  when Numeric
    els = @elements.collect{|e| e * x}
    Vector.elements(els, false)
  when Matrix
    Matrix.column_vector(self) * x
  when Vector
    Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
  else
    apply_through_coercion(x, __method__)
  end
end
Run Code Online (Sandbox Code Playgroud)

无论如何,将(列)向量乘以(列)向量(如矩阵乘法)是没有意义的。也许你想要inner_product