Browse Source

Delegate Matrix To(Row/Column)Array to storage classes

v2
Christoph Ruegg 14 years ago
parent
commit
d1d20b01c1
  1. 15
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 25
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  3. 15
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  4. 25
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  5. 15
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  6. 25
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  7. 44
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  8. 15
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  9. 25
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  10. 34
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  11. 32
      src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
  12. 41
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  13. 55
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

15
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -919,21 +919,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return target;
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
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;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>

25
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -483,31 +483,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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;
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

15
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -919,21 +919,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return target;
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
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;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>

25
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -483,31 +483,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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;
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

15
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -913,21 +913,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return target;
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
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;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>

25
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -482,31 +482,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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;
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

44
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -1160,20 +1160,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
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();
}
/// <summary>
@ -1185,19 +1174,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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();
}
/// <summary>
@ -1209,20 +1188,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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

15
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -913,21 +913,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return target;
}
/// <summary>
/// Returns this matrix as a multidimensional array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
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;
}
/// <summary>
/// Creates a new <see cref="SparseMatrix"/> and inserts the given column at the given index.
/// </summary>

25
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -482,31 +482,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Returns the matrix's elements as an array with the data laid out column-wise.
/// </summary>
/// <example><pre>
/// 1, 2, 3
/// 4, 5, 6 will be returned as 1, 4, 7, 2, 5, 8, 3, 6, 9
/// 7, 8, 9
/// </pre></example>
/// <returns>An array containing the matrix's elements.</returns>
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;
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

34
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;
}
}
}

32
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;
}
}
}

41
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;
}
}
}

55
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;
}
}
}

Loading…
Cancel
Save