// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // // Copyright (c) 2009-2015 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 System; namespace IStation.Numerics.LinearAlgebra.Factorization { /// /// 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. /// Then there exists a factorization of the form M = UΣVT where: /// - U is an m-by-m unitary matrix; /// - Σ is m-by-n diagonal matrix with nonnegative real numbers on the diagonal; /// - VT denotes transpose of V, an n-by-n unitary matrix; /// Such a factorization is called a singular-value decomposition of M. A common convention is to order the diagonal /// entries Σ(i,i) in descending order. In this case, the diagonal matrix Σ is uniquely determined /// by M (though the matrices U and V are not). The diagonal entries of Σ are known as the singular values of M. /// /// /// The computation of the singular value decomposition is done at construction time. /// /// Supported data types are double, single, , and . public abstract class Svd : ISolver where T : struct, IEquatable, IFormattable { readonly Lazy> _lazyW; /// Indicating whether U and VT matrices have been computed during SVD factorization. protected readonly bool VectorsComputed; protected Svd(Vector s, Matrix u, Matrix vt, bool vectorsComputed) { S = s; U = u; VT = vt; VectorsComputed = vectorsComputed; _lazyW = new Lazy>(ComputeW); } Matrix ComputeW() { var rows = U.RowCount; var columns = VT.ColumnCount; var result = Matrix.Build.SameAs(U, rows, columns); for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { if (i == j) { result.At(i, i, S[i]); } } } return result; } /// /// Gets the singular values (Σ) of matrix in ascending value. /// public Vector S { get; } /// /// Gets the left singular vectors (U - m-by-m unitary matrix) /// public Matrix U { get; } /// /// Gets the transpose right singular vectors (transpose of V, an n-by-n unitary matrix) /// public Matrix VT { get; } /// /// Returns the singular values as a diagonal . /// /// The singular values as a diagonal . public Matrix W => _lazyW.Value; /// /// Gets the effective numerical matrix rank. /// /// The number of non-negligible singular values. public abstract int Rank { get; } /// /// Gets the two norm of the . /// /// The 2-norm of the . public abstract double L2Norm { get; } /// /// Gets the condition number max(S) / min(S) /// /// The condition number. public abstract T ConditionNumber { get; } /// /// Gets the determinant of the square matrix for which the SVD was computed. /// public abstract T Determinant { get; } /// /// Solves a system of linear equations, AX = B, with A SVD factorized. /// /// The right hand side , B. /// The left hand side , X. public virtual Matrix Solve(Matrix input) { if (!VectorsComputed) { throw new InvalidOperationException("The singular vectors were not computed."); } var x = Matrix.Build.SameAs(U, VT.ColumnCount, input.ColumnCount, fullyMutable: true); Solve(input, x); return x; } /// /// Solves a system of linear equations, AX = B, with A SVD factorized. /// /// The right hand side , B. /// The left hand side , X. public abstract void Solve(Matrix input, Matrix result); /// /// Solves a system of linear equations, Ax = b, with A SVD factorized. /// /// The right hand side vector, b. /// The left hand side , x. public virtual Vector Solve(Vector input) { if (!VectorsComputed) { throw new InvalidOperationException("The singular vectors were not computed."); } var x = Vector.Build.SameAs(U, VT.ColumnCount); Solve(input, x); return x; } /// /// Solves a system of linear equations, Ax = b, with A SVD factorized. /// /// The right hand side vector, b. /// The left hand side , x. public abstract void Solve(Vector input, Vector result); } }