Extreme Optimization > QuickStart Samples > Triangular Matrices QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Triangular Matrices QuickStart Sample (C#)

Illustrates the use of the TriangularMatrix class (Extreme.Mathematics.LinearAlgebra namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

namespace Extreme.Mathematics.QuickStart.CSharp
{
    // The TriangularMatrix class resides in the Extreme.Mathematics.LinearAlgebra
    // namespace.
    using Extreme.Mathematics.LinearAlgebra;

    /// <summary>
    /// Illustrates the use of the TriangularMatrix class in the 
    /// Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    /// Mathematics Library for .NET.
    /// </summary>
    class TriangularMatrices
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Triangular matrices are matrices whose elements
            // above or below the diagonal are all zero. The
            // former is called lower triangular, the latter
            // lower triangular. In addition, triangular matrices
            // can have all 1's on the diagonal.

            //
            // Constructing triangular matrices
            //

            // Constructing triangular matrices is similar to
            // constructing general matrices. See the
            // BasicMatrices QuickStart samples for a more
            // complete discussion.
            //
            // All constructors take a MatrixTriangleMode
            // value as their first parameter. This indicates
            // whether an upper or lower triangular matrix 
            // should be created. The following creates a
            // 5x5 lower triangular matrix:
            TriangularMatrix t1 = new TriangularMatrix(
                MatrixTriangleMode.Lower, 5, 5);
            // You can also specify whether the diagonal 
            // consists of all 1's using a unitDiagonal parameter:
            TriangularMatrix t2 = new TriangularMatrix(
                MatrixTriangleMode.Lower, 
                5, 5, MatrixDiagonalMode.UnitDiagonal);
            // Triangular matrices access and modify only the
            // elements that are non-zero. If the diagonal
            // mode is UnitDiagonal, the diagonal elements
            // are not used, since they are all equal to 1.
            double[] components = new double[]
                {
                    11, 12, 13, 14, 15,
                    21, 22, 23, 24, 25,
                    31, 32, 33, 34, 35,
                    41, 42, 43, 44, 45,
                    51, 52, 53, 54, 55
                };
            // The following creates a matrix using the
            // upper triangular part of the above.
            TriangularMatrix t3 = new TriangularMatrix(
                MatrixTriangleMode.Upper, 5, 5, 
                components, MatrixElementOrder.RowMajor);
            Console.WriteLine("t3 = {0}", t3);
            // Same as above, but unit diagonal:
            TriangularMatrix t4 = new TriangularMatrix(
                MatrixTriangleMode.Upper, 5, 5, 
                components, MatrixDiagonalMode.UnitDiagonal, 
                MatrixElementOrder.RowMajor, true);
            Console.WriteLine("t4 = {0}", t4);

            //
            // Extracting triangular matrices
            //

            // You may want to use part of a dense matrix
            // as a triangular matrix. The static 
            // ExtractUpperTriangle and ExtractLowerTriangle
            // methods perform this task.
            GeneralMatrix m = new GeneralMatrix(5, 5, components);
            Console.WriteLine("m = {0}", m);
            // Both methods are overloaded. The simplest
            // returns a triangular matrix of the same dimension:
            TriangularMatrix t5 = 
                TriangularMatrix.ExtractLowerTriangle(m);
            Console.WriteLine("t5 = {0}", t5);
            // You can also specify if the matrix is unit diagonal:
            TriangularMatrix t6 =
                TriangularMatrix.ExtractUpperTriangle(
                    m, MatrixDiagonalMode.UnitDiagonal);
            Console.WriteLine("t6 = {0}", t6);
            // Or the dimensions of the matrix if they don't 
            // match the original:
            TriangularMatrix t7 =
                TriangularMatrix.ExtractUpperTriangle(
                m, 3, 3, MatrixDiagonalMode.UnitDiagonal);
            Console.WriteLine("t7 = {0}", t7);
            Console.WriteLine();

            //
            // TriangularMatrix properties
            //

            // The IsLowerTriangular and IsUpperTriangular return
            // a boolean value:
            Console.WriteLine("t4 is lower triangular? - {0}", 
                t4.IsLowerTriangular);
            Console.WriteLine("t4 is upper triangular? - {0}", 
                t4.IsUpperTriangular);
            // The IsUnitDiagonal property indicates whether the
            // matrix has all 1's on its diagonal:
            Console.WriteLine("t3 is unit diagonal? - {0}", 
                t3.IsUnitDiagonal);
            Console.WriteLine("t4 is unit diagonal? - {0}", 
                t4.IsUnitDiagonal);
            Console.WriteLine();
            // You can get and set matrix elements:
            t3[1, 3] = 55;
            Console.WriteLine("t3[1, 3] = {0}", t3[1, 3]);
            // But trying to set an element that is zero or
            // is on the diagonal for a unit diagonal matrix
            // causes an exception to be thrown:
            try
            {
                t3[3, 1] = 100;
            }
            catch(ComponentReadOnlyException e)
            {
                Console.WriteLine("Error accessing element: {0}",
                    e.Message);
            }

            //
            // Rows and columns
            //

            // The GetRow and GetColumn methods are
            // available.
            Vector row = t3.GetRow(1);
            Console.WriteLine("row 1 of t3 = {0}", row);
            Vector column = t4.GetColumn(1, 1, 3);
            Console.WriteLine("2nd column of t4 from row 1 to 3 = {0}", column);

            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