NMath User's Guide

TOC | Previous | Next | Index

18.4 Arithmetic Operations on Matrices (.NET, C#, CSharp, VB, Visual Basic, F#)

NMath provides overloaded arithmetic operators for structured sparse matrices with their conventional meanings for those .NET languages that support them, and equivalent named methods for those that do not. Table 14 lists the equivalent operators and methods

.

Table 14 – Arithmetic operators

Operator

Equivalent Named Method

+

Add()

-

Subtract()

*

Multiply()

/

Divide()

Unary -

Negate()

All binary operators and equivalent named methods work either with two matrices, or with a matrix and a scalar.

NOTE—Matrices must have the same dimensions to be combined using the element-wise operators. Otherwise, a MismatchedSizeException is raised.

For example, this C# code uses the overloaded operators:

Code Example – C# matrix

var genMat = new DoubleMatrix( 8, 8, 0, 1 );
var A = new DoubleBandMatrix( genMat, 4, 5, 1, 2 );
var B = new DoubleBandMatrix( genMat, 4, 5, 1, 1 );
double s = 2.25;



DoubleBandMatrix result = A + s*B;

Note that although the banded matrices must have the same dimensions, they do not need to have the same bandwidth.

This Visual Basic code uses the equivalent named methods:

Code Example – VB matrix

Dim genMat As new DoubleMatrix( 8, 8, 0, 1 )
Dim A As new DoubleBandMatrix( genMat, 4, 5, 1, 2 );
Dim B As new DoubleBandMatrix( genMat, 4, 5, 1, 1 );
Dim s As Double = 2.25;



Dim result As DoubleBandMatrix = _
   DoubleBandMatrix.Add(A, DoubleBandMatrix.Multiply(s, B));

Top

Top