Extreme Optimization >
Mathematics Library for .NET >
User's Guide >
Current Page >
Operations on vectors
Extreme Optimization Mathematics Library for .NET
Operations on vectors
The Extreme Optimization Mathematics Library for .NET provides static
methods for all common operations on vectors. 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.
All operations can be performed on any combination of vector types and always
return an instance of type Vector. The actual return
value is of a derived type that is chosen for optimal performance.
Addition and subtraction
The Add and Subtract methods support vector 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 scalarf 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.Add(f, m, b) |
Adds the scalarf times the matrix m times the vector b to the vector a. |
a.Subtract(b) |
Subtracts 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. Operators and methods for addition and subtraction.
Scaling
The
Multiply method multiplies the components of a vector by a
real constant. The instance method with the same name scales the components of
the instance. The static (Shared) method scales the vector that was passed as
the second argument. The
Normalize instance method scales a vector so that its
Euclidean norm is equal to one.
Dot product
The DotProduct method calculates the dot product of two vectors. An
instance method returns the dot product of the instance and its argument. A
static method returns the dot product of its two arguments.
Overloaded operators
The addition, subtraction and negation operators have been overloaded for
Vector arguments. These return a new vector that is
the sum or the difference of its two arguments, or the negation of its single
argument. Equivalent static Add, Subtract and Negate
methods are available for use in languages that don't support operator
overloading.
The multiplication and division operators have also been overloaded. These
represent multiplication or division by a scalar. The
Multiply and
Divide methods are the static equivalent.
The operator overloads, and their static method equivalents are summarized in
the table below. In this table v, v1 and v2
represent vectors, while a represents a scalar (Double):
| Operator |
Static method equivalent |
v1 + v2 |
Vector.Add(a, b) |
v1 - v2 |
Vector.Subtract(a, b) |
-v |
Vector.Negate(a) |
a * v |
Vector.Multiply(a, v) |
v * a |
Vector.Multiply(a, v) |
v / a |
Vector.Divide(v, a) |
Table 2. Operators and methods for division and multiplication.
Other operations
Some operations do not have operator equivalents. All methods in this section are static (Shared in Visual Basic)
unless noted otherwise.
The
Pow method raises each component of a vector to the specified power.
This method has two overloads. Both take a Vector as their first parameter. The second parameter
is a real number or an integer. Special cases are optimized.
The
Max method has three overloads. The first returns a vector whose components are
the maximum of the corresponding components of two vectors.
The second and third overload returns a vector whose components are the maximum of the components
of a vector and a fixed number. The order of the parameters is not important.
The
Min method also has three overloads that perform the same function except the minimum
replaces the maximum.
In addition, the Vector class has a number of methods that apply elementary functions
to each of the components of a vector. These methods are listed in the table below:
| Method |
Description |
Vector.Abs(a) |
Absolute value. |
Vector.Inverse(a) |
Reciprocal or inverse (1/x). |
Vector.Sqrt(a) |
Square root. |
Vector.Exp(a) |
Exponential function. |
Vector.Log(a) |
Natural logarithm. |
Vector.Cos(a) |
Cosine. |
Vector.Sin(a) |
Sine. |
Vector.Tan(a) |
Tangent. |
Table 3. Elementary functions on vectors.
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