Home
Products
Support
Blog
Resources
Company
NMath Matrix User's Guide
TOC |  Previous |  Next |  Index

3.4 Arithmetic Operations on Matrices

NMath Matrix 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 4 lists the equivalent operators and methods

.

Table 4 - 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:

DoubleMatrix genMat = new DoubleMatrix( 8, 8, 0, 1 );
DoubleBandMatrix A = new DoubleBandMatrix( genMat, 4, 5, 1, 2 );
DoubleBandMatrix 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.NET code uses the equivalent named methods:

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))

TOC |  Previous |  Next |  Index

Copyright © 2008 CenterSpace Software, LLC. All rights reserved.
All trademarks and registered trademarks mentioned on this web site are the property of their respective owners.
Contact Webmaster