Browse Source

LA: Initialize sparse matrix by enumerable #104

pull/112/head
Christoph Ruegg 14 years ago
parent
commit
d87f23e046
  1. 4
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 11
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  3. 4
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  4. 11
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  5. 4
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  6. 11
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  7. 4
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  8. 11
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  9. 37
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

4
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex> other)
: this(DenseColumnMajorMatrixStorage<Complex>.FromColumnMajorEnumerable(rows, columns, other))
public DenseMatrix(int rows, int columns, IEnumerable<Complex> columnMajor)
: this(DenseColumnMajorMatrixStorage<Complex>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}

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

@ -172,6 +172,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<Complex> rowMajor)
: this(SparseCompressedRowMatrixStorage<Complex>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary>
/// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix.

4
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex32> other)
: this(DenseColumnMajorMatrixStorage<Complex32>.FromColumnMajorEnumerable(rows, columns, other))
public DenseMatrix(int rows, int columns, IEnumerable<Complex32> columnMajor)
: this(DenseColumnMajorMatrixStorage<Complex32>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}

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

@ -172,6 +172,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<Complex32> rowMajor)
: this(SparseCompressedRowMatrixStorage<Complex32>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary>
/// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix.

4
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<double> other)
: this(DenseColumnMajorMatrixStorage<double>.FromColumnMajorEnumerable(rows, columns, other))
public DenseMatrix(int rows, int columns, IEnumerable<double> columnMajor)
: this(DenseColumnMajorMatrixStorage<double>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}

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

@ -171,6 +171,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<double> rowMajor)
: this(SparseCompressedRowMatrixStorage<double>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary>
/// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix.

4
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -152,8 +152,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<float> other)
: this(DenseColumnMajorMatrixStorage<float>.FromColumnMajorEnumerable(rows, columns, other))
public DenseMatrix(int rows, int columns, IEnumerable<float> columnMajor)
: this(DenseColumnMajorMatrixStorage<float>.FromColumnMajorEnumerable(rows, columns, columnMajor))
{
}

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

@ -171,6 +171,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public SparseMatrix(int rows, int columns, IEnumerable<float> rowMajor)
: this(SparseCompressedRowMatrixStorage<float>.FromRowMajorEnumerable(rows, columns, rowMajor))
{
}
/// <summary>
/// Create a new sparse matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix.

37
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -361,6 +361,43 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return hash;
}
// INITIALIZATION
public static SparseCompressedRowMatrixStorage<T> FromRowMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns);
var rowPointers = storage.RowPointers;
var columnIndices = new List<int>();
var values = new List<T>();
var iterator = data.GetEnumerator();
for (int row = 0; row < rows; row++)
{
rowPointers[row] = values.Count;
for (int col = 0; col < columns; col++)
{
iterator.MoveNext();
if (!Zero.Equals(iterator.Current))
{
values.Add(iterator.Current);
columnIndices.Add(col);
}
}
}
storage.ColumnIndices = columnIndices.ToArray();
storage.Values = values.ToArray();
storage.ValueCount = values.Count;
return storage;
}
// MATRIX COPY
internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)
{
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;

Loading…
Cancel
Save