Dave Baldwin
10/9/2008 3:48:00 PM
[Note: parts of this message were removed to make it a legal post.]
On 9 Oct 2008, at 10:40, Dave Baldwin wrote:
> require 'matrix'
> m = Matrix.identity(4)
> v = Vector[5, 6, 7, 1]
> puts m * v
>
> gives the expected answer of [5, 6, 7, 1]
>
> but
> puts v * m
>
> gives
> ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
>
> because the Vector#* method promotes self to a column vector when
> the argument is found to be a Matrix. I think it should be
> promoting self to a row vector and making this change removes the
> exception and gives the correct result. The updated method is:
>
> def *(x)
> case x
> when Numeric
> els = @elements.collect{|e| e * x}
> Vector.elements(els, false)
> when Matrix
> Matrix.row_vector(self) * x
> else
> s, x = x.coerce(self)
> s * x
> end
> end
>
>
> Dave.
>
Further modification so it returns a Vector instead of a matrix
(Matrix.row_vector(self) * x).row(0)
Dave.