Extreme Optimization > QuickStart Samples > Matrix Decompositions QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Matrix Decompositions QuickStart Sample (C#)

Illustrates the use of matrix decomposition classes (Extreme.Mathematics.LinearAlgebra namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The Vector and GeneralMatrix classes reside in the 
    // Extreme.Mathematics.LinearAlgebra namespace.
    using Extreme.Mathematics.LinearAlgebra;

    /// <summary>
    /// Illustrates the use of matrix decompositions for solving systems of
    /// simultaneous linear equations and related operations using the 
    /// Decomposition class and its derived classes from the
    /// Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    /// Mathematics Library for .NET.
    /// </summary>
    class MatrixDecompositions
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // For details on the basic workings of Vector 
            // objects, including constructing, copying and
            // cloning vectors, see the BasicVectors QuickStart
            // Sample.
            //
            // For details on the basic workings of Matrix
            // objects, including constructing, copying and
            // cloning vectors, see the BasicVectors QuickStart
            // Sample.
            //

            //
            // LU Decomposition
            //

            // The LU decomposition of a matrix rewrites a matrix A in the
            // form A = PLU with P a permutation matrix, L a unit-
            // lower triangular matrix, and U an upper triangular matrix.

            GeneralMatrix aLU = 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
            });

            GeneralMatrix bLU = new GeneralMatrix(4, 2, new double[]
            {
                9.52,24.35,0.77,-6.22,
                18.47,2.25,-13.28,-6.21
            });

            // The constructor takes one or two parameters. The second
            // parameter is a bool value that indicates whether the
            // matrix should be overwritten with its decomposition.
            LUDecomposition lu = new LUDecomposition(aLU, true);
            Console.WriteLine("A = {0}", aLU);

            // The Decompose method performs the decomposition. You don't need
            // to call it explicitly, as it is called automatically as needed.
            
            // The IsSingular method checks for singularity.
            Console.WriteLine("'A is singular' is {0}.", lu.IsSingular());
            // The LowerTriangularFactor and UpperTriangularFactor properties
            // return the two main components of the decomposition.
            Console.WriteLine("L = {0}", lu.UpperTriangularFactor.ToString("F4"));
            Console.WriteLine("U = {0}", lu.UpperTriangularFactor.ToString("F4"));

            // GetInverse() gives the matrix inverse, 
            // Determinant() the determinant:
            Console.WriteLine("Inv A = {0}", lu.GetInverse().ToString("F4"));
            Console.WriteLine("Det A = {0}", lu.GetDeterminant());

            // The Solve method solves a system of simultaneous linear equations
            // with one or more right-hand-sides:
            Matrix xLU = lu.Solve(bLU);
            Console.WriteLine("x = {0}", xLU);

            // The permutation matrix P is not available directly. 
            // You can apply the permutation by calling the ApplyP method:
            Console.WriteLine("Px = {0}", lu.ApplyP(xLU));

            //
            // QR Decomposition
            //

            // The QR decomposition of a matrix A rewrites the matrix
            // in the form A = QR, with Q a square, orthogonal matrix,
            // and R an upper triangular matrix.

            GeneralMatrix aQR = new GeneralMatrix(5, 3,
                new double[] 
                {
                    2.0, 2.0, 1.6, 2.0, 1.2,
                    2.5, 2.5,-0.4,-0.5,-0.3,
                    2.5, 2.5, 2.8, 0.5,-2.9
                });
            GeneralVector bQR = new GeneralVector(1.1, 0.9, 0.6, 0.0,-0.8);

            // The constructor takes one or two parameters. The second
            // parameter is a bool value that indicates whether the
            // matrix should be overwritten with its decomposition.
            QRDecomposition qr = new QRDecomposition(aQR, true);
            Console.WriteLine("A = {0}", aQR);

            // The Decompose method performs the decomposition. You don't need
            // to call it explicitly, as it is called automatically as needed.
            
            // The IsSingular method checks for singularity.
            Console.WriteLine("'A is singular' is {0}.", qr.IsSingular());

            // GetInverse() gives the matrix inverse, 
            // Determinant() the determinant:
            Console.WriteLine("Inv A = {0}", qr.GetInverse().ToString("F4"));
            Console.WriteLine("Det A = {0}", qr.GetDeterminant());

            // The Solve method solves a system of simultaneous
            // linear equations with one or more right-hand-sides.
            // If the matrix is over-determined, you can use 
            // the LeastSquaresSolve method to find a least squares solution:
            GeneralVector xQR = (GeneralVector)qr.LeastSquaresSolve(bQR);
            Console.WriteLine("x = {0}", xQR);

            // The OrthogonalFactor and UpperTriangularFactor properties
            // return the two main components of the decomposition.
            Console.WriteLine("Q = {0}", qr.OrthogonalFactor.ToString("F4"));
            Console.WriteLine("R = {0}", qr.UpperTriangularFactor.ToString("F4"));

            // You don't usually need to form Q explicitly. You can multiply
            // a vector or matrix on either side by Q using the ApplyQ method:
            Console.WriteLine("Qx = {0}", qr.ApplyQ(xQR));
            Console.WriteLine("transpose(Q)x = {0}",
                qr.ApplyQ(TransposeOperation.Transpose, xQR));

            //
            // Singular Value Decomposition
            //

            // The singular value decomposition of a matrix A rewrites
            // the matrix in the form A = USVt, with U and V orthogonal 
            // matrices, S a diagonal matrix. The diagonal elements of S 
            // are called the singular values.

            GeneralMatrix aSvd = new GeneralMatrix(3, 5,
                new double[] 
                {
                    2.0, 2.0, 1.6, 2.0, 1.2,
                    2.5, 2.5,-0.4,-0.5,-0.3,
                    2.5, 2.5, 2.8, 0.5,-2.9
                }, MatrixElementOrder.RowMajor);
            GeneralVector bSvd = new GeneralVector(1.1, 0.9, 0.6);

            // The constructor takes one or two parameters. The second
            // parameter indicates which parts of the decomposition
            // are to be calculated. The default is All.
            SingularValueDecomposition svd =
                new SingularValueDecomposition(aSvd);
            Console.WriteLine("A = {0:F1}", aSvd);

            // The Decompose method performs the decomposition. You don't need
            // to call it explicitly, as it is called automatically as needed.
            
            // The IsSingular method checks for singularity.
            Console.WriteLine("'A is singular' is {0:F6}.", svd.IsSingular());

            // Several methods are specific to this class.
            // The GetPseudoInverse method returns the Moore-Penrose
            // pseudo-inverse, a generalization of the inverse 
            // of a matrix to rectangular and/or singular matrices:
            Matrix aInv = svd.GetPseudoInverse();

            // It can be used to solve over- or under-determined systems.
            Vector xSvd = aInv * bSvd;
            Console.WriteLine("x = {0:F6}", xSvd);

            // The SingularValues property returns a vector that contains
            // the singular values in descending order:
            Console.WriteLine("S = {0:F6}", svd.SingularValues);            

            // The LeftSingularVectors and RightSingularVectors properties
            // return matrices that contain the U and V factors
            // of the decomposition.
            Console.WriteLine("U = {0:F6}", svd.LeftSingularVectors);
            Console.WriteLine("V = {0:F6}", svd.RightSingularVectors);

            //
            // Cholesky decomposition
            //

            // The Cholesky decomposition of a symmetric matrix A
            // rewrites the matrix in the form A = GGt with
            // G a lower-triangular matrix.

            // Remember the column-major storage mode: each line of
            // components contains one COLUMN of the matrix.
            SymmetricMatrix aC = new SymmetricMatrix(4, 
                new double[]
            {
                4.16,-3.12, 0.56,-0.10,
                0, 5.03,-0.83, 1.18,
                0,0, 0.76, 0.34,
                0,0,0, 1.18
            }, MatrixTriangleMode.Lower);
            GeneralMatrix bC = new GeneralMatrix(4, 2,
                new double[] {8.70,-13.35,1.89,-4.14,8.30,2.13,1.61,5.00});

            // The constructor takes one or two parameters. The second
            // parameter is a bool value that indicates whether the
            // matrix should be overwritten with its decomposition.
            CholeskyDecomposition c = new CholeskyDecomposition(aC, true);
            Console.WriteLine("A = {0:F2}", aC);

            // The Decompose method performs the decomposition. You don't need
            // to call it explicitly, as it is called automatically as needed.
            
            // The IsSingular method checks for singularity.
            Console.WriteLine("'A is singular' is {0:F6}.", c.IsSingular());
            // The LowerTriangularFactor returns the component 
            // of the decomposition.
            Console.WriteLine("L = {0:F6}", c.LowerTriangularFactor);

            // GetInverse() gives the matrix inverse, 
            // Determinant() the determinant:
            Console.WriteLine("Inv A = {0:F6}", c.GetInverse());
            Console.WriteLine("Det A = {0:F6}", c.GetDeterminant());

            // The Solve method solves a system of simultaneous linear
            // equations with one or more right-hand-sides:
            Matrix xC = c.Solve(bC);
            Console.WriteLine("x = {0:F6}", xC);
            
            //
            // Symmetric eigenvalue decomposition
            //

            // The eigenvalue decomposition of a symmetric matrix A
            // rewrites the matrix in the form A = XLXt with
            // X an orthogonal matrix and L a diagonal matrix.
            // The diagonal elements of L are the eigenvalues.
            // The columns of X are the eigenvectors.

            // Remember the column-major storage mode: each line of
            // components contains one COLUMN of the matrix.
            SymmetricMatrix aEig = new SymmetricMatrix(4, 
                new double[]
            {
                  0.5,  0.0,  2.3, -2.6,
                  0.0,  0.5, -1.4, -0.7,
                  2.3, -1.4,  0.5,  0.0,
                 -2.6, -0.7,  0.0,  0.5
            }, MatrixTriangleMode.Lower);

            // The constructor takes one or two parameters. The second
            // parameter is a bool value that indicates whether the
            // matrix should be overwritten with its decomposition.
            SymmetricEigenvalueDecomposition eig =
                new SymmetricEigenvalueDecomposition(aEig);
            Console.WriteLine("A = {0:F2}", aEig);

            // The Decompose method performs the decomposition. You don't need
            // to call it explicitly, as it is called automatically as needed.
            
            // The IsSingular method checks for singularity.
            Console.WriteLine("'A is singular' is {0:F6}.", eig.IsSingular());
            // The Eigenvalues property returns a vector containing
            // the eigenvalues:
            Console.WriteLine("L = {0:F6}", eig.Eigenvalues);
            // The Eigenvectors property returns a matrix whose columns 
            // contain the corresponding eigenvectors:
            Console.WriteLine("X = {0:F6}", eig.Eigenvectors);

            // GetInverse() gives the matrix inverse,
            // Determinant() the determinant:
            Console.WriteLine("Inv A = {0:F6}", eig.GetInverse());
            Console.WriteLine("Det A = {0:F6}", eig.GetDeterminant());

            Console.Write("Press Enter key to exit...");
            Console.ReadLine();
        }
    }
}
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