Some Simple Examples
Vectors, matrices, and arrays are represented by the following Essential Math Module classes:
Let's start with a simple example of how each class works, and analyze each example line by line.
Vectors
The first example shows how to declare and use
RWMathVec<T>, the Essential Math Module vector class, to add two vectors.
#include <rw/math/mathvec.h> // 1
#include <iostream.h> // 2
int main()
{
RWMathVec<int> iv(10, 0, 1); // 3
RWMathVec<double> dv = "[4 5 8 9 7 5 3 4 3 0]"; // 4
RWMathVec<double> div = RWConvertMathVec<int,double>(iv); // 5
cout << div + dv; // 6
}
Matrix Example
As the next example shows, using matrices is no more complicated than using vectors. Here we use class
RWGenMat<T> to define and print out the matrix
A:
#include <rw/math/genmat.h> // 1
#include <iostream.h>
int main()
{
RWGenMat<float> A("2x3[1 2 3 4 5 6]"); // 2
A(1,1) = 0; // 3
cout << A; // 4
}
An Array Example
Finally, here is an example showing how to use the array class to define and print a three-dimensional array of integers:
#include <rw/math/mtharray.h> // 1
#include <iostream.h>
int main()
{
RWMathArray<int> A; // 2
A.resize(3,3,3); // 3
A = 3; // 4
A(1,1,1) = 0; // 5
cout << A; // 6
}