diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs
index a2bd3141..e13fd38b 100644
--- a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/Ilutp.cs
+++ b/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)
diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs
index 25d69d09..ce41969b 100644
--- a/src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/IncompleteLU.cs
+++ b/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
diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
index 5ba10240..ddcd222e 100644
--- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
@@ -188,6 +188,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
+ ///
+ /// Initializes a new instance of the class, copying
+ /// the values from the given matrix.
+ ///
+ /// The matrix to copy.
+ public SparseMatrix(Matrix 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);
+ }
+ }
+
///
/// Creates a SparseMatrix for the given number of rows and columns.
///
diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs
index f7769a3e..18cc82f7 100644
--- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/Ilutp.cs
+++ b/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)
diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs
index 1bfc4848..016e65ff 100644
--- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/IncompleteLU.cs
+++ b/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
diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
index 7df052f3..584a13e4 100644
--- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
@@ -183,6 +183,42 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
+ ///
+ /// Initializes a new instance of the class, copying
+ /// the values from the given matrix.
+ ///
+ /// The matrix to copy.
+ public SparseMatrix(Matrix 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);
+ }
+ }
+
///
/// Creates a SparseMatrix for the given number of rows and columns.
///
diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs
index 6a2d4032..709b5987 100644
--- a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/Ilutp.cs
+++ b/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)
diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs
index a2e042ac..4b3862df 100644
--- a/src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/IncompleteLU.cs
+++ b/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
diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
index a1ac50bd..0aa76e6d 100644
--- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
@@ -182,6 +182,41 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
+ ///
+ /// Initializes a new instance of the class, copying
+ /// the values from the given matrix.
+ ///
+ /// The matrix to copy.
+ public SparseMatrix(Matrix 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);
+ }
+ }
+
///
/// Creates a SparseMatrix for the given number of rows and columns.
///
diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs
index d86f48d5..2042270e 100644
--- a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/Ilutp.cs
+++ b/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)
diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs
index 627c1f6c..22cec0da 100644
--- a/src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/IncompleteLU.cs
+++ b/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
diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
index 2f2f4eb2..51690d84 100644
--- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
+++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
@@ -182,6 +182,42 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
+ ///
+ /// Initializes a new instance of the class, copying
+ /// the values from the given matrix.
+ ///
+ /// The matrix to copy.
+ public SparseMatrix(Matrix 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);
+ }
+ }
+
///
/// Creates a SparseMatrix for the given number of rows and columns.
///