//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// 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
// 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;
using IStation.Numerics.Providers.LinearAlgebra;
namespace IStation.Numerics.LinearAlgebra.Complex.Factorization
{
using Complex = System.Numerics.Complex;
///
/// A class which encapsulates the functionality of the singular value decomposition (SVD) for .
/// 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.
///
internal sealed class DenseSvd : Svd
{
///
/// Initializes a new instance of the class. This object will compute the
/// the singular value decomposition when the constructor is called and cache it's decomposition.
///
/// The matrix to factor.
/// Compute the singular U and VT vectors or not.
/// If is null.
/// If SVD algorithm failed to converge with matrix .
public static DenseSvd Create(DenseMatrix matrix, bool computeVectors)
{
var nm = Math.Min(matrix.RowCount, matrix.ColumnCount);
var s = new DenseVector(nm);
var u = new DenseMatrix(matrix.RowCount);
var vt = new DenseMatrix(matrix.ColumnCount);
LinearAlgebraControl.Provider.SingularValueDecomposition(computeVectors, ((DenseMatrix) matrix.Clone()).Values, matrix.RowCount, matrix.ColumnCount, s.Values, u.Values, vt.Values);
return new DenseSvd(s, u, vt, computeVectors);
}
DenseSvd(Vector s, Matrix u, Matrix vt, bool vectorsComputed)
: base(s, u, vt, vectorsComputed)
{
}
///
/// Solves a system of linear equations, AX = B, with A SVD factorized.
///
/// The right hand side , B.
/// The left hand side , X.
public override void Solve(Matrix input, Matrix result)
{
if (!VectorsComputed)
{
throw new InvalidOperationException("The singular vectors were not computed.");
}
// The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount)
{
throw new ArgumentException("Matrix column dimensions must agree.");
}
// The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows
if (U.RowCount != input.RowCount)
{
throw new ArgumentException("Matrix row dimensions must agree.");
}
// The solution X row dimension is equal to the column dimension of A
if (VT.ColumnCount != result.RowCount)
{
throw new ArgumentException("Matrix column dimensions must agree.");
}
if (input is DenseMatrix dinput && result is DenseMatrix dresult)
{
LinearAlgebraControl.Provider.SvdSolveFactored(U.RowCount, VT.ColumnCount, ((DenseVector) S).Values, ((DenseMatrix) U).Values, ((DenseMatrix) VT).Values, dinput.Values, input.ColumnCount, dresult.Values);
}
else
{
throw new NotSupportedException("Can only do SVD factorization for dense matrices at the moment.");
}
}
///
/// Solves a system of linear equations, Ax = b, with A SVD factorized.
///
/// The right hand side vector, b.
/// The left hand side , x.
public override void Solve(Vector input, Vector result)
{
if (!VectorsComputed)
{
throw new InvalidOperationException("The singular vectors were not computed.");
}
// Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries
if (U.RowCount != input.Count)
{
throw new ArgumentException("All vectors must have the same dimensionality.");
}
// Check that x is a column vector with n entries
if (VT.ColumnCount != result.Count)
{
throw Matrix.DimensionsDontMatch(VT, result);
}
if (input is DenseVector dinput && result is DenseVector dresult)
{
LinearAlgebraControl.Provider.SvdSolveFactored(U.RowCount, VT.ColumnCount, ((DenseVector) S).Values, ((DenseMatrix) U).Values, ((DenseMatrix) VT).Values, dinput.Values, 1, dresult.Values);
}
else
{
throw new NotSupportedException("Can only do SVD factorization for dense vectors at the moment.");
}
}
}
}