# A Simple Example - Inverting a Matrix

The following example illustrates how to use the functions in the STATISTICA library of matrix functions to perform some basic matrix operations: Namely, matrix inversion and matrix multiplication:

' The following statement will cause all arrays to be

' declared by default as 1-referenced, i.e., the

' first element in an array will be element 1, not 0.

' Note that the matrix and statistical functions

' will work fine with 0-referenced arrays; however,

' it can be confusing to write programs with 0-

' referenced arrays, because many of the matrix

' functions expect arguments based on 1-referenced

' arrays.

Option Base 1

'\$include: "*STB.svx"

Sub Main

' Set up two 4 x 4 matrices.

Dim a(4,4) As Double, ainv(4,4) As Double

Dim i As Long, j As Long

' Fill the matrix with random numbers

For i=1 To 4

For j=1 To 4

a(i,j)=Rnd(10)

Next j

Next i

' Display the original matrix.

MatrixDisplay (a, "Original Matrix A")

' Invert the matrix.

MatrixInverse(a,ainv)

' Display the inverted matrix.

MatrixDisplay(ainv, "A, Inverse")

' Multiply a*a-inverse

MatrixMultiply(a,ainv,a)

MatrixDisplay(a,"A * A-inverse")

'Note that you could also directly call the MatrixObject to access

'these functions.  For example: MatrixObject.MatrixDisplay(a,"A * A-inverse")

End Sub

A few things should be noted. First, be sure to include file STB.svx before making the calls to the matrix library, as shown above; otherwise, the program will not run. Second, the statement Option Base 1 causes all arrays to be created, by default, as 1- referenced arrays. In other words, the first element in arrays can be referenced as element number 1, the second as number 2, etc. If  the Option Base 1 statement is omitted , then by default, arrays are declared as 0-referenced, i.e., the first element in an array is element 0 (zero), the second element is element 1, etc. While it is not necessary that the arrays used in the calls to matrix functions (via the interfaces in STB.SVX) are 1-referenced, it can make writing programs using these functions a little less confusing.