Extreme Optimization > User's Guide > Vector and Matrix Library > Matrix Decompositions > The Singular Value Decomposition

Extreme Optimization User's Guide

User's Guide

Up: Matrix Decompositions Next: Solving Linear Systems and Related Operations Previous: The Non-Symmetric Eigenvalue Decomposition Contents

The Singular Value Decomposition

The Singular Value Decomposition or SVD of a matrix expresses the matrix as the product of an orthogonal matrix, a diagonal matrix, and an orthogonal matrix. The matrix can be of any type, and of any shape.The SVD decomposition is usually written as

A = USVT,

where U and V are square, orthogonal matrices, and S is a diagonal matrix with the same shape as A.

The singular value decomposition is the most stable of all decompositions. Aside from its own, unique, uses, it is used in situations where the highest possible accuracy is required. The numerical rank is determined using the singular value decomposition, as is the exact condition number, which is the ratio of the largest to the smallest singular value. On the downside, the method is slower than all other decompositions.

In the Extreme Optimization Mathematics Library for .NET, the singular value decomposition is implemented by the SingularValueDecomposition class.

The SingularValueDecompositionclass

The SingularValueDecomposition class represents the singular value decomposition of a matrix. It has four constructors. The first variant takes a Matrix as its only parameter.

C# CopyCode imageCopy Code
GeneralMatrix aSVD = new GeneralMatrix(4, 4,
    new double[] 
{
    1.80, 5.25, 1.58, -1.11, 
    2.88,-2.95, -2.69, -0.66, 
    2.05,-0.95, -2.90, -0.59, 
    -0.89,-3.80,-1.04, 0.80
});
SingularValueDecomposition svd = new SingularValueDecomposition(aSVD);
Visual Basic CopyCode imageCopy Code
Dim aSVD As GeneralMatrix = New GeneralMatrix(4, 4, _
    New Double() _
{ _
    1.8, 5.25, 1.58, -1.11, _
    2.88, -2.95, -2.69, -0.66, _
    2.05, -0.95, -2.9, -0.59, _
    -0.89, -3.8, -1.04, 0.8 _
})
Dim svd As SingularValueDecomposition = _
    New SingularValueDecomposition(aSVD)

The second constructor takes a GeneralMatrix as its first parameter. The second parameter is a Boolean value that specifies whether the contents of the first parameter should be overwritten by the SVD decomposition. The default is false.

C# CopyCode imageCopy Code
SingularValueDecomposition svd2 =    new SingularValueDecomposition(A, true);
Visual Basic CopyCode imageCopy Code
Dim svd2 As SingularValueDecomposition = _
    New SingularValueDecomposition(A, True)

The two remaining constructors let you specify which calculations are to be performed. Because the calculation of the left and right singular vectors is relatively expensive and not always needed, it is possible to specify that only the singular values need to be computed. This is done in one of two ways. The first is by passing a value of type SingularValueDecompositionFactors to the constructor as the second parameter. Alternatively, you can set the RequestedFactors property to a value of the same type before the decomposition is computed. The possible values for the SingularValueDecompositionFactors enumeration are listed below:

Value Description
SingularValues The singular values are required. This is always included.
LeftSingularVectors The left singular vectors are required.
RightSingularVectors The right singular vectors are required.
All The singular values as well as the left and right singular vectors are required.
Table 1. Possible values for the SingularValueDecompositionFactors
enumeration.

The Decompose method performs the actual decomposition. This method copies the matrix if necessary. It then calls the appropriate LAPACK routine to perform the actual decomposition. This method is called by other methods as needed. You will rarely need to call it explicitly.

Once the decomposition is computed, a number of operations can be performed in much less time. You can repeatedly solve a system of simultaneous linear equations with different right-hand sides. You can also calculate the determinant (only the magnitude) and the inverse of the base matrix:

C# CopyCode imageCopy Code
GeneralMatrix bSVD = new GeneralMatrix(4, 2, new double[]
{
    9.52,24.35,0.77,-6.22,
    18.47,2.25,-13.28,-6.21
});
Matrix xSVD = lu.Solve(bSVD);
Console.WriteLine("Ax = b -> x = {0}", xSVD);
Console.WriteLine("Inv A = {0}", svd.GetInverse().ToString("F4"));
Console.WriteLine("Det A = {0}", svd.GetDeterminant());
Visual Basic CopyCode imageCopy Code
Dim bSVD As GeneralMatrix = New GeneralMatrix(4, 2, New Double() _
{ _
    9.52, 24.35, 0.77, -6.22, _
    18.47, 2.25, -13.28, -6.21 _
})
Dim xSVD As Matrix = lu.Solve(bSVD)
Console.WriteLine("Ax = b -> x = {0}", xSVD)
Console.WriteLine("Inv A = {0}", svd.GetInverse().ToString("F4"))
Console.WriteLine("Det A = {0}", svd.GetDeterminant())

A number of methods are unique to the singular value decomposition. The GetPseudoInverse method returns a GeneralMatrix that is the Moore-Penrose pseudo-inverse of the matrix. A pseudo-inverse is a generalization of the inverse of a matrix to matrices that are not square or singular. The Moore-Penrose pseudo-inverse is the most common, and is defined as the unique matrix A+ that satisfies the following four conditions:

AA+A = A (AA+)T =AA+
A+AA+ =A+ (A+A)T =A+A

The SingularValues property returns a GeneralVector containing the singular values in decreasing order. The SingularValueMatrix property returns a DiagonalMatrix with the same shape as the decomposed matrix with the singular values on the diagonal in decreasing order.

The LeftSingularVectors and RightSingularVectors properties return a GeneralMatrix whose columns are the left and right singular vectors corresponding to each singular value. When the number of rows of the matrix equals the number of columns, these matrices correspond to the matrices U and V of the decomposition. If the number of rows is greater than the number of columns, then LeftSingularVectors only returns the first c columns of U, where c is the number of columns. If the number of rows is less than the number of columns, then RightSingularVectors only returns the first r columns of V, where r is the number of rows.

C# CopyCode imageCopy Code
Console.WriteLine("S = {0:F4}", svd.SingularValues)
Console.WriteLine("U = {0:F4}", svd.LeftSingularVectors)
Console.WriteLine("V = {0:F4}", svd.RightSingularVectors)
Visual Basic CopyCode imageCopy Code
Console.WriteLine("S = {0:F4}", svd.SingularValues)
Console.WriteLine("U = {0:F4}", svd.LeftSingularVectors)
Console.WriteLine("V = {0:F4}", svd.RightSingularVectors)

The managed implementation of the singular value decomposition is based on the LINPACK routine DSVDC. The native version uses the LAPACK routine DGESDD.

Up: Matrix Decompositions Next: Solving Linear Systems and Related Operations Previous: The Non-Symmetric Eigenvalue Decomposition Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army