new user question

Jan 29, 2013 at 8:18 PM
Edited Jan 29, 2013 at 8:19 PM


I am new to Math.NET Numerics and want to know the proper way to create vectors and matrices of both double and complex in the same file without prepending the namespace.  For example, is this the only way to create things?

Vector<double> a = new MathNet.Numerics.LinearAlgebra.Double.DenseVector(new double[] { 1, 2, 3 });
Vector<double> b = new MathNet.Numerics.LinearAlgebra.Double.DenseVector(new double[] { 4, 5, 6 });
var c = a + b;
var d = a * b;

//Matrix<Complex> mat = new DenseMatrix(2, 3);

Matrix<Complex> mat = new MathNet.Numerics.LinearAlgebra.Complex.DenseMatrix(2, 3 );

Matrix<Complex> mat2 = new MathNet.Numerics.LinearAlgebra.Complex.DenseMatrix(1,3, 
                                        new Complex[] { new Complex(1.0, 0.0), 
                                                        new Complex(2.0,0.0),
                                                        new Complex(3.0, 0.0)});
Is there a way to just do the following:
Matrix<Complex> mat = new Matrix<Complex>(2,3);
Jan 29, 2013 at 11:53 PM


There's no need to write the full namespace, just use relative ones as usual. For example:

using MathNet.Numerics.LinearAlgebra;

var a = new Double.DenseVector(new double[] ...);
var mat = new Complex.DenseMatrix(2,3);

Nevertheless, you may still want to include the .Generic, .Double and .Complex namespaces as well, if not just to get the extension methods.


Jan 30, 2013 at 6:45 AM
Edited Jan 30, 2013 at 6:46 AM

You could also use using as a sorta typedef within a file:

using Vector = MathNet.Numerics.LinearAlgebra.Double.DenseVector;
using Matrix = MathNet.Numerics.LinearAlgebra.Complex.DenseMatrix;


var v = new Vector(5);
var m = new Matrix(5, 5);

This should also get you access to the extension methods.

Marked as answer by cdrnet on 10/3/2013 at 5:50 PM
Jan 30, 2013 at 3:35 PM

Hi Christoph,

When I try your suggestion,

var a = new Double.DenseVector(new double[] {1,2,3});

I get the following error: "The type name 'DenseMatrix' does not exist in the type 'double'"

I guess this is what I expected since it does not know to use MathNet.Numerics.LinearAlgebra.Double.DenseVector.  It thinks it is System.Double.  I understand what Cuda has suggested but I wanted to know if there is any other alternative.



Jan 30, 2013 at 5:39 PM
Edited Jan 30, 2013 at 5:39 PM

Sorry, my mistake - that trick only actually works if you're inside the MathNet.Numerics.LinearAlgebra namespace. Please consider cuda's approach instead with an alias, or more general e.g.:

using DoubleLA = MathNet.Numerics.LinearAlgebra.Double;
using ComplexLA = MathNet.Numerics.LinearAlgebra.Complex;

var a = new DoubleLA.DenseMatrix(3,4);


Jan 30, 2013 at 6:36 PM

Thank you for your help