Extreme Optimization > QuickStart Samples > Matrix-Vector Operations QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Matrix-Vector Operations QuickStart Sample (VB.NET)

Illustrates operations on Matrix objects and combined operations on Vector and Matrix objects (Extreme.Mathematics.LinearAlgebra namespace) in Visual Basic .NET.

C# code Back to QuickStart Samples

' The GeneralMatrix and DoubleVector classes resides in the 
' Extreme.Mathematics.LinearAlgebra namespace.
Imports Extreme.Mathematics.LinearAlgebra

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates operations on GeneralMatrix objects and combined
    ' operations on DoubleVector and GeneralMatrix objects from the
    ' Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
    ' Mathematics Library for .NET.
    Module MatrixVectorOperations

        Sub Main()
            ' 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 GeneralMatrix
            ' objects, including constructing, copying and
            ' cloning vectors, see the BasicVectors QuickStart
            ' Sample.
            '
            ' Let's create some vectors to work with.
            Dim v1 As Vector = New GeneralVector(1, 2, 3, 4, 5)
            Dim v2 As Vector = New GeneralVector(1, -2, 3, -4, 5)
            Console.WriteLine("v1 = {0}", v1)
            Console.WriteLine("v2 = {0}", v2)
            ' These will hold results.
            Dim v As Vector

            ' Also, here are a couple of matrices.
            ' We start out with a 5x5 identity matrix:
            Dim m1 As GeneralMatrix = GeneralMatrix.GetIdentity(5)
            ' Now we use the GetDiagonal method and combine it
            ' with the SetValue method of the Vector class to
            ' set some of the off-diagonal elements:
            m1.GetDiagonal(1).SetValue(2)
            m1.GetDiagonal(2).SetValue(3)
            m1.GetDiagonal(-1).SetValue(4)
            Console.WriteLine("m1 = {0}", m1)
            ' We define our second matrix by hand:
            Dim m2 As GeneralMatrix = New GeneralMatrix(5, 5, New Double() _
                { _
                1, 2, 3, 4, 5, _
                1, 3, 5, 7, 9, _
                1, 4, 9, 16, 25, _
                1, 8, 27, 64, 125, _
                1, -1, 1, -1, 1 _
                })
            Console.WriteLine("m2 = {0}", m2)
            ' This one holds the results:
            Dim m As GeneralMatrix
            Console.WriteLine()

            '
            ' Matrix arithmetic
            '

            ' The GeneralMatrix class defines Shared methods for
            ' addition, subtraction, and multiplication of
            ' matrices. Overloaded operators are available 
            ' in languages that support them.
            ' Addition:
            Console.WriteLine("Matrix arithmetic:")
            m = Matrix.Add(m1, m2)
            Console.WriteLine("m1 + m2 = {0}", m)
            ' Subtraction:
            m = Matrix.Subtract(m1, m2)
            Console.WriteLine("m1 - m2 = {0}", m)
            ' Multiplication is the true matrix product:
            m = Matrix.Multiply(m1, m2)
            Console.WriteLine("m1 * m2 = {0}", m)
            Console.WriteLine()

            '
            ' Matrix-Vector products
            '

            ' The GeneralMatrix class defines Shared Add, Subtract, and
            ' Multiply methods for vectors and matrices. 
            ' Overloaded operators are available in languages
            ' that support them.
            Console.WriteLine("Matrix-vector products:")
            v = Matrix.Multiply(m1, v1)
            Console.WriteLine("m1 v1 = {0}", v)
            ' You can also multiply a vector by a matrix on the right.
            ' This is equivalent to multiplying on the left by the 
            ' transpose of the matrix:
            v = Matrix.Multiply(v1, m1)
            Console.WriteLine("v1 m1 = {0}", v)

            ' Now for some methods of the DoubleVector class that
            ' involve matrices:
            ' Add a product of a matrix and a vector:
            v.Add(m1, v1)
            Console.WriteLine("v + m1 v1 = {0}", v)
            ' Or add a scaled product:
            v.Add(-2, m1, v2)
            Console.WriteLine("v - 2 m1 v2 = {0}", v)
            ' You can also use static Subtract methods:
            v.Subtract(m1, v1)
            Console.WriteLine("v - m1 v1 = {0}", v)
            Console.WriteLine()

            '
            ' Matrix norms
            '
            Console.WriteLine("Matrix norms")
            ' Matrix norms are not as easily defined as
            ' vector norms. Three matrix norms are available.
            ' 1. The one-norm through the OneNorm property:
            Dim a As Double = m2.OneNorm()
            Console.WriteLine("OneNorm of m2 = {0}", a)
            ' 2. The infinity norm through the 
            '    InfinityNorm property:
            a = m2.InfinityNorm()
            Console.WriteLine("InfinityNorm of m2 = {0}", a)
            ' 3. The Frobenius norm is often used because it
            '    is easy to calculate.
            a = m2.FrobeniusNorm()
            Console.WriteLine("FrobeniusNorm of m2 = {0}", a)
            Console.WriteLine()

            ' The trace of a matrix is the sum of its diagonal
            ' elements. It is returned by the Trace property:
            a = m2.Trace
            Console.WriteLine("Trace(m2) = {0}", a)

            ' The Transpose method returns the transpose of a 
            ' matrix. This transposed matrix shares element storage
            ' with the original matrix. Use the CloneData method
            ' to give the transpose its own data storage.
            m = m2.Transpose()
            Console.WriteLine("Transpose(m2) = {0}", m)

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

    End Module

End Namespace
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