3

Assuming I have:

double[] someArray = new [] { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44 };

Is there any out of the box way of creating a 4x4 matrix out of this array without having to split it into 4 arrays myself?

I know it is simple to do this, but I'm exploring how much is ready out of the box.

EDIT

Sorry for not being clear (thought title was):

What I'm wondering is if there is out of the box functionality with the Matrix builder in Math.NET Numerics. Something like:

Matrix<double> someMatrix = DenseMatrix.OfArray(columns: 4, rows: 4, data: someArray);

johnildergleidisson
  • 2,087
  • 3
  • 30
  • 50

1 Answers1

5

From looking at the documentation, you could use the constructor directly, or the function OfColumnMajor(int rows, int columns, IEnumerable<double> columnMajor), if your data is in column-major order.

The code would look like this:

//Using the constructor
Matrix<double> someMatrix = new DenseMatrix(4, 4, someArray)

//Using the static function
Matrix<double> someMatrix = DenseMatrix.OfColumnMajor(4, 4, someArray);

If your data is in row-major order, you could split into arrays and use one of the OfRows function, or use the constructor and transpose the matrix, as suggested by Christoph.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58