diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
index 09f065c0..6c01a206 100644
--- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
@@ -919,21 +919,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return target;
}
- ///
- /// Returns this matrix as a multidimensional array.
- ///
- /// A multidimensional containing the values of this matrix.
- public override Complex[,] ToArray()
- {
- var result = new Complex[RowCount, ColumnCount];
- for (var i = 0; i < _data.Length; i++)
- {
- result[i, i] = _data[i];
- }
-
- return result;
- }
-
///
/// Creates a new and inserts the given column at the given index.
///
diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
index a45540f5..8dd87de7 100644
--- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
@@ -483,31 +483,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
- ///
- /// Returns the matrix's elements as an array with the data laid out column-wise.
- ///
- ///
- /// 1, 2, 3
- /// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
- /// 7, 8, 9
- ///
- /// An array containing the matrix's elements.
- public override Complex[] ToColumnWiseArray()
- {
- var values = _storage.Values;
- var ret = new Complex[RowCount * ColumnCount];
- for (var j = 0; j < ColumnCount; j++)
- {
- for (var i = 0; i < RowCount; i++)
- {
- var index = _storage.FindItem(i, j);
- ret[(j * RowCount) + i] = index >= 0 ? values[index] : 0.0;
- }
- }
-
- return ret;
- }
-
///
/// Returns the transpose of this matrix.
///
diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
index 7e3feb58..ab0282ec 100644
--- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
@@ -919,21 +919,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return target;
}
- ///
- /// Returns this matrix as a multidimensional array.
- ///
- /// A multidimensional containing the values of this matrix.
- public override Complex32[,] ToArray()
- {
- var result = new Complex32[RowCount, ColumnCount];
- for (var i = 0; i < _data.Length; i++)
- {
- result[i, i] = _data[i];
- }
-
- return result;
- }
-
///
/// Creates a new and inserts the given column at the given index.
///
diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
index a4a16d07..ec3249a1 100644
--- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
@@ -483,31 +483,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
- ///
- /// Returns the matrix's elements as an array with the data laid out column-wise.
- ///
- ///
- /// 1, 2, 3
- /// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
- /// 7, 8, 9
- ///
- /// An array containing the matrix's elements.
- public override Complex32[] ToColumnWiseArray()
- {
- var values = _storage.Values;
- var ret = new Complex32[RowCount * ColumnCount];
- for (var j = 0; j < ColumnCount; j++)
- {
- for (var i = 0; i < RowCount; i++)
- {
- var index = _storage.FindItem(i, j);
- ret[(j * RowCount) + i] = index >= 0 ? values[index] : 0.0f;
- }
- }
-
- return ret;
- }
-
///
/// Returns the transpose of this matrix.
///
diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
index 66f406b3..5ccd41cd 100644
--- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
@@ -913,21 +913,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return target;
}
- ///
- /// Returns this matrix as a multidimensional array.
- ///
- /// A multidimensional containing the values of this matrix.
- public override double[,] ToArray()
- {
- var result = new double[RowCount, ColumnCount];
- for (var i = 0; i < _data.Length; i++)
- {
- result[i, i] = _data[i];
- }
-
- return result;
- }
-
///
/// Creates a new and inserts the given column at the given index.
///
diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
index 0c8a7806..adf012bf 100644
--- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
@@ -482,31 +482,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
- ///
- /// Returns the matrix's elements as an array with the data laid out column-wise.
- ///
- ///
- /// 1, 2, 3
- /// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
- /// 7, 8, 9
- ///
- /// An array containing the matrix's elements.
- public override double[] ToColumnWiseArray()
- {
- var values = _storage.Values;
- var ret = new double[RowCount * ColumnCount];
- for (var j = 0; j < ColumnCount; j++)
- {
- for (var i = 0; i < RowCount; i++)
- {
- var index = _storage.FindItem(i, j);
- ret[(j * RowCount) + i] = index >= 0 ? values[index] : 0.0;
- }
- }
-
- return ret;
- }
-
///
/// Returns the transpose of this matrix.
///
diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs
index a6e9db82..876f5332 100644
--- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs
+++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs
@@ -1160,20 +1160,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// Returns this matrix as a multidimensional array.
///
/// A multidimensional containing the values of this matrix.
- public virtual T[,] ToArray()
+ public T[,] ToArray()
{
- var ret = new T[RowCount, ColumnCount];
- CommonParallel.For(
- 0,
- ColumnCount,
- j =>
- {
- for (var i = 0; i < RowCount; i++)
- {
- ret[i, j] = At(i, j);
- }
- });
- return ret;
+ return Storage.ToArray();
}
///
@@ -1185,19 +1174,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// 7, 8, 9
///
/// An array containing the matrix's elements.
- public virtual T[] ToColumnWiseArray()
+ public T[] ToColumnWiseArray()
{
- var ret = new T[RowCount * ColumnCount];
- foreach (var column in ColumnEnumerator())
- {
- var columnIndex = column.Item1 * RowCount;
- foreach (var element in column.Item2.GetIndexedEnumerator())
- {
- ret[columnIndex + element.Item1] = element.Item2;
- }
- }
-
- return ret;
+ return Storage.ToColumnMajorArray();
}
///
@@ -1209,20 +1188,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// 7, 8, 9
///
/// An array containing the matrix's elements.
- public virtual T[] ToRowWiseArray()
+ public T[] ToRowWiseArray()
{
- var ret = new T[RowCount * ColumnCount];
-
- foreach (var row in RowEnumerator())
- {
- var rowIndex = row.Item1 * ColumnCount;
- foreach (var element in row.Item2.GetIndexedEnumerator())
- {
- ret[rowIndex + element.Item1] = element.Item2;
- }
- }
-
- return ret;
+ return Storage.ToRowMajorArray();
}
#region Implemented Interfaces
diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
index 8b529c93..972523e3 100644
--- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
@@ -913,21 +913,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return target;
}
- ///
- /// Returns this matrix as a multidimensional array.
- ///
- /// A multidimensional containing the values of this matrix.
- public override float[,] ToArray()
- {
- var result = new float[RowCount, ColumnCount];
- for (var i = 0; i < _data.Length; i++)
- {
- result[i, i] = _data[i];
- }
-
- return result;
- }
-
///
/// Creates a new and inserts the given column at the given index.
///
diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
index fe3a93a1..d3336a6e 100644
--- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
@@ -482,31 +482,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
- ///
- /// Returns the matrix's elements as an array with the data laid out column-wise.
- ///
- ///
- /// 1, 2, 3
- /// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
- /// 7, 8, 9
- ///
- /// An array containing the matrix's elements.
- public override float[] ToColumnWiseArray()
- {
- var values = _storage.Values;
- var ret = new float[RowCount * ColumnCount];
- for (var j = 0; j < ColumnCount; j++)
- {
- for (var i = 0; i < RowCount; i++)
- {
- var index = _storage.FindItem(i, j);
- ret[(j * RowCount) + i] = index >= 0 ? values[index] : 0.0f;
- }
- }
-
- return ret;
- }
-
///
/// Returns the transpose of this matrix.
///
diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
index fc1f4dce..fa940cd6 100644
--- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
+++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
@@ -169,5 +169,39 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{
Array.Copy(Data, columnIndex*RowCount + sourceRowIndex, target.Data, targetRowIndex, rowCount);
}
+
+ public override T[] ToRowMajorArray()
+ {
+ var ret = new T[Data.Length];
+ for (int i = 0; i < RowCount; i++)
+ {
+ var offset = i * ColumnCount;
+ for (int j = 0; j < ColumnCount; j++)
+ {
+ ret[offset + j] = Data[(j * RowCount) + i];
+ }
+ }
+ return ret;
+ }
+
+ public override T[] ToColumnMajorArray()
+ {
+ var ret = new T[Data.Length];
+ Array.Copy(Data, ret, Data.Length);
+ return ret;
+ }
+
+ public override T[,] ToArray()
+ {
+ var ret = new T[RowCount, ColumnCount];
+ for (int i = 0; i < RowCount; i++)
+ {
+ for (int j = 0; j < ColumnCount; j++)
+ {
+ ret[i, j] = Data[(j * RowCount) + i];
+ }
+ }
+ return ret;
+ }
}
}
diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
index 0d2132e0..a08775a3 100644
--- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
+++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
@@ -385,5 +385,37 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
target.At(columnIndex - sourceRowIndex + targetRowIndex, Data[columnIndex]);
}
}
+
+ public override T[] ToRowMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ var stride = ColumnCount + 1;
+ for (int i = 0; i < Data.Length; i++)
+ {
+ ret[i * stride] = Data[i];
+ }
+ return ret;
+ }
+
+ public override T[] ToColumnMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ var stride = RowCount + 1;
+ for (int i = 0; i < Data.Length; i++)
+ {
+ ret[i * stride] = Data[i];
+ }
+ return ret;
+ }
+
+ public override T[,] ToArray()
+ {
+ var ret = new T[RowCount, ColumnCount];
+ for (int i = 0; i < Data.Length; i++)
+ {
+ ret[i, i] = Data[i];
+ }
+ return ret;
+ }
}
}
diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
index 138df142..1237fcd6 100644
--- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
+++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
@@ -311,5 +311,46 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
target.At(ii, At(i, columnIndex));
}
}
+
+ public virtual T[] ToRowMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ for (int i = 0; i < RowCount; i++)
+ {
+ var offset = i * ColumnCount;
+ for (int j = 0; j < ColumnCount; j++)
+ {
+ ret[offset + j] = At(i, j);
+ }
+ }
+ return ret;
+ }
+
+ public virtual T[] ToColumnMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ for (int j = 0; j < ColumnCount; j++)
+ {
+ var offset = j * RowCount;
+ for (int i = 0; i < RowCount; i++)
+ {
+ ret[offset + i] = At(i, j);
+ }
+ }
+ return ret;
+ }
+
+ public virtual T[,] ToArray()
+ {
+ var ret = new T[RowCount,ColumnCount];
+ for (int i = 0; i < RowCount; i++)
+ {
+ for (int j = 0; j < ColumnCount; j++)
+ {
+ ret[i, j] = At(i, j);
+ }
+ }
+ return ret;
+ }
}
}
diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
index fce72d20..aa6627b7 100644
--- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
+++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
@@ -550,5 +550,60 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
target.At(j, index >= 0 ? Values[index] : _zero);
}
}
+
+ public override T[] ToRowMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ if (ValueCount != 0)
+ {
+ for (int row = 0; row < RowCount; row++)
+ {
+ var offset = row * ColumnCount;
+ var startIndex = RowPointers[row];
+ var endIndex = row < RowPointers.Length - 1 ? RowPointers[row + 1] : ValueCount;
+ for (var j = startIndex; j < endIndex; j++)
+ {
+ ret[offset + ColumnIndices[j]] = Values[j];
+ }
+ }
+ }
+ return ret;
+ }
+
+ public override T[] ToColumnMajorArray()
+ {
+ var ret = new T[RowCount * ColumnCount];
+ if (ValueCount != 0)
+ {
+ for (int row = 0; row < RowCount; row++)
+ {
+ var startIndex = RowPointers[row];
+ var endIndex = row < RowPointers.Length - 1 ? RowPointers[row + 1] : ValueCount;
+ for (var j = startIndex; j < endIndex; j++)
+ {
+ ret[(ColumnIndices[j]) * RowCount + row] = Values[j];
+ }
+ }
+ }
+ return ret;
+ }
+
+ public override T[,] ToArray()
+ {
+ var ret = new T[RowCount, ColumnCount];
+ if (ValueCount != 0)
+ {
+ for (int row = 0; row < RowCount; row++)
+ {
+ var startIndex = RowPointers[row];
+ var endIndex = row < RowPointers.Length - 1 ? RowPointers[row + 1] : ValueCount;
+ for (var j = startIndex; j < endIndex; j++)
+ {
+ ret[row, ColumnIndices[j]] = Values[j];
+ }
+ }
+ }
+ return ret;
+ }
}
}