Essential Math Module User’s Guide : Chapter 3 Vector, Matrix, and Array Classes : Some Simple Examples
Some Simple Examples
Vectors, matrices, and arrays are represented by the following Essential Math Module classes:
RWMathVec<T>, the templatized vector class.
RWGenMat<T>, the templatized general matrix class.
RWMathArray<T>, the templatized arbitrary dimension array class.
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
}
//1 This first #include statement declares the class RWMathVec<T>, a vector of elements of type T. You can think of it as describing to the compiler how the class designer (Rogue Wave, in this case) has extended the language to include a new type, RWMathVec<T>.
//2 The second #include statement allows the use of streams for input/output.
//3 The third line defines an RWMathVec<int> with the name iv using a constructor with arguments (vector length, start value, increment). The result is a a vector of 10 integers initialized to [0, 1, 2, ... 9]. See the Class Reference for a complete guide to all of the constructors.
//4 The fourth line defines an RWMathVec<double> with the name dv, also with 10 elements, but initialized using a character string.
//5 The fifth line uses the conversion object RWConvertMathVec to convert the integer vector iv to an RWMathVec<double>.
//6 The sixth line prints the sum of the two vectors. Input and output of vectors using streams is exactly analogous to input and output of basic types like double.
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
}
//1 This statement declares the class RWGenMat<T>, a matrix of elements of type T.
//2 Here we define A to be a matrix with 2 rows and 3 columns of floating point numbers, initialized using a character string. The format of the string is the same as that expected by the stream extraction operator, operator>>(istream& RWGenMat<float>).
The implementation of this constructor is a nice example of the power of abstraction provided by the iostream stream library. Rather than parse the string directly in the constructor, we construct an istream using the contents of the string as a source, then use the stream extraction operator >> to extract the matrix. Thus, the same code is used to translate an ASCII representation to a matrix, whether the input is from the keyboard (using the istream cin), from a file (using an ifstream), or from a C++ character string. Thanks to the iostream library, it's easy to a achieve this level of reuse.
//3 In this line, the middle entry in the bottom row of the matrix A is set to 0. The expression A(1,1) is a subscripting expression; it returns a reference to entry 1,1 in the matrix. Subscripting is explained in more detail in “Subscripting” and “Advanced Subscripting”.
//4 In the last line, the matrix is printed out.
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
}
//1 This statement declares the class RWMathArray<T> an array of type T.
//2 Here we define A to be an array of integers using the default constructor. This constructor takes no arguments and constructs a null (0- dimensional) array. Why use it? One reason is that such a constructor is required to declare C-style arrays of RWMathArray<T>s:
RWMathArray<double> aa[5];
Default constructors exist for all the vector, matrix, and array classes.
//3 The array is resized to be a three-dimensional array of size 3x3x3.
//4 Each element in the array is set to 3.
//5 In this line, the middle entry of the array A is set to 0.
//6 In the last line, the array is printed out.