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

Extreme Optimization QuickStart Samples

Symmetric Matrices QuickStart Sample (C#)

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

VB.NET code Back to QuickStart Samples

using System;

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

    /// <summary>
    /// Illustrates the use of the SymmetricMatrix class in the 
    /// Extreme.Mathematics.LinearAlgebra namespace of the Extreme
    /// Optimization Mathematics Library for .NET.
    /// </summary>
    class SymmetricMatrices
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Symmetric matrices are matrices whose elements
            // are symmetrical around the main diagonal.
            // Symmetric matrices are always square, and are
            // equal to their own transpose.

            //
            // Constructing symmetric matrices
            //

            // Constructing symmetric matrices is similar to
            // constructing general matrices. See the
            // BasicMatrices QuickStart samples for a more
            // complete discussion.
            
            // Symmetric matrices are always square. You don't
            // have to specify both the number of rows and the
            // number of columns.
            //
            // The following creates a 5x5 symmetric matrix:
            SymmetricMatrix s1 = new SymmetricMatrix(5);
            // Symmetric matrices access and modify only the
            // elements on and either above or below the 
            // main diagonal. When initializing a
            // symmetric matrix in a constructor, you must
            // specify a triangleMode parameter that specifies
            // whether to use the upper or lower triangle:
            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
                };
            SymmetricMatrix s2 = new SymmetricMatrix(5, 
                components, MatrixTriangleMode.Upper);
            Console.WriteLine("s2 = {0:F0}", s2);

            // You can also create a symmetric matrix by
            // multiplying any matrix by its transpose:
            GeneralMatrix m = new GeneralMatrix(3, 4, new double[]
                {
                    1, 2, 3, 
                    2, 3, 4,
                    3, 4, 5,
                    4, 5, 7
                });
            Console.WriteLine("m = {0:F0}", m);
            // This calculates transpose(m) times m:
            SymmetricMatrix s3 = SymmetricMatrix.FromOuterProduct(m);
            Console.WriteLine("s3 = {0:F0}", s3);
            // An optional 'side' parameter lets you specify
            // whether the left or right operand of the 
            // multiplication is the transposed matrix.
            // This calculates m times transpose(m):
            SymmetricMatrix s4 = 
                SymmetricMatrix.FromOuterProduct(m,
                    MatrixOperationSide.Right);
            Console.WriteLine("s4 = {0:F0}", s4);

            //
            // SymmetricMatrix methods
            //

            // The GetEigenvalues method returns a vector
            // containing the eigenvalues.
            Vector l = s4.GetEigenvalues();
            Console.WriteLine("Eigenvalues: {0:F4}", l);

            // The ApplyMatrixFunction calculates a function 
            // of the entire matrix. For example, to calculate
            // the 'sine' of a matrix:
            SymmetricMatrix sinS =
                s4.ApplyMatrixFunction(new RealFunction(Math.Sin));
            Console.WriteLine("sin(s4): {0:F4}", sinS);

            // Symmetric matrices don't have any specific
            // properties.

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

            //
            // Row and column views
            //

            // The GetRow and GetColumn methods are
            // available.
            Vector row = s2.GetRow(1);
            Console.WriteLine("row 1 of s2 = {0:F0}", row);
            Vector column = s2.GetColumn(2, 3, 4);
            Console.WriteLine("column 3 of s2 from row 4 to ");
            Console.WriteLine("  row 5 = {0:F0}", 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