From 44228a6d41f078b97452a24781e23cd3736f086b Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 1 Dec 2013 17:38:04 +0100 Subject: [PATCH] LA: Build.SameType of one or two example instances --- src/Numerics/LinearAlgebra/Builder.cs | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index e668e7e3..e167164e 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -396,6 +396,31 @@ namespace MathNet.Numerics.LinearAlgebra throw new NotSupportedException(); } + /// + /// Create a new matrix with the same kind of the provided example. + /// + public Matrix SameType(Matrix example, int rows, int columns) + { + var storage = example.Storage; + if (storage is DenseColumnMajorMatrixStorage) return Dense(rows, columns); + if (storage is DiagonalMatrixStorage) return Diagonal(rows, columns); + if (storage is SparseCompressedRowMatrixStorage) return Sparse(rows, columns); + return Dense(rows, columns); + } + + /// + /// Create a new matrix with a type that can represent and is closest to both provided samples. + /// + public Matrix SameType(Matrix example1, Matrix example2 , int rows, int columns) + { + var storage1 = example1.Storage; + var storage2 = example2.Storage; + if (storage1 is DenseColumnMajorMatrixStorage || storage2 is DenseColumnMajorMatrixStorage) return Dense(rows, columns); + if (storage1 is DiagonalMatrixStorage && storage2 is DiagonalMatrixStorage) return Diagonal(rows, columns); + if (storage1 is SparseCompressedRowMatrixStorage || storage2 is SparseCompressedRowMatrixStorage) return Sparse(rows, columns); + return Dense(rows, columns); + } + /// /// Create a new dense matrix with values sampled from the provided random distribution. /// @@ -1190,6 +1215,22 @@ namespace MathNet.Numerics.LinearAlgebra throw new NotSupportedException(); } + /// + /// Create a new vector with the same kind of the provided example. + /// + public Vector SameType(Vector example, int length) + { + return example.Storage.IsDense ? Dense(length) : Sparse(length); + } + + /// + /// Create a new vector with a type that can represent and is closest to both provided samples. + /// + public Vector SameType(Vector example1, Vector example2, int length) + { + return example1.Storage.IsDense || example2.Storage.IsDense ? Dense(length) : Sparse(length); + } + /// /// Create a new dense vector with values sampled from the provided random distribution. ///