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

Extreme Optimization QuickStart Samples

Basic Matrices QuickStart Sample (C#)

Illustrates the basic use of the Matrix class (Extreme.Mathematics.LinearAlgebra namespace) for working with matrices in C#.

VB.NET code Back to QuickStart Samples

using System;

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

    /// <summary>
    /// Illustrates the use of the GeneralMatrix class in the 
    /// Extreme.Mathematics.LinearAlgebra namespace.
    /// </summary>
    class BasicMatrices
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // Constructing matrices
            //

            // Option #1: specify number of rows and columns.
            // The following constructs a matrix with 3 rows
            // and 5 columns:
            GeneralMatrix m1 = new GeneralMatrix(3, 5);
            Console.WriteLine("m1 = {0}", m1);
            // Option #2: specify a rank 2 double array. 
            // By default, elements are taken in column-major
            // order. Therefore, the following creates a matrix
            // with 3 rows and 4 columns:
            GeneralMatrix m2 = new GeneralMatrix(new double[,]
                {
                    {1, 2, 3}, 
                    {2, 3, 4},
                    {3, 4, 5},
                    {4, 5, 6}
                });
            Console.WriteLine("m2 = {0}", m2);
            GeneralMatrix m3 = m2;
            // Option #4: Specify component array, and number
            // of rows and columns. The elements are listed
            // in column-major order. The following matrix
            // is identical to m3:
            double [] components = new double[] 
            {
                1, 2, 3, 
                2, 3, 4, 
                3, 4, 5, 
                4, 5, 6
            };
            GeneralMatrix m4 = new GeneralMatrix(3, 4, components);
            Console.WriteLine("m4 = {0}", m4);
            // Option #5: same as above, but specify element
            // order. The following matrix is identical to m4:
            GeneralMatrix m5 = new GeneralMatrix(4, 3, components,
                MatrixElementOrder.RowMajor);
            Console.WriteLine("m5 = {0}", m5);
            // Option #6: same as #4, but specify whether to copy
            // the matrix components, or use the specified array
            // as internal storage.
            GeneralMatrix m6 = new GeneralMatrix(3, 4, components, false);
            // Option #7: same as #5, but specify whether to copy
            // the matrix components, or use the specified array
            // as internal storage.
            GeneralMatrix m7 = new GeneralMatrix(4, 3, components, 
                MatrixElementOrder.RowMajor, false);
            // In addition, you can also create an identity 
            // matrix by calling the static GetIdentity method.
            // The following constructs a 4x4 identity matrix:
            GeneralMatrix m8 = GeneralMatrix.GetIdentity(4);
            Console.WriteLine("m8 = {0}", m8);

            //
            // GeneralMatrix properties
            //
            
            // The RowCount and ColumnCount properties give the 
            // number of rows and columns, respectively:
            Console.WriteLine("m1.RowCount = {0}", m1.RowCount);
            Console.WriteLine("m1.ColumnCount = {0}", m1.ColumnCount);
            // The GetComponents method returns a one-dimensional
            // double array that contains the components of the 
            // vector. By default, elements are returned in
            // column major order. This is always a copy:
            components = m3.GetComponents();
            Console.WriteLine("Components:");
            Console.WriteLine("components[3] = {0}", components[3]);
            components[3] = 1;
            Console.WriteLine("m3[0,1] = {0}", m3[0,1]);
            // The GetComponents method is overloaded, so you can
            // choose whether you want the elements in row major 
            // or in column major order. The order parameter is
            // of type MatrixElementOrder:
            components = 
                m3.GetComponents(MatrixElementOrder.RowMajor);
            Console.WriteLine("In row major order:");
            Console.WriteLine("components[3] = {0}", components[3]);

            //
            // Accessing matrix elements
            //

            // The GeneralMatrix class defines an indexer property 
            // that takes zero-based row and column indices.
            Console.WriteLine("Assigning with private storage:");
            Console.WriteLine("m1[0,2] = {0}", m1[0,2]);
            // You can assign to this property:
            m1[0,2] = 7;
            Console.WriteLine("m1[0,2] = {0}", m1[0,2]);

            // The matrices m6 and m7 had the copy parameter in
            // the constructor set to false. As a result, they
            // share their component storage. Changing one vector
            // also changes the other:
            Console.WriteLine("Assigning with shared storage:");
            Console.WriteLine("m6[0,0] = {0}", m6[0,0]);
            m7[0,0] = 3;
            Console.WriteLine("m6[0,0] = {0}", m6[0,0]);

            //
            // Copying and cloning matrices
            //

            // A shallow copy of a matrix constructs a matrix
            // that shares the component storage with the original.
            // This is done using the ShallowCopy method. Note
            // that we have to cast the return value since it is
            // of type Matrix, the abstract base type of all
            // the matrix classes:
            Console.WriteLine("Shallow copy vs. clone:");
            GeneralMatrix m10 = (GeneralMatrix)m2.ShallowCopy();
            // The clone method creates a full copy.
            GeneralMatrix m11 = (GeneralMatrix)m2.Clone();
            // When we change m2, m10 changes, but m11 is left
            // unchanged:
            Console.WriteLine("m2[1,1] = {0}", m2[1,1]);
            m2[1,1] = -2;
            Console.WriteLine("m10[1,1] = {0}", m10[1,1]);
            Console.WriteLine("m11[1,1] = {0}", m11[1,1]);
            // We can give a matrix its own component storage
            // by calling the CloneData method:
            Console.WriteLine("CloneData:");
            m11.CloneData();
            // Now, changing the original v2 no longer changes v7:
            m2[1,1] = 4;
            Console.WriteLine("m11[1,1] = {0}", m11[1,1]);

            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