     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:  Defining the variables:
Matrix A;
ColumnVector b;

Calculations:
Take inverse, : Matrix Ainv = A.i();
Matrix multiply, : Matrix C = A * b.t();
Solve system, : 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: On C++ for non-C++ Up: A note on C++ Previous: Source availability   Contents   Index
Bernt Arne Odegaard
1999-09-09