Question

I have recently started using the MathNET library and it is awesome. I am working heavily with matrices and vectors. The library works great, but I am finding that I have to use casting all the time; an example of this would be:

using MathNet.Numerics.LinearAlgebra.Double;
...
Matrix P = (DenseMatrix)(T.Multiply(P.TransposeAndMultiply(T)) + 
    R.Multiply(Q.TransposeAndMultiply(R)));

or

using MathNet.Numerics.LinearAlgebra.Double;
...
Vector a, v, y;
Matrix F, K, Z;
...
Matrix FI = (DenseMatrix)F.Inverse();
if (FI != null)
    K = (DenseMatrix)K.Multiply(FI);
...
v = (DenseVector)(y - Z.Multiply(a));

Why do I have to cast using ((DenseMatrix)/(DenseVector)) and is there a way around having to do this for each operation?

Was it helpful?

Solution

Type hierarchy: Matrix<double> <- Double.Matrix <- Double.DenseMatrix

Do you actually need to explicitly use specialized types like Double.Matrix or Double.DenseMatrix instead of the generic matrix and vector types?

With v3 we recommend to always work with the generic types. This way there's no need to cast at all in most cases. Your example would then look like this:

using MathNet.Numerics.LinearAlgebra;
Matrix<double> P = T*P.TransposeAndMultiply(T) + R*Q.TransposeAndMultiply(R);

and

var FI = F.Inverse();
K = K*FI
v = y - Z*a

You can also use the new builders (Matrix<double>.Build) to construct matrices and vectors directly with the generic types, so the only namespaces to open is MathNet.Numerics.LinearAlgebra.

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