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

Extreme Optimization QuickStart Samples

Band Matrices QuickStart Sample (C#)

Illustrates working with general and symmetric band matrices using the BandMatrix class in the Extreme.Mathematics.LinearAlgebra namespace in C#.

VB.NET code Back to QuickStart Samples

using System;

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

    /// <summary>
    /// Illustrates the use of the BandMatrix class in the 
    /// Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    /// Numerical Libraries for .NET.
    /// </summary>
    class BandMatrices
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Band matrices are matrices whose elements
            // are nonzero only in a diagonal band around
            // the main diagonal.
            //
            // General band matrices, upper and lower band
            // matrices, and symmetric band matrices are all
            // represented by a single class: BandMatrix.

            //
            // Constructing band matrices
            //

            // Constructing band matrices is similar to
            // constructing general matrices. See the
            // BasicMatrices QuickStart samples for a more
            // complete discussion.

            // The following creates a 7x5 band matrix with
            // upper bandwidth 1 and lower bandwidth 2:
            BandMatrix b1 = new BandMatrix(7, 5, 2, 1);

            // Once the upper and lower bandwidth are set, 
            // it cannot be changed. Elements that are outside
            // the band cannot be set.

            // A second constructor lets you create upper
            // or lower band matrices. The following constructs
            // an 11x11 upper band matrix with unit diagonal
            // and three non-zero upper diagonals.
            BandMatrix b2 = new BandMatrix(MatrixTriangleMode.Upper,
                11, 3, MatrixDiagonalMode.UnitDiagonal);

            // To create a symmetric band matrix, you only need
            // the size and the bandwith. The following creates
            // a 6x6 symmetric tri-diagonal matrix:
            BandMatrix b3 = new BandMatrix(7, 1);

            // We can assign values to the components by using
            // the GetDiagonal method.
            b3.GetDiagonal(0).SetValue(2);
            b3.GetDiagonal(1).SetValue(-1);

            // Extracting band matrices

            // Another way to construct a band matrix is by
            // extracting them from an existing matrix.
            GeneralMatrix m = new GeneralMatrix(3, 4, new double[]
                {
                    1, 2, 3, 
                    2, 3, 4,
                    3, 4, 5,
                    4, 5, 7
                });
            // To get the lower band part of m with bandwidth 2:
            BandMatrix b4 = BandMatrix.Extract(m, 2, 0);

            //
            // BandMatrix properties
            //

            // A number of properties are available to determine 
            // whether a BandMatrix has a special structure:
            Console.WriteLine("b2 is upper? {0}", b2.IsUpperTriangular);
            Console.WriteLine("b2 is lower? {0}", b2.IsUpperTriangular);
            Console.WriteLine("b2 is unit diagonal? {0}", b2.IsUnitDiagonal);
            Console.WriteLine("b2 is symmetrical? {0}", b2.IsSymmetric);

            //
            // BandMatrix methods
            //

            // You can get and set matrix elements:
            b3[2, 3] = 55;
            Console.WriteLine("b3[2, 3] = {0:F0}", b3[2, 3]);
            // And the change will automatically be reflected
            // in the symmetric element:
            Console.WriteLine("b3[3, 2] = {0:F0}", b3[3, 2]);

            //
            // Row and column views
            //

            // The GetRow and GetColumn methods are
            // available.
            Vector row = b2.GetRow(1);
            Console.WriteLine("row 1 of b2 = {0:F0}", row);
            Vector column = b2.GetColumn(2, 3, 4);
            Console.WriteLine("column 3 of b2 from row 4 to ");
            Console.WriteLine("  row 5 = {0:F0}", column);

            //
            // Band matrix decompositions
            //

            // Specialized classes exist to represent the
            // LU decomposition of a general band matrix
            // and the Cholesky decomposition of a 
            // symmetric band matrix.

            // Because of pivoting, the upper band matrix of
            // the LU decomposition has larger bandwidth.
            // You need to allocate extra space to be able to
            // overwrite a matrix with its LU decomposition.

            // The following creates a 7x5 band matrix with
            // upper bandwidth 1 and lower bandwidth 2.
            BandMatrix b5 = new BandMatrix(7, 7, 2, 1, true);
            b5.GetDiagonal(0).SetValue(2.0);
            b5.GetDiagonal(-2).SetValue(-1.0);
            b5.GetDiagonal(1).SetValue(-1.0);

            // Other than that, the API is the same as
            // other decomposition classes.
            BandLUDecomposition blu = new BandLUDecomposition(b5, true);
            Vector solution = blu.Solve(new ConstantVector(b5.ColumnCount, 1.0));
            Console.WriteLine("  solution of b5*x = ones: {0:F4}", solution);

            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