Extreme Optimization >
Mathematics Library for .NET >
User's Guide >
Current Page >
Operations on Matrices
Extreme Optimization Mathematics Library for .NET
Operations On Matrices
The Extreme Optimization Mathematics Library for .NET provides static
methods for all common operations on matrices and combined matrix-vector
operations. Where applicable, overloaded operator methods are supplied for
languages that support them. In addition, a series of instance methods have
been defined. These provide more efficient use of resources. All instance
methods return a reference to the (modified) instance. This enables their use
in compound expressions.
Addition and subtraction
The Add and Subtract methods support matrix addition
and subtraction. These methods have several overloads, corresponding to the
most common composite operations. Performing several operations in one method
call is more efficient, because a lot of overhead and most temporary storage
can be eliminated. The following table lists the instance methods for addition
and subtraction:
| Method |
Description |
a.Add(b) |
Adds the vector b to the vector a. |
a.Add(f, b) |
Adds the scalar f times the vector b to the vector a. |
a.Add(m, b) |
Adds the matrix m times the vector b to the vector a. |
a.Subtract(b) |
Subtracts the vector b from the vector a. |
a.Subtract(f, b) |
Subtracts the scalar f times the vector b
from the vector a. |
a.Subtract(m, b) |
Subtracts the matrix m times the vector b
from the vector a. |
Table 1. Matrix methods for addition and subtraction.
Also, two static Add and Subtract methods are
available that return a new vector that is the sum or the difference of its two
arguments. The overloaded addition (+) and subtraction (-) operators correspond
to these two static methods. In languages that don't support operator
overloading, you must use these methods.
Scaling
The
Multiply method multiplies the components of a matrix by a
real constant. The instance method with the same name scales the components of
the instance. The static (Shared) method scales the matrix that was passed as
the second argument.
The static
Multiply method has equivalent operator overloads of
the * and / operators. The table below summarizes the operator overloads and
the static method equivalent for a Double a, and a Vector
v.
| Operator |
Static method equivalent |
a * x |
Matrix.Multiply(a, x) |
x * a |
Matrix.Multiply(a, x) |
x / a |
Matrix.Divide(1/a, x) |
Table 2. Matrix multiplication and division operators and their method equivalents.
The
ScaleRows and
ScaleColumns methods each take a Vector as their only argument.
They multiply each row (column) of the matrix by the corresponding component of the vector.
This is equivalent to multiplying a matrix on the left (right) by a diagonal matrix with diagonal specified by the vector.
The
UnscaleRows and
UnscaleColumns methods perform the inverse operation. They divide each row or column by the corresponding
component of the vector.
A note on compound assignment operators
The C# language does not provide for explicit overloading of compound assignment
operators. A compound operator is translated into the operator followed by an
assignment. This means that an expression like
a += b;
is exactly equivalent to
a = a + b;
This is important when working with reference types such as vectors. To perform
the above addition, a new vector instance must be created to hold the sum of a
and b. This new instance is then assigned to a,
releasing the original value. This causes an unnecessary memory allocation. For
large vectors, this may lead to excessive garbage collection, degrading overall
performance.
References
G. H. Golub, C. F. Van Loan, Matrix Computations (3rd Ed), Johns
Hopkins University Press, 1996.
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation