Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Math Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

3.2 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.

3.2.1 Vectors

The first example shows how to declare and use RWMathVec<T>, the Essential Math Module vector class, to add two vectors.

//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.

3.2.2 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:

//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 Section 3.9 and Section 3.9.2.

//4

In the last line, the matrix is printed out.

3.2.3 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:

//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.



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.