Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

3.2 Some Simple Examples

Vectors, matrices, and arrays are represented by the following Math.h++ 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 Math.h++ vector class, to add two vectors.

//1This 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>.
//2The second #include statement allows the use of streams for input/output.
//3The 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.
//4The fourth line defines an RWMathVec<double> with the name dv, also with 10 elements, but initialized using a character string.
//5The fifth line uses the conversion object RWconvertMathVec to convert the integer vector iv to an RWMathVec <double>.
//6The 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:

//1This statement declares the class RWGenMat<T>, a matrix of elements of type T.
//2Here 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.

//3In 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.3.
//4In 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:

//1This statement declares the class RWMathArray<T> an array of type T.
//2Here 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.
//3The array is resized to be a three-dimensional array of size 3x3x3.
//4Each element in the array is set to 3.
//5In this line, the middle entry of the array A is set to 0.
//6In the last line, the array is printed out.

Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.