next up previous contents index
Next: On C++ for non-C++ Up: A note on C++ Previous: Source availability   Contents   Index


Libraries.

One of the major advances of C++ relative to other programming languages is its ability to use libraries to extend the language. While I have tried to avoid use of libraries, in a few places I have used a library that performs linear algebra. This is mainly for clarity, the algorithms can be cleanly and simply expressed in vector notation, and implementing them directly would involve long, unnecessary loops, which has the added disadvantage of most likely introducing trivial mistakes. Using a well-designed linear algebra library will also in most cases be much more efficient than writing code for equation solving yourself.

The linear algebra library that is used in the algorithms is called newmat, which is a public domain library. The current version is found as a file newmat09.zip or newmat09.tar.gz on major ftp archives. Search using archie. The usage in the algorithms should be pretty clear from the context, and other linear algebra C++ libraries will have similar notation, but here are some examples:

\begin{displaymath}A = \left[
\begin{array}[c]{cc}
a_{11} & a_{12} \\
a_{21} & a_{22} \\
\end{array}\right]
\end{displaymath}


\begin{displaymath}\mathbf{b} = \left[
\begin{array}[c]{cc}
b_{11}\\
b_{21}
\end{array}\right]
\end{displaymath}

Defining the variables:
Matrix A;
ColumnVector b;

Calculations:
Take inverse, $A^{-1}$: Matrix Ainv = A.i();
Matrix multiply, $\bf C=A\times b^\prime$: Matrix C = A * b.t();
Solve system, $\bf Ax=b$: Matrix x = A.i()*b;

For linear algebra, it is possible to take advantage of particular forms of matrices, such as banded matrices, diagonal matrices etc. C++ is particularly useful here, a good linear algebra library will choose efficient algorithms depending on the form of the matrices, without the need for particular choice of subroutines by the programmer, the choice of algorithm is purely made from context. For an example of this, look at the algorithm for option pricing using implicit finite differences.


next up previous contents index
Next: On C++ for non-C++ Up: A note on C++ Previous: Source availability   Contents   Index
Bernt Arne Odegaard
1999-09-09