Browse Source

added new constructor to sparse matrix that takes a matrix. Replaced new SparseMatrix(matrix.ToArray()) calls with it

la-knuth
Marcus Cuda 16 years ago
parent
commit
e9ab742b6e
  1. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs
  2. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs
  3. 36
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs
  5. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs
  6. 36
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  7. 2
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs
  8. 2
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs
  9. 35
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  10. 2
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs
  11. 2
      src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs
  12. 36
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

2
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs

@ -316,7 +316,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix.ToArray());
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix);
// The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A)

2
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs

@ -114,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_decompositionLU = new SparseMatrix(matrix.ToArray());
_decompositionLU = new SparseMatrix(matrix);
// M == A
// for i = 2, ... , n do

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

@ -188,6 +188,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class, copying
/// the values from the given matrix.
/// </summary>
/// <param name="matrix">The matrix to copy.</param>
public SparseMatrix(Matrix<Complex> matrix)
: base(matrix.RowCount, matrix.ColumnCount)
{
var sparseMatrix = matrix as SparseMatrix;
var rows = matrix.RowCount;
var columns = matrix.ColumnCount;
if (sparseMatrix == null)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
SetValueAt(i, j, matrix.At(i, j));
}
}
}
else
{
NonZerosCount = sparseMatrix.NonZerosCount;
_rowIndex = new int[rows];
_columnIndices = new int[NonZerosCount];
_nonZeroValues = new Complex[NonZerosCount];
Array.Copy(sparseMatrix._nonZeroValues, _nonZeroValues, NonZerosCount);
Array.Copy(sparseMatrix._columnIndices, _columnIndices, NonZerosCount);
Array.Copy(sparseMatrix._rowIndex, _rowIndex, rows);
}
}
/// <summary>
/// Creates a <c>SparseMatrix</c> for the given number of rows and columns.
/// </summary>

2
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs

@ -316,7 +316,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix.ToArray());
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix);
// The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A)

2
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs

@ -114,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_decompositionLU = new SparseMatrix(matrix.ToArray());
_decompositionLU = new SparseMatrix(matrix);
// M == A
// for i = 2, ... , n do

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

@ -183,6 +183,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class, copying
/// the values from the given matrix.
/// </summary>
/// <param name="matrix">The matrix to copy.</param>
public SparseMatrix(Matrix<Complex32> matrix)
: base(matrix.RowCount, matrix.ColumnCount)
{
var sparseMatrix = matrix as SparseMatrix;
var rows = matrix.RowCount;
var columns = matrix.ColumnCount;
if (sparseMatrix == null)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
SetValueAt(i, j, matrix.At(i, j));
}
}
}
else
{
NonZerosCount = sparseMatrix.NonZerosCount;
_rowIndex = new int[rows];
_columnIndices = new int[NonZerosCount];
_nonZeroValues = new Complex32[NonZerosCount];
Array.Copy(sparseMatrix._nonZeroValues, _nonZeroValues, NonZerosCount);
Array.Copy(sparseMatrix._columnIndices, _columnIndices, NonZerosCount);
Array.Copy(sparseMatrix._rowIndex, _rowIndex, rows);
}
}
/// <summary>
/// Creates a <c>SparseMatrix</c> for the given number of rows and columns.
/// </summary>

2
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs

@ -315,7 +315,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix.ToArray());
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix);
// The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A)

2
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs

@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_decompositionLU = new SparseMatrix(matrix.ToArray());
_decompositionLU = new SparseMatrix(matrix);
// M == A
// for i = 2, ... , n do

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

@ -182,6 +182,41 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class, copying
/// the values from the given matrix.
/// </summary>
/// <param name="matrix">The matrix to copy.</param>
public SparseMatrix(Matrix<double> matrix) : base(matrix.RowCount, matrix.ColumnCount)
{
var sparseMatrix = matrix as SparseMatrix;
var rows = matrix.RowCount;
var columns = matrix.ColumnCount;
if (sparseMatrix == null)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
SetValueAt(i, j, matrix.At(i, j));
}
}
}
else
{
NonZerosCount = sparseMatrix.NonZerosCount;
_rowIndex = new int[rows];
_columnIndices = new int[NonZerosCount];
_nonZeroValues = new double[NonZerosCount];
Buffer.BlockCopy(sparseMatrix._nonZeroValues, 0, _nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(sparseMatrix._columnIndices, 0, _columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(sparseMatrix._rowIndex, 0, _rowIndex, 0, rows * Constants.SizeOfInt);
}
}
/// <summary>
/// Creates a <c>SparseMatrix</c> for the given number of rows and columns.
/// </summary>

2
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs

@ -315,7 +315,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix.ToArray());
var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : new SparseMatrix(matrix);
// The creation of the preconditioner follows the following algorithm.
// spaceLeft = lfilNnz * nnz(A)

2
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs

@ -113,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_decompositionLU = new SparseMatrix(matrix.ToArray());
_decompositionLU = new SparseMatrix(matrix);
// M == A
// for i = 2, ... , n do

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

@ -182,6 +182,42 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class, copying
/// the values from the given matrix.
/// </summary>
/// <param name="matrix">The matrix to copy.</param>
public SparseMatrix(Matrix<float> matrix)
: base(matrix.RowCount, matrix.ColumnCount)
{
var sparseMatrix = matrix as SparseMatrix;
var rows = matrix.RowCount;
var columns = matrix.ColumnCount;
if (sparseMatrix == null)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
SetValueAt(i, j, matrix.At(i, j));
}
}
}
else
{
NonZerosCount = sparseMatrix.NonZerosCount;
_rowIndex = new int[rows];
_columnIndices = new int[NonZerosCount];
_nonZeroValues = new float[NonZerosCount];
Buffer.BlockCopy(sparseMatrix._nonZeroValues, 0, _nonZeroValues, 0, NonZerosCount * Constants.SizeOfFloat);
Buffer.BlockCopy(sparseMatrix._columnIndices, 0, _columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(sparseMatrix._rowIndex, 0, _rowIndex, 0, rows * Constants.SizeOfInt);
}
}
/// <summary>
/// Creates a <c>SparseMatrix</c> for the given number of rows and columns.
/// </summary>

Loading…
Cancel
Save