VivekDev
VivekDev

Reputation: 25369

Creating a Matrix in MathDotNet given a 2D array

I have the following code got from this site as a download from here. The full path after the zip file is extracted would be some thing like E:\Csharp2Dand3DTestbed\GraphicsBook\LA\LA\MatrixTransform2.cs

protected static double[,] MatrixInverse(double[,] mat)
{
    Matrix m = new Matrix(mat);
    Matrix k = m.Inverse();
    return k;
}

But that does not compile. I see from here that I need to do something like

protected static double[,] MatrixInverse(double[,] mat)
{
    Matrix<double> m = Matrix<double>.Build.WhatHere(???)(3, 4); // How with existing matrix
    Matrix k = m.Inverse();
    return k;
}

Can someone please guide me. I am not able to go further. I am using the latest version of the Math.NET Numerics

Upvotes: 0

Views: 1356

Answers (1)

Christoph R&#252;egg
Christoph R&#252;egg

Reputation: 4726

From double[,] to matrix (two options):

var matrix = Matrix<double>.Build.DenseOfArray(array);
var matrix = CreateMatrix.DenseOfArray(array);

From matrix to double[,]:

var array = matrix.ToArray();

Note that these involve a full copy since matrices do not use 2D arrays internally. There are more examples on this in the documentation.

Upvotes: 2

Related Questions