Question

This is really simple stuff but, as I am a noob with math.net, I may need to be pointed in the right direction:

let a = new DenseVector([| 5.0; 2.0; 3.0; |])
let m = new DenseMatrix(3, 3, 1.0)
let r = a * m 
let r2 = m * a

results in:

> r;;
val it : DenseVector = seq [10.0; 10.0; 10.0]
> r2;;
val it : DenseVector = seq [10.0; 10.0; 10.0]

Matrix-Vector multiplication takes too much liberty here. I need to enforce proper dimensionality checks. Should I just work with DenseMatrix, creating 1xn, nx1 matrices? This basically makes Vectors and DenseVectors redundant in my case.

Was it helpful?

Solution

The observed behavior of Matrix-Vector and Vector-Matrix multiplication in the library is by design. Distinguishing column and row vectors is not possible within mere Vector type, so explicit matrix-by-vector multiply and vector-by-matrix multiply operations simply have respective signatures:

Matrix.Multiply: Vector -> Vector // matrix-by-vector

and

Matrix.LeftMultiply: Vector -> Vector // vector-by-matrix

where in the first case both argument and result are assumed column vectors, and in the second case both are assumed row ones.

The library always assumes "right" vector orientation when chaining operations in more complex expressions, at the same time enforcing other dimensionality checks, indeed.

If you want for whatever reason to explicitly distinguishing row from column vectors and carry, if required, the burden of explicit transpositions, then using 1-by-n and n-by-1 matrices in place of row and column vectors respectively is the way to go. In this case if matrix dimensions do not agree the library will be throwing System.ArgumentException instead of silently assuming "right" transposition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top