Extreme Optimization > Mathematics Library for .NET > QuickStart Samples > AccessingMatrixComponents QuickStart Sample (VB.NET)

Extreme Optimization Mathematics Library for .NET

AccessingMatrixComponents QuickStart Sample (VB.NET)

Illustrates different ways of iterating through the rows and columns of a matrix using classes in the Extreme.Mathematics.LinearAlgebra namespace  in Visual Basic .NET.

C# code Back to QuickStart Samples

' Matrix and Vector classes reside in the
' Extreme.Mathematics.LinearAlgebra namespace.
Imports Extreme.Mathematics.LinearAlgebra

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates accessing matrix components and iterating 
    ' through the rows and columns of a matrix. Matrix classes
    ' reside in the Extreme.Mathematics.LinearAlgebra namespace 
    ' of the Extreme Optimization Mathematics Library for .NET.
    Module AccessingMatrixComponents

        Sub Main()
            ' We'll work with this matrix:
            Dim m As Matrix = New GeneralMatrix(2, 3, New Double() {1, 2, 3, 4, 5, 6})

            '
            ' Individual components
            '

            ' The Matrix class has an indexer property that takes two arguments:
            ' the row and column index. Both are zero based.
            Console.WriteLine("m(1,1) = {0}", m(1, 1))

            '
            ' Rows and columns
            '

            ' Indexed range access

            ' The indexer property is overloaded to allow for direct indexed access 
            ' to complete or partial rows or columns.

            Dim row1 As Vector = m(0, New Range(1, 2))
            ' This prints "(3, 5)":
            Console.WriteLine("row1 = {0}", row1)

            ' The special range Range.All lets you access an entire row
            ' or column without having to specify any details about the range.
            Dim row2 As Vector = m(1, Range.All)
            ' This prints "(2, 4, 6)":
            Console.WriteLine("row1 = {0}", row2)
            Dim column1 As Vector = m(Range.All, 0)
            ' This prints "(1, 2)":
            Console.WriteLine("column1 = {0}", column1)

            ' We can assign to rows and columns, too:
            m(Range.All, 0) = row2
            ' This prints "((3, 3, 5) (5, 4, 6)"
            Console.WriteLine("m = {0}", m)

            ' GetRow and GetColumn provide an alternate mechanism 
            ' for achieving the same result.

            ' Passing just one parameter retrieves the specified row or column:
            row1 = m.GetRow(1)
            ' This prints "(2, 4, 6)":
            Console.WriteLine("row1 = {0}", row1)
            column1 = m.GetColumn(0)
            ' This prints "(1, 2)":
            Console.WriteLine("column1 = {0}", column1)

            ' You can also pass a start and end index:
            row2 = m.GetRow(0, 1, 2)
            ' This prints "(3, 5)":
            Console.WriteLine("row2 = {0}", row2)

            ' We can assign to rows and columns, too, using CopyTo:
            row2.CopyTo(m.GetColumn(0))
            ' This prints "((3, 3, 5) (5, 4, 6)"
            Console.WriteLine("m = {0}", m)

            ' Enumeration

            ' The Rows and Columns methods allow you to enumerate over 
            ' the rows and columns of a matrix.

            ' For example: this calculates the sum of the absolute values
            ' of the components of the matrix m:
            Dim sum As Double = 0
            Dim column As Vector
            For Each column In m.Columns
                sum += column.OneNorm()
            Next

            '
            ' Accessing diagonals
            '

            ' Diagonals are retrieved using the GetDiagonal method:
            Dim mainDiagonal As Vector = m.GetDiagonal()
            ' An optional parameter specifies which diagonal:
            '   n < 0 means subdiagonal
            '   n > 0 means nth superdiagonal:
            Dim superDiagonal As Vector = m.GetDiagonal(1)

            '
            ' Accessing submatrices
            '

            ' Indexed range access

            ' A fourth overload of the indexer property lets you 
            ' extract a part of a matrix. Both parameters are Range 
            ' structures:
            Dim a As Matrix = New GeneralMatrix(10, 10)
            ' Extract the 2nd to the 5th row of m:
            Dim a1 As Matrix = a(New Range(1, 4), Range.All)
            ' Extract the odd columns:
            Dim a2 As Matrix = a(Range.All, New Range(1, 10, 2))
            ' Extract the 4x4 leading submatrix of m:
            Dim a3 As Matrix = a(New Range(0, 3), New Range(0, 3))

            ' You can also assign to submatrices:
            Dim identity5 As Matrix = GeneralMatrix.GetIdentity(5)
            a(New Range(0, 4), New Range(5, 9)) = identity5
            a(New Range(5, 9), New Range(0, 4)) = identity5

            ' The same results can be achieved with the GetSubmatrix method.

            ' Extract the 2nd to the 5th row of m. 
            ' Start and end columns are supplied manually.
            Dim a4 As Matrix = a.GetSubmatrix(1, 4, 0, 9)
            ' Extract the odd columns:
            ' Here we need to supply the transpose parameter.
            Dim a5 As Matrix = a.GetSubmatrix(0, 9, 1, 1, 10, 2, _
             TransposeOperation.None)
            ' Extract the 4x4 leading submatrix of m.
            ' And let's get its transpose, just because we can.
            ' We need to specify the row and column stride:
            Dim a6 As Matrix = a.GetSubmatrix(0, 3, 1, 0, 3, 1, _
                TransposeOperation.Transpose)

            ' You can still assign to submatrices, using the
            ' CopyTo method:
            identity5.CopyTo(a.GetSubmatrix(0, 4, 5, 9))
            identity5.CopyTo(a.GetSubmatrix(5, 9, 0, 4))
        End Sub

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
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