Vectors and Matrices
Defining Vectors
The Linear Algebra Module provides the objects you need to write code that uses numerical linear algebra. Vectors and matrices are at the heart of linear algebra, so we present these concepts early in the documentation.
The abstract concept of a vector is the most important abstraction of linear algebra. From a mathematical point of view, a vector space is simply a set whose elements satisfy a set of axioms. In a computational context, however, we can specify a vector in a much more concrete way. Because we are interested only in finite-dimensional vectors, each vector in a given space is uniquely identified by a sequence of numbers. Thus, in the Linear Algebra Module, a vector is simply a one-dimensional array of numbers.
The C++ classes used to represent vectors are the Essential Math Module classes RWMathVec<double>, RWMathVec<float>, and RWMathVec<DComplex>. They are based on a data-view architecture which is used heavily throughout the Linear Algebra Module and the Essential Math Module. If you haven't done so already, please read the chapter, “Vector, Matrix, and Array Classes,” in the Essential Math Module User’s Guide and the class description in the SourcePro API Reference Guide for a good understanding of the benefits and potential pitfalls of this approach.
In general, the vector classes are designed to be efficient and intuitive. For efficiency and expressiveness, reference counting semantics are used where possible; for example, subscripting and the copy constructor both return new views of the same data. Details are given in the Essential Math Module documentation, but here is a simple example, described line by line.
 
#include <iostream>
#include <rw/dvec.h> // 1
 
int main()
{
RWMathVec<double> x = "[ 2 3 8 9 3 2 1 ]"; // 2
RWMathVec<double> y(7,0); // 3
y("2:5") = 4; // 4
RWMathVec<double> z = x+y; // 5
std::cout << z; // 6
 
return 0;
}
//1 This line includes the header file for the class RWMathVec<double>, a vector of doubles. This describes to the compiler a new type, RWMathVec<double>, which you can use in your code.
//2 Here we define a vector x. The constructor builds a vector using the data in the argument string for initialization.
//3 This line defines a second vector, y, with seven elements initialized to 0. See the Essential Math Module Reference Guide for a complete guide to all available constructors.
//4 The left side of this expression shows how you can subscript a vector to build a new vector which references some of its elements. The new vector in this case consists of elements 2 through 5 of y. Since the new vector is a reference to the original, we can use it as an lvalue and thus change y. Subscripting in this way is powerful, flexible, and efficient. It is described in detail in the Essential Math Module documentation.
//5 Vectors can be used in expressions like scalars. Here we add two vectors, and use the result to initialize a third vector.
//6 On this line, we show that the stream input-output operators have been overloaded to work with vectors.