2

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
MoonKnight
  • 23,214
  • 40
  • 145
  • 277

1 Answers1

3

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.

Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
  • `var` is your friend here. – John Alexiou Jan 02 '14 at 20:20
  • I understand that `var` could indeed be used here. However, the use of `var` in this case could introduce unwanted ambiguity between `Vector`s and `Matrix`s. Thanks... – MoonKnight Jan 03 '14 at 09:57
  • The actual type of that specific `var` is `Matrix` (assuming you work with doubles), so you can write that explicitly instead of var if needed for clarity. – Christoph Rüegg Jan 03 '14 at 10:01
  • I have found that I need to use: `using MathNet.Numerics.LinearAlgebra.Generic;` – MoonKnight Jan 03 '14 at 13:57
  • Ah yes, you're right. I should have stated more clearly that part of the answer only apply to v3, not to v2. – Christoph Rüegg Jan 03 '14 at 16:28
  • Where can I get version three from? – MoonKnight Jan 04 '14 at 13:55
  • 1
    @Killercam You can download the binaries of v3.0.0-alpha8 on [Codeplex](https://mathnetnumerics.codeplex.com/releases/view/119109), or just checkout the matching [tagged revision on Github](https://github.com/mathnet/mathnet-numerics/tree/v3.0.0-alpha8). – wip Mar 27 '14 at 07:09