From b522c1f6da14d3cc49acee0dd70dba706d69e2ab Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 5 Sep 2013 21:20:13 +0200 Subject: [PATCH] LA: member methods instead of extension methods for matrix factorizations --- src/Numerics/Distributions/InverseWishart.cs | 4 +- src/Numerics/Distributions/MatrixNormal.cs | 6 +- src/Numerics/Distributions/Wishart.cs | 4 +- .../LinearAlgebra/Complex/DenseMatrix.cs | 32 ++++++ .../LinearAlgebra/Complex/ExtensionMethods.cs | 106 ----------------- src/Numerics/LinearAlgebra/Complex/Matrix.cs | 32 ++++++ .../LinearAlgebra/Complex/Solvers/Iterator.cs | 3 + .../LinearAlgebra/Complex32/DenseMatrix.cs | 32 ++++++ .../Complex32/ExtensionMethods.cs | 101 ----------------- .../LinearAlgebra/Complex32/Matrix.cs | 32 ++++++ .../Complex32/Solvers/Iterator.cs | 3 + .../LinearAlgebra/Double/DenseMatrix.cs | 32 ++++++ .../LinearAlgebra/Double/ExtensionMethods.cs | 99 ---------------- src/Numerics/LinearAlgebra/Double/Matrix.cs | 32 ++++++ .../LinearAlgebra/Double/Solvers/Iterator.cs | 3 + .../LinearAlgebra/Factorization/Cholesky.cs | 76 +------------ .../LinearAlgebra/Factorization/Evd.cs | 93 ++------------- .../Factorization/GramSchmidt.cs | 59 ---------- .../LinearAlgebra/Factorization/LU.cs | 77 +------------ .../LinearAlgebra/Factorization/QR.cs | 90 +-------------- .../LinearAlgebra/Factorization/Svd.cs | 107 ++---------------- .../LinearAlgebra/Matrix.Arithmetic.cs | 8 +- ...sionMethods.cs => Matrix.Factorization.cs} | 51 +++------ src/Numerics/LinearAlgebra/Matrix.cs | 2 +- .../LinearAlgebra/Single/DenseMatrix.cs | 32 ++++++ src/Numerics/LinearAlgebra/Single/Matrix.cs | 32 ++++++ .../LinearAlgebra/Single/Solvers/Iterator.cs | 3 + src/Numerics/Numerics.csproj | 5 +- 28 files changed, 323 insertions(+), 833 deletions(-) delete mode 100644 src/Numerics/LinearAlgebra/Complex/ExtensionMethods.cs delete mode 100644 src/Numerics/LinearAlgebra/Complex32/ExtensionMethods.cs delete mode 100644 src/Numerics/LinearAlgebra/Double/ExtensionMethods.cs rename src/Numerics/LinearAlgebra/{Single/ExtensionMethods.cs => Matrix.Factorization.cs} (61%) diff --git a/src/Numerics/Distributions/InverseWishart.cs b/src/Numerics/Distributions/InverseWishart.cs index eb98786f..54c0959f 100644 --- a/src/Numerics/Distributions/InverseWishart.cs +++ b/src/Numerics/Distributions/InverseWishart.cs @@ -129,7 +129,7 @@ namespace MathNet.Numerics.Distributions _freedom = degreesOfFreedom; _scale = scale; - _chol = Cholesky.Create(_scale); + _chol = _scale.Cholesky(); } /// @@ -217,7 +217,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentOutOfRangeException("x", Resources.ArgumentMatrixDimensions); } - var chol = Cholesky.Create(x); + var chol = x.Cholesky(); var dX = chol.Determinant; var sXi = chol.Solve(Scale); diff --git a/src/Numerics/Distributions/MatrixNormal.cs b/src/Numerics/Distributions/MatrixNormal.cs index 5391f6a2..3afd30ff 100644 --- a/src/Numerics/Distributions/MatrixNormal.cs +++ b/src/Numerics/Distributions/MatrixNormal.cs @@ -216,8 +216,8 @@ namespace MathNet.Numerics.Distributions } var a = x - _m; - var cholV = Cholesky.Create(_v); - var cholK = Cholesky.Create(_k); + var cholV = _v.Cholesky(); + var cholK = _k.Cholesky(); return Math.Exp(-0.5*cholV.Solve(a.Transpose()*cholK.Solve(a)).Trace()) /Math.Pow(2.0*Constants.Pi, x.RowCount*x.ColumnCount/2.0) @@ -281,7 +281,7 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from defined distribution. static Vector SampleVectorNormal(System.Random rnd, Vector mean, Matrix covariance) { - var chol = Cholesky.Create(covariance); + var chol = covariance.Cholesky(); // Sample a standard normal variable. var v = DenseVector.CreateRandom(mean.Count, new Normal(rnd)); diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs index e9d9871f..719e13be 100644 --- a/src/Numerics/Distributions/Wishart.cs +++ b/src/Numerics/Distributions/Wishart.cs @@ -105,7 +105,7 @@ namespace MathNet.Numerics.Distributions _degreesOfFreedom = degreesOfFreedom; _scale = scale; - _chol = Cholesky.Create(_scale); + _chol = _scale.Cholesky(); } /// @@ -281,7 +281,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); } - return DoSample(rnd, degreesOfFreedom, scale, Cholesky.Create(scale)); + return DoSample(rnd, degreesOfFreedom, scale, scale.Cholesky()); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index a138e3a9..26314d10 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -33,6 +33,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Complex.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; using MathNet.Numerics.Providers.LinearAlgebra; @@ -1006,5 +1008,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return (DenseMatrix)leftSide.Modulus(rightSide); } + + public override Cholesky Cholesky() + { + return new DenseCholesky(this); + } + + public override LU LU() + { + return new DenseLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new DenseQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new DenseGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new DenseSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new DenseEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex/ExtensionMethods.cs b/src/Numerics/LinearAlgebra/Complex/ExtensionMethods.cs deleted file mode 100644 index 22323f83..00000000 --- a/src/Numerics/LinearAlgebra/Complex/ExtensionMethods.cs +++ /dev/null @@ -1,106 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Complex.Factorization; -using MathNet.Numerics.LinearAlgebra.Factorization; - -namespace MathNet.Numerics.LinearAlgebra.Complex -{ - -#if NOSYSNUMERICS - using Numerics; -#else - using System.Numerics; -#endif - - /// - /// Extension methods which return factorizations for the various matrix classes. - /// - public static class ExtensionMethods - { - /// - /// Computes the Cholesky decomposition for a matrix. - /// - /// The matrix to factor. - /// The Cholesky decomposition object. - public static Cholesky Cholesky(this Matrix matrix) - { - return (Cholesky)Cholesky.Create(matrix); - } - - /// - /// Computes the LU decomposition for a matrix. - /// - /// The matrix to factor. - /// The LU decomposition object. - public static LU LU(this Matrix matrix) - { - return (LU)LU.Create(matrix); - } - - /// - /// Computes the QR decomposition for a matrix. - /// - /// The matrix to factor. - /// The type of QR factorization to perform. - /// The QR decomposition object. - public static QR QR(this Matrix matrix, QRMethod method = QRMethod.Full) - { - return (QR)QR.Create(matrix, method); - } - - /// - /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. - /// - /// The matrix to factor. - /// The QR decomposition object. - public static GramSchmidt GramSchmidt(this Matrix matrix) - { - return (GramSchmidt)GramSchmidt.Create(matrix); - } - - /// - /// Computes the SVD decomposition for a matrix. - /// - /// The matrix to factor. - /// Compute the singular U and VT vectors or not. - /// The SVD decomposition object. - public static Svd Svd(this Matrix matrix, bool computeVectors) - { - return (Svd)Svd.Create(matrix, computeVectors); - } - - /// - /// Computes the EVD decomposition for a matrix. - /// - /// The matrix to factor. - /// The EVD decomposition object. - public static Evd Evd(this Matrix matrix) - { - return (Evd)Evd.Create(matrix); - } - } -} diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index 6a7df912..5a6231b6 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -29,6 +29,8 @@ // using System; +using MathNet.Numerics.LinearAlgebra.Complex.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; @@ -455,5 +457,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return sum; } + + public override Cholesky Cholesky() + { + return new UserCholesky(this); + } + + public override LU LU() + { + return new UserLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new UserQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new UserGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new UserSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new UserEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs index fb4f5fc8..3468ecf0 100644 --- a/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex/Solvers/Iterator.cs @@ -46,6 +46,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers /// public static class Iterator { + + // TODO: Refactor + /// /// Creates a default iterator with all the objects. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index f68780b8..d2fd2239 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -33,6 +33,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Complex32.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; using MathNet.Numerics.Providers.LinearAlgebra; @@ -1001,5 +1003,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return (DenseMatrix)leftSide.Modulus(rightSide); } + + public override Cholesky Cholesky() + { + return new DenseCholesky(this); + } + + public override LU LU() + { + return new DenseLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new DenseQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new DenseGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new DenseSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new DenseEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/ExtensionMethods.cs b/src/Numerics/LinearAlgebra/Complex32/ExtensionMethods.cs deleted file mode 100644 index cd5fced5..00000000 --- a/src/Numerics/LinearAlgebra/Complex32/ExtensionMethods.cs +++ /dev/null @@ -1,101 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Complex32.Factorization; -using MathNet.Numerics.LinearAlgebra.Factorization; - -namespace MathNet.Numerics.LinearAlgebra.Complex32 -{ - using Numerics; - - /// - /// Extension methods which return factorizations for the various matrix classes. - /// - public static class ExtensionMethods - { - /// - /// Computes the Cholesky decomposition for a matrix. - /// - /// The matrix to factor. - /// The Cholesky decomposition object. - public static Cholesky Cholesky(this Matrix matrix) - { - return (Cholesky)Cholesky.Create(matrix); - } - - /// - /// Computes the LU decomposition for a matrix. - /// - /// The matrix to factor. - /// The LU decomposition object. - public static LU LU(this Matrix matrix) - { - return (LU)LU.Create(matrix); - } - - /// - /// Computes the QR decomposition for a matrix. - /// - /// The matrix to factor. - /// The type of QR factorization to perform. - /// The QR decomposition object. - public static QR QR(this Matrix matrix, QRMethod method = QRMethod.Full) - { - return (QR)QR.Create(matrix, method); - } - - /// - /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. - /// - /// The matrix to factor. - /// The QR decomposition object. - public static GramSchmidt GramSchmidt(this Matrix matrix) - { - return (GramSchmidt)GramSchmidt.Create(matrix); - } - - /// - /// Computes the SVD decomposition for a matrix. - /// - /// The matrix to factor. - /// Compute the singular U and VT vectors or not. - /// The SVD decomposition object. - public static Svd Svd(this Matrix matrix, bool computeVectors) - { - return (Svd)Svd.Create(matrix, computeVectors); - } - - /// - /// Computes the EVD decomposition for a matrix. - /// - /// The matrix to factor. - /// The EVD decomposition object. - public static Evd Evd(this Matrix matrix) - { - return (Evd)Evd.Create(matrix); - } - } -} diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index a8d734a4..9bf95c5c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -28,6 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.LinearAlgebra.Complex32.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; using System; @@ -450,5 +452,35 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return sum; } + + public override Cholesky Cholesky() + { + return new UserCholesky(this); + } + + public override LU LU() + { + return new UserLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new UserQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new UserGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new UserSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new UserEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs index 40a4037e..7161e3f0 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Solvers/Iterator.cs @@ -41,6 +41,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers /// public static class Iterator { + + // TODO: Refactor + /// /// Creates a default iterator with all the objects. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 3e2d8573..a874d9bc 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -33,6 +33,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Double.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; using MathNet.Numerics.Providers.LinearAlgebra; @@ -1032,5 +1034,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double return (DenseMatrix)leftSide.Modulus(rightSide); } + + public override Cholesky Cholesky() + { + return new DenseCholesky(this); + } + + public override LU LU() + { + return new DenseLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new DenseQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new DenseGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new DenseSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new DenseEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/ExtensionMethods.cs b/src/Numerics/LinearAlgebra/Double/ExtensionMethods.cs deleted file mode 100644 index 6a1cbd7c..00000000 --- a/src/Numerics/LinearAlgebra/Double/ExtensionMethods.cs +++ /dev/null @@ -1,99 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using MathNet.Numerics.LinearAlgebra.Double.Factorization; -using MathNet.Numerics.LinearAlgebra.Factorization; - -namespace MathNet.Numerics.LinearAlgebra.Double -{ - /// - /// Extension methods which return factorizations for the various matrix classes. - /// - public static class ExtensionMethods - { - /// - /// Computes the Cholesky decomposition for a matrix. - /// - /// The matrix to factor. - /// The Cholesky decomposition object. - public static Cholesky Cholesky(this Matrix matrix) - { - return (Cholesky)Cholesky.Create(matrix); - } - - /// - /// Computes the LU decomposition for a matrix. - /// - /// The matrix to factor. - /// The LU decomposition object. - public static LU LU(this Matrix matrix) - { - return (LU)LU.Create(matrix); - } - - /// - /// Computes the QR decomposition for a matrix. - /// - /// The matrix to factor. - /// The type of QR factorization to perform. - /// The QR decomposition object. - public static QR QR(this Matrix matrix, QRMethod method = QRMethod.Full) - { - return (QR)QR.Create(matrix, method); - } - - /// - /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. - /// - /// The matrix to factor. - /// The QR decomposition object. - public static GramSchmidt GramSchmidt(this Matrix matrix) - { - return (GramSchmidt)GramSchmidt.Create(matrix); - } - - /// - /// Computes the SVD decomposition for a matrix. - /// - /// The matrix to factor. - /// Compute the singular U and VT vectors or not. - /// The SVD decomposition object. - public static Svd Svd(this Matrix matrix, bool computeVectors) - { - return (Svd)Svd.Create(matrix, computeVectors); - } - - /// - /// Computes the EVD decomposition for a matrix. - /// - /// The matrix to factor. - /// The EVD decomposition object. - public static Evd Evd(this Matrix matrix) - { - return (Evd)Evd.Create(matrix); - } - } -} diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 6c6b1232..51d0e295 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -29,6 +29,8 @@ // using System; +using MathNet.Numerics.LinearAlgebra.Double.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; @@ -456,5 +458,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double return sum; } + + public override Cholesky Cholesky() + { + return new UserCholesky(this); + } + + public override LU LU() + { + return new UserLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new UserQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new UserGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new UserSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new UserEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs index b7cbe980..61688c99 100644 --- a/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Double/Solvers/Iterator.cs @@ -39,6 +39,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers /// public static class Iterator { + + // TODO: Refactor + /// /// Creates a default iterator with all the objects. /// diff --git a/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs b/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs index 7d3a5906..70022292 100644 --- a/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs +++ b/src/Numerics/LinearAlgebra/Factorization/Cholesky.cs @@ -32,12 +32,6 @@ using System; namespace MathNet.Numerics.LinearAlgebra.Factorization { - using Numerics; - -#if !NOSYSNUMERICS - using System.Numerics; -#endif - /// /// A class which encapsulates the functionality of a Cholesky factorization. /// For a symmetric, positive definite matrix A, the Cholesky factorization @@ -51,68 +45,10 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization public abstract class Cholesky : ISolver where T : struct, IEquatable, IFormattable { - /// - /// Internal method which routes the call to perform the Cholesky factorization to the appropriate class. - /// - /// The matrix to factor. - /// A cholesky factorization object. - internal static Cholesky Create(Matrix matrix) - { - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseCholesky(dense) as Cholesky; - } - - return new Double.Factorization.UserCholesky(matrix as Matrix) as Cholesky; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseCholesky(dense) as Cholesky; - } - - return new Single.Factorization.UserCholesky(matrix as Matrix) as Cholesky; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseCholesky(dense) as Cholesky; - } - - return new LinearAlgebra.Complex.Factorization.UserCholesky(matrix as Matrix) as Cholesky; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseCholesky(dense) as Cholesky; - } - - return new LinearAlgebra.Complex32.Factorization.UserCholesky(matrix as Matrix) as Cholesky; - } - - throw new NotSupportedException(); - } - /// /// Gets or sets the lower triangular form of the Cholesky matrix /// - protected Matrix CholeskyFactor - { - get; - set; - } + protected Matrix CholeskyFactor { get; set; } /// /// Gets the lower triangular form of the Cholesky matrix. @@ -128,18 +64,12 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets the determinant of the matrix for which the Cholesky matrix was computed. /// - public abstract T Determinant - { - get; - } + public abstract T Determinant { get; } /// /// Gets the log determinant of the matrix for which the Cholesky matrix was computed. /// - public abstract T DeterminantLn - { - get; - } + public abstract T DeterminantLn { get; } /// /// Solves a system of linear equations, AX = B, with A Cholesky factorized. diff --git a/src/Numerics/LinearAlgebra/Factorization/Evd.cs b/src/Numerics/LinearAlgebra/Factorization/Evd.cs index d07158ec..e85aa38b 100644 --- a/src/Numerics/LinearAlgebra/Factorization/Evd.cs +++ b/src/Numerics/LinearAlgebra/Factorization/Evd.cs @@ -60,118 +60,39 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets or sets a value indicating whether matrix is symmetric or not /// - public bool IsSymmetric - { - get; - protected set; - } + public bool IsSymmetric { get; protected set; } /// /// Gets the absolute value of determinant of the square matrix for which the EVD was computed. /// - public abstract T Determinant - { - get; - } + public abstract T Determinant { get; } /// /// Gets the effective numerical matrix rank. /// /// The number of non-negligible singular values. - public abstract int Rank - { - get; - } + public abstract int Rank { get; } /// /// Gets a value indicating whether the matrix is full rank or not. /// /// true if the matrix is full rank; otherwise false. - public abstract bool IsFullRank - { - get; - } + public abstract bool IsFullRank { get; } /// /// Gets or sets the eigen values (λ) of matrix in ascending value. /// - protected Vector VectorEv - { - get; - set; - } + protected Vector VectorEv { get; set; } /// /// Gets or sets eigenvectors. /// - protected Matrix MatrixEv - { - get; - set; - } + protected Matrix MatrixEv { get; set; } /// /// Gets or sets the block diagonal eigenvalue matrix. /// - protected Matrix MatrixD - { - get; - set; - } - - /// - /// Internal method which routes the call to perform the singular value decomposition to the appropriate class. - /// - /// The matrix to factor. - /// An EVD object. - internal static Evd Create(Matrix matrix) - { - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseEvd(dense) as Evd; - } - - return new Double.Factorization.UserEvd(matrix as Matrix) as Evd; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseEvd(dense) as Evd; - } - - return new Single.Factorization.UserEvd(matrix as Matrix) as Evd; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseEvd(dense) as Evd; - } - - return new LinearAlgebra.Complex.Factorization.UserEvd(matrix as Matrix) as Evd; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseEvd(dense) as Evd; - } - - return new LinearAlgebra.Complex32.Factorization.UserEvd(matrix as Matrix) as Evd; - } - - throw new NotSupportedException(); - } + protected Matrix MatrixD { get; set; } /// Returns the eigen values as a . /// The eigen values. diff --git a/src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs b/src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs index 4e2f12aa..68b09636 100644 --- a/src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs +++ b/src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs @@ -28,12 +28,6 @@ using System; namespace MathNet.Numerics.LinearAlgebra.Factorization { - using Numerics; - -#if !NOSYSNUMERICS - using System.Numerics; -#endif - /// /// A class which encapsulates the functionality of the QR decomposition Modified Gram-Schmidt Orthogonalization. /// Any real square matrix A may be decomposed as A = QR where Q is an orthogonal mxn matrix and R is an nxn upper triangular matrix. @@ -45,58 +39,5 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization public abstract class GramSchmidt : QR where T : struct, IEquatable, IFormattable { - /// - /// Internal method which routes the call to perform the QR factorization to the appropriate class. - /// - /// The matrix to factor. - /// A QR factorization object. - internal static GramSchmidt Create(Matrix matrix) - { - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseGramSchmidt(dense) as GramSchmidt; - } - - return new Double.Factorization.UserGramSchmidt(matrix as Matrix) as GramSchmidt; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseGramSchmidt(dense) as GramSchmidt; - } - - return new Single.Factorization.UserGramSchmidt(matrix as Matrix) as GramSchmidt; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseGramSchmidt(dense) as GramSchmidt; - } - - return new LinearAlgebra.Complex.Factorization.UserGramSchmidt(matrix as Matrix) as GramSchmidt; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseGramSchmidt(dense) as GramSchmidt; - } - - return new LinearAlgebra.Complex32.Factorization.UserGramSchmidt(matrix as Matrix) as GramSchmidt; - } - - throw new NotSupportedException(); - } } } diff --git a/src/Numerics/LinearAlgebra/Factorization/LU.cs b/src/Numerics/LinearAlgebra/Factorization/LU.cs index 6927dba2..db569eea 100644 --- a/src/Numerics/LinearAlgebra/Factorization/LU.cs +++ b/src/Numerics/LinearAlgebra/Factorization/LU.cs @@ -28,12 +28,6 @@ using System; namespace MathNet.Numerics.LinearAlgebra.Factorization { - using Numerics; - -#if !NOSYSNUMERICS - using System.Numerics; -#endif - /// /// A class which encapsulates the functionality of an LU factorization. /// For a matrix A, the LU factorization is a pair of lower triangular matrix L and @@ -56,74 +50,12 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets or sets both the L and U factors in the same matrix. /// - protected Matrix Factors - { - get; - set; - } + protected Matrix Factors { get; set; } /// /// Gets or sets the pivot indices of the LU factorization. /// - protected int[] Pivots - { - get; - set; - } - - /// - /// Internal method which routes the call to perform the LU factorization to the appropriate class. - /// - /// The matrix to factor. - /// An LU factorization object. - internal static LU Create(Matrix matrix) - { - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseLU(dense) as LU; - } - - return new Double.Factorization.UserLU(matrix as Matrix) as LU; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseLU(dense) as LU; - } - - return new Single.Factorization.UserLU(matrix as Matrix) as LU; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseLU(dense) as LU; - } - - return new LinearAlgebra.Complex.Factorization.UserLU(matrix as Matrix) as LU; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseLU(dense) as LU; - } - - return new LinearAlgebra.Complex32.Factorization.UserLU(matrix as Matrix) as LU; - } - - throw new NotSupportedException(); - } + protected int[] Pivots { get; set; } /// /// Gets the lower triangular factor. @@ -167,10 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets the determinant of the matrix for which the LU factorization was computed. /// - public abstract T Determinant - { - get; - } + public abstract T Determinant { get; } /// /// Solves a system of linear equations, AX = B, with A LU factorized. diff --git a/src/Numerics/LinearAlgebra/Factorization/QR.cs b/src/Numerics/LinearAlgebra/Factorization/QR.cs index 280c7a26..137387a8 100644 --- a/src/Numerics/LinearAlgebra/Factorization/QR.cs +++ b/src/Numerics/LinearAlgebra/Factorization/QR.cs @@ -28,12 +28,6 @@ using System; namespace MathNet.Numerics.LinearAlgebra.Factorization { - using Numerics; - -#if !NOSYSNUMERICS - using System.Numerics; -#endif - /// /// The type of QR factorization go perform. /// @@ -69,85 +63,17 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets or sets orthogonal Q matrix /// - protected Matrix MatrixQ - { - get; - set; - } + protected Matrix MatrixQ { get; set; } /// /// Gets or sets upper triangular factor R /// - protected Matrix MatrixR - { - get; - set; - } + protected Matrix MatrixR { get; set; } /// /// The QR factorization method. /// - protected QRMethod QrMethod - { - get; - set; - } - - /// - /// Internal method which routes the call to perform the QR factorization to the appropriate class. - /// - /// The matrix to factor. - /// The type of QR factorization to perform. - /// A QR factorization object. - internal static QR Create(Matrix matrix, QRMethod method = QRMethod.Full) - { - - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseQR(dense, method) as QR; - } - - return new Double.Factorization.UserQR(matrix as Matrix, method) as QR; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseQR(dense, method) as QR; - } - - return new Single.Factorization.UserQR(matrix as Matrix, method) as QR; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseQR(dense, method) as QR; - } - - return new LinearAlgebra.Complex.Factorization.UserQR(matrix as Matrix, method) as QR; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseQR(dense, method) as QR; - } - - return new LinearAlgebra.Complex32.Factorization.UserQR(matrix as Matrix, method) as QR; - } - - throw new NotSupportedException(); - } + protected QRMethod QrMethod { get; set; } /// /// Gets orthogonal Q matrix @@ -174,19 +100,13 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// - public abstract T Determinant - { - get; - } + public abstract T Determinant { get; } /// /// Gets a value indicating whether the matrix is full rank or not. /// /// true if the matrix is full rank; otherwise false. - public abstract bool IsFullRank - { - get; - } + public abstract bool IsFullRank { get; } /// /// Solves a system of linear equations, AX = B, with A QR factorized. diff --git a/src/Numerics/LinearAlgebra/Factorization/Svd.cs b/src/Numerics/LinearAlgebra/Factorization/Svd.cs index 740f4e8a..f520cc24 100644 --- a/src/Numerics/LinearAlgebra/Factorization/Svd.cs +++ b/src/Numerics/LinearAlgebra/Factorization/Svd.cs @@ -28,17 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using MathNet.Numerics.Properties; using System; +using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Factorization { - using Numerics; - -#if !NOSYSNUMERICS - using System.Numerics; -#endif - /// /// A class which encapsulates the functionality of the singular value decomposition (SVD). /// Suppose M is an m-by-n matrix whose entries are real numbers. @@ -60,128 +54,45 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization /// /// Gets or sets a value indicating whether to compute U and VT matrices during SVD factorization or not /// - protected bool ComputeVectors - { - get; - set; - } + protected bool ComputeVectors { get; set; } /// /// Gets or sets the singular values (Σ) of matrix in ascending value. /// - protected Vector VectorS - { - get; - set; - } + protected Vector VectorS { get; set; } /// /// Gets or sets left singular vectors (U - m-by-m unitary matrix) /// - protected Matrix MatrixU - { - get; - set; - } + protected Matrix MatrixU { get; set; } /// /// Gets or sets transpose right singular vectors (transpose of V, an n-by-n unitary matrix /// - protected Matrix MatrixVT - { - get; - set; - } + protected Matrix MatrixVT { get; set; } /// /// Gets the effective numerical matrix rank. /// /// The number of non-negligible singular values. - public abstract int Rank - { - get; - } - - /// - /// Internal method which routes the call to perform the singular value decomposition to the appropriate class. - /// - /// The matrix to factor. - /// Compute the singular U and VT vectors or not. - /// An SVD object. - internal static Svd Create(Matrix matrix, bool computeVectors) - { - if (typeof(T) == typeof(double)) - { - var dense = matrix as Double.DenseMatrix; - if (dense != null) - { - return new Double.Factorization.DenseSvd(dense, computeVectors) as Svd; - } - - return new Double.Factorization.UserSvd(matrix as Matrix, computeVectors) as Svd; - } - - if (typeof(T) == typeof(float)) - { - var dense = matrix as Single.DenseMatrix; - if (dense != null) - { - return new Single.Factorization.DenseSvd(dense, computeVectors) as Svd; - } - - return new Single.Factorization.UserSvd(matrix as Matrix, computeVectors) as Svd; - } - - if (typeof(T) == typeof(Complex)) - { - var dense = matrix as LinearAlgebra.Complex.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex.Factorization.DenseSvd(dense, computeVectors) as Svd; - } - - return new LinearAlgebra.Complex.Factorization.UserSvd(matrix as Matrix, computeVectors) as Svd; - } - - if (typeof(T) == typeof(Complex32)) - { - var dense = matrix as LinearAlgebra.Complex32.DenseMatrix; - if (dense != null) - { - return new LinearAlgebra.Complex32.Factorization.DenseSvd(dense, computeVectors) as Svd; - } - - return new LinearAlgebra.Complex32.Factorization.UserSvd(matrix as Matrix, computeVectors) as Svd; - } - - throw new NotSupportedException(); - } + public abstract int Rank { get; } /// /// Gets the two norm of the . /// /// The 2-norm of the . - public abstract T Norm2 - { - get; - } + public abstract T Norm2 { get; } /// /// Gets the condition number max(S) / min(S) /// /// The condition number. - public abstract T ConditionNumber - { - get; - } + public abstract T ConditionNumber { get; } /// /// Gets the determinant of the square matrix for which the SVD was computed. /// - public abstract T Determinant - { - get; - } + public abstract T Determinant { get; } /// Returns the left singular vectors as a . /// The left singular vectors. The matrix will be null, if computeVectors in the constructor is set to false. diff --git a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs index ff3a0e0a..8ee38084 100644 --- a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs @@ -1032,7 +1032,7 @@ namespace MathNet.Numerics.LinearAlgebra /// effective numerical rank, obtained from SVD public virtual int Rank() { - return Svd.Create(this, false).Rank; + return Svd(false).Rank; } /// Calculates the condition number of this matrix. @@ -1040,7 +1040,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The condition number is calculated using singular value decomposition. public virtual T ConditionNumber() { - return Svd.Create(this, false).ConditionNumber; + return Svd(false).ConditionNumber; } /// Computes the determinant of this matrix. @@ -1052,7 +1052,7 @@ namespace MathNet.Numerics.LinearAlgebra throw new ArgumentException(Resources.ArgumentMatrixSquare); } - return LU.Create(this).Determinant; + return LU().Determinant; } /// Computes the inverse of this matrix. @@ -1064,7 +1064,7 @@ namespace MathNet.Numerics.LinearAlgebra throw new ArgumentException(Resources.ArgumentMatrixSquare); } - return LU.Create(this).Inverse(); + return LU().Inverse(); } /// diff --git a/src/Numerics/LinearAlgebra/Single/ExtensionMethods.cs b/src/Numerics/LinearAlgebra/Matrix.Factorization.cs similarity index 61% rename from src/Numerics/LinearAlgebra/Single/ExtensionMethods.cs rename to src/Numerics/LinearAlgebra/Matrix.Factorization.cs index 1c495160..ba1fd360 100644 --- a/src/Numerics/LinearAlgebra/Single/ExtensionMethods.cs +++ b/src/Numerics/LinearAlgebra/Matrix.Factorization.cs @@ -1,9 +1,11 @@ -// +// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// Copyright (c) 2009-2010 Math.NET +// +// Copyright (c) 2009-2013 Math.NET +// // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -12,8 +14,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -25,75 +29,50 @@ // using MathNet.Numerics.LinearAlgebra.Factorization; -using MathNet.Numerics.LinearAlgebra.Single.Factorization; -namespace MathNet.Numerics.LinearAlgebra.Single +namespace MathNet.Numerics.LinearAlgebra { /// - /// Extension methods which return factorizations for the various matrix classes. + /// Defines the base class for Matrix classes. /// - public static class ExtensionMethods + public abstract partial class Matrix { /// /// Computes the Cholesky decomposition for a matrix. /// - /// The matrix to factor. /// The Cholesky decomposition object. - public static Cholesky Cholesky(this Matrix matrix) - { - return (Cholesky)Cholesky.Create(matrix); - } + public abstract Cholesky Cholesky(); /// /// Computes the LU decomposition for a matrix. /// - /// The matrix to factor. /// The LU decomposition object. - public static LU LU(this Matrix matrix) - { - return (LU)LU.Create(matrix); - } + public abstract LU LU(); /// /// Computes the QR decomposition for a matrix. /// - /// The matrix to factor. /// The type of QR factorization to perform. /// The QR decomposition object. - public static QR QR(this Matrix matrix, QRMethod method = QRMethod.Full) - { - return (QR)QR.Create(matrix, method); - } + public abstract QR QR(QRMethod method = QRMethod.Thin); /// /// Computes the QR decomposition for a matrix using Modified Gram-Schmidt Orthogonalization. /// - /// The matrix to factor. /// The QR decomposition object. - public static GramSchmidt GramSchmidt(this Matrix matrix) - { - return (GramSchmidt)GramSchmidt.Create(matrix); - } + public abstract GramSchmidt GramSchmidt(); /// /// Computes the SVD decomposition for a matrix. /// - /// The matrix to factor. /// Compute the singular U and VT vectors or not. /// The SVD decomposition object. - public static Svd Svd(this Matrix matrix, bool computeVectors) - { - return (Svd)Svd.Create(matrix, computeVectors); - } + public abstract Svd Svd(bool computeVectors); /// /// Computes the EVD decomposition for a matrix. /// - /// The matrix to factor. /// The EVD decomposition object. - public static Evd Evd(this Matrix matrix) - { - return (Evd)Evd.Create(matrix); - } + public abstract Evd Evd(); } } diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index bdfcf69b..49de914d 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -1215,7 +1215,7 @@ namespace MathNet.Numerics.LinearAlgebra /// In a later release, it will be replaced with a sparse implementation. public virtual T L2Norm() { - return Svd.Create(this, false).Norm2; + return Svd(false).Norm2; } /// Calculates the Frobenius norm of this matrix. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index e2a8b8ff..78c27dd7 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -33,6 +33,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Factorization; +using MathNet.Numerics.LinearAlgebra.Single.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; using MathNet.Numerics.Providers.LinearAlgebra; @@ -1032,5 +1034,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single return (DenseMatrix)leftSide.Modulus(rightSide); } + + public override Cholesky Cholesky() + { + return new DenseCholesky(this); + } + + public override LU LU() + { + return new DenseLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new DenseQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new DenseGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new DenseSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new DenseEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 8f5b37cb..c0d2ab08 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -29,6 +29,8 @@ // using System; +using MathNet.Numerics.LinearAlgebra.Single.Factorization; +using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; @@ -456,5 +458,35 @@ namespace MathNet.Numerics.LinearAlgebra.Single return sum; } + + public override Cholesky Cholesky() + { + return new UserCholesky(this); + } + + public override LU LU() + { + return new UserLU(this); + } + + public override QR QR(QRMethod method = QRMethod.Thin) + { + return new UserQR(this, method); + } + + public override GramSchmidt GramSchmidt() + { + return new UserGramSchmidt(this); + } + + public override Svd Svd(bool computeVectors) + { + return new UserSvd(this, computeVectors); + } + + public override Evd Evd() + { + return new UserEvd(this); + } } } diff --git a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs index 928aacff..1ed924a1 100644 --- a/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs +++ b/src/Numerics/LinearAlgebra/Single/Solvers/Iterator.cs @@ -39,6 +39,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers /// public static class Iterator { + + // TODO: Refactor + /// /// Creates a default iterator with all the objects. /// diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index e0bfb248..209fd8c6 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -122,6 +122,7 @@ + @@ -181,7 +182,6 @@ - @@ -199,7 +199,6 @@ Code - @@ -210,7 +209,6 @@ - @@ -291,7 +289,6 @@ -