- Introduction
- An Example
- Education and Libraries
- References
An Example
Here is an example illustrating the power of libraries:
void Decompose(double delt, SymTensor& V, Tensor& R, const Tensor& L) { Symtensor D = Sym(L) ; AntiTensor W = Anti(L) ; Vector z = Dual(V*D) ; Vector omega = Dual(W) - 2.0*Inverse(V-Tr(V)*One)*z; AntiTensor Omega = 0.5*Dual(omega) ; R = Inverse(One-0.5*delt*Omega) * (One+0.5*delt*Omega)*R; V += delt*Sym(L*V-V*Omega) ; }
According to [Budge,1992], "This code is transparent and...a physicist familiar with the polar decomposition algorithm can make immediate sense of this code fragment without the need for additional documentation."
I'm not a physicist, so that code is not obvious to me, but it is clear how that code is trivially translated from what you find in a physics textbook. This example is taken from a pioneering library from Sandia Labs. However, such libraries are now common in various branches of high-energy physics, and general vector and matrix libraries supporting them are widely used and available.
I chose physics as my example partly because it's a field where performance is essential. If a program in this field is significantly slower than a conventional Fortran program, it will be discarded. In this area, elegance of programs traditionally takes the backseat compared to performance. The reason is simple: This is a field in which results are often limited by available computing resources. C++ and modern programming techniques have met that challenge. Using generic programming techniques, C++ vector and matrix libraries have equaled and exceeded the performance of classical Fortran code (for example, POOMA [POOMA] MTL [MTL], and ROOT [ROOT]). One key to this exceptional performance is the elimination of temporaries through the use of small function objects [Stroustrup,2000§22.4.7]. Another key is (other) function objects representing critical implementation policies. Basically, the average programmer cannot write code that equals the modern libraries in numerical performance. Good programmers often know better than to even try.
Different fields have different requirements. This leads to the use of different programming styles being key to the design and use of different libraries. Numeric libraries tend to rely on generic programming, emphasizing conventional notation and efficiency. By contrast, libraries focusing on graphics or user interaction tend to rely heavily on object-oriented techniques involving class hierarchies. When number crunching meets data management and graphics, combinations of programming techniques occur naturally.
The time taken to write a program is at best roughly proportional to the number of lines written, and so is the number of errors in that code. It follows that a good way of writing correct programs is to write short programs. In other words, we need good libraries to allow us to write correct code that performs well. This in turn means that we need libraries to get our programs finished in a reasonable time. In many fields, such C++ libraries exist. The standard library is an excellent way to start getting away from the messier and more error-prone levels of programming [Stroustrup,1999] [Stroustrup,2000]. However, there is much more to C++ than the standard librarythat library is just an essential beginning.