diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 3db59612..3bd3c760 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -121,9 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public static DenseMatrix OfMatrix(Matrix matrix) { - var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new DenseMatrix(storage); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 37b037b1..3517c3c3 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -102,9 +102,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) - : this(other.Count) + : this(DenseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index fbae4c03..87221505 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -119,6 +119,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Create a new diagonal matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// The matrix to copy from must be diagonal as well. + /// A new memory block will be allocated for storing the matrix. + /// + public static DiagonalMatrix OfMatrix(Matrix matrix) + { + return new DiagonalMatrix(DiagonalMatrixStorage.OfMatrix(matrix.Storage)); + } + /// /// Create a new diagonal matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. @@ -150,6 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The matrix to copy from must be diagonal as well. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DiagonalMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 7b970775..8aaa73ea 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -99,9 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public static SparseMatrix OfMatrix(Matrix matrix) { - var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 322f6b49..11d8dc17 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -113,9 +113,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// A new memory block will be allocated for storing the vector. /// public SparseVector(Vector other) - : this(new SparseVectorStorage(other.Count)) + : this(SparseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index af183991..05eff4a3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -121,9 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public static DenseMatrix OfMatrix(Matrix matrix) { - var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new DenseMatrix(storage); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 02858aa4..7b987363 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -102,9 +102,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) - : this(other.Count) + : this(DenseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 1d9827f9..2cf2aee5 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -119,6 +119,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Create a new diagonal matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// The matrix to copy from must be diagonal as well. + /// A new memory block will be allocated for storing the matrix. + /// + public static DiagonalMatrix OfMatrix(Matrix matrix) + { + return new DiagonalMatrix(DiagonalMatrixStorage.OfMatrix(matrix.Storage)); + } + /// /// Create a new diagonal matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. @@ -150,6 +161,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The matrix to copy from must be diagonal as well. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DiagonalMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 27b833bb..f61c12b1 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -99,9 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public static SparseMatrix OfMatrix(Matrix matrix) { - var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index c82be4cb..364d2c8b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -113,9 +113,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// A new memory block will be allocated for storing the vector. /// public SparseVector(Vector other) - : this(new SparseVectorStorage(other.Count)) + : this(SparseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 78d9b9d9..595c0863 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -121,9 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public static DenseMatrix OfMatrix(Matrix matrix) { - var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new DenseMatrix(storage); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 6f797b88..d1da3c27 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -103,9 +103,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) - : this(other.Count) + : this(DenseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index fd8150f1..7bcbd0a3 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -118,6 +118,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Create a new diagonal matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// The matrix to copy from must be diagonal as well. + /// A new memory block will be allocated for storing the matrix. + /// + public static DiagonalMatrix OfMatrix(Matrix matrix) + { + return new DiagonalMatrix(DiagonalMatrixStorage.OfMatrix(matrix.Storage)); + } + /// /// Create a new diagonal matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. @@ -149,6 +160,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The matrix to copy from must be diagonal as well. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DiagonalMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 66992346..79e2f79a 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -98,9 +98,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public static SparseMatrix OfMatrix(Matrix matrix) { - var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 08be4091..87d21ecd 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -113,9 +113,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A new memory block will be allocated for storing the vector. /// public SparseVector(Vector other) - : this(new SparseVectorStorage(other.Count)) + : this(SparseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index caaaaf1b..c124af13 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -121,9 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public static DenseMatrix OfMatrix(Matrix matrix) { - var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new DenseMatrix(storage); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index a0e4fd1e..9c1c3a26 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -102,9 +102,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) - : this(other.Count) + : this(DenseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 45ff9cd8..454447f2 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -118,6 +118,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Create a new diagonal matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// The matrix to copy from must be diagonal as well. + /// A new memory block will be allocated for storing the matrix. + /// + public static DiagonalMatrix OfMatrix(Matrix matrix) + { + return new DiagonalMatrix(DiagonalMatrixStorage.OfMatrix(matrix.Storage)); + } + /// /// Create a new diagonal matrix as a copy of the given two-dimensional array. /// This new matrix will be independent from the provided array. @@ -149,6 +160,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The matrix to copy from must be diagonal as well. /// A new memory block will be allocated for storing the matrix. /// + [Obsolete("Use DiagonalMatrix.OfMatrix instead. Scheduled for removal in v3.0.")] public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 7729c0c7..8fa78ebe 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -98,9 +98,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public static SparseMatrix OfMatrix(Matrix matrix) { - var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); - matrix.Storage.CopyToUnchecked(storage, skipClearing: true); - return new SparseMatrix(storage); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 433b7f24..c59b9953 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -113,9 +113,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// A new memory block will be allocated for storing the vector. /// public SparseVector(Vector other) - : this(new SparseVectorStorage(other.Count)) + : this(SparseVectorStorage.OfVector(other.Storage)) { - other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index e81263e0..6426e3a2 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -101,6 +101,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION + public static DenseColumnMajorMatrixStorage OfMatrix(MatrixStorage matrix) + { + var storage = new DenseColumnMajorMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.CopyToUnchecked(storage, skipClearing: true); + return storage; + } + public static DenseColumnMajorMatrixStorage OfColumnMajorEnumerable(int rows, int columns, IEnumerable data) { if (data == null) diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index 0feb5e58..bda26619 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -92,6 +92,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION + public static DenseVectorStorage OfVector(VectorStorage vector) + { + var storage = new DenseVectorStorage(vector.Length); + vector.CopyToUnchecked(storage, skipClearing: true); + return storage; + } + public static DenseVectorStorage OfEnumerable(IEnumerable data) { if (data == null) diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs index 1d7eb71d..a2a97995 100644 --- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs @@ -170,6 +170,17 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } + // INITIALIZATION + + public static DiagonalMatrixStorage OfMatrix(MatrixStorage matrix) + { + var storage = new DiagonalMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.CopyToUnchecked(storage, skipClearing: true); + return storage; + } + + // MATRIX COPY + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var diagonalTarget = target as DiagonalMatrixStorage; diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index e59b6eff..42d5b3af 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -363,6 +363,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION + public static SparseCompressedRowMatrixStorage OfMatrix(MatrixStorage matrix) + { + var storage = new SparseCompressedRowMatrixStorage(matrix.RowCount, matrix.ColumnCount); + matrix.CopyToUnchecked(storage, skipClearing: true); + return storage; + } + public static SparseCompressedRowMatrixStorage OfRowMajorEnumerable(int rows, int columns, IEnumerable data) { if (data == null) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 92863756..bc60705b 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -284,6 +284,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage // INITIALIZATION + public static SparseVectorStorage OfVector(VectorStorage vector) + { + var storage = new SparseVectorStorage(vector.Length); + vector.CopyToUnchecked(storage, skipClearing: true); + return storage; + } + public static SparseVectorStorage OfEnumerable(IEnumerable data) { if (data == null)