Chapter 26: Numerics

Chapter 26 deals with building block abstractions to aid in numerical computing:

All of the Standard C math functions are of course included in C++, and overloaded versions for long, float, and long double have been added for all of them.


Contents


Complex Number Processing

Using complex<> becomes even more comple- er, sorry, complicated, with the not-quite-gratuitously-incompatible addition of complex types to the C language. David Tribble has compiled a list of C++89 and C99 conflict points; his description of C's new type versus those of C++ and how to get them playing together nicely is here.

complex<> is intended to be instantiated with a floating-point type. As long as you meet that and some other basic requirements, then the resulting instantiation has all of the usual math operators defined, as well as definitions of op<< and op>> that work with iostreams: op<< prints (u,v) and op>> can read u, (u), and (u,v).

Return to top of page or to the FAQ.


Array Processing

One of the major reasons why FORTRAN can chew through numbers so well is that it is defined to be free of pointer aliasing, an assumption that C89 is not allowed to make, and neither is C++. C99 adds a new keyword, restrict, to apply to individual pointers. The C++ solution is contained in the library rather than the language (although many vendors can be expected to add this to their compilers as an extension).

That library solution is a set of two classes, five template classes, and "a whole bunch" of functions. The classes are required to be free of pointer aliasing, so compilers can optimize the daylights out of them the same way that they have been for FORTRAN. They are collectively called valarray, although strictly speaking this is only one of the five template classes, and they are designed to be familiar to people who have worked with the BLAS libraries before.

Some more stuff should go here once somebody has time to write it.

Return to top of page or to the FAQ.


Numerical Functions

There are four generalized functions in the <numeric> header that follow the same conventions as those in <algorithm>. Each of them is overloaded: one signature for common default operations, and a second for fully general operations. Their names are self-explanatory to anyone who works with numerics on a regular basis:

Here is a simple example of the two forms of accumulate.

   int   ar[50];
   int   someval = somefunction();

   // ...initialize members of ar to something...

   int  sum       = std::accumulate(ar,ar+50,0);
   int  sum_stuff = std::accumulate(ar,ar+50,someval);
   int  product   = std::accumulate(ar,ar+50,1,std::multiplies<int>());
      
The first call adds all the members of the array, using zero as an initial value for sum. The second does the same, but uses someval as the starting value (thus, sum_stuff == sum + someval). The final call uses the second of the two signatures, and multiplies all the members of the array; here we must obviously use 1 as a starting value instead of 0.

The other three functions have similar dual-signature forms.

Return to top of page or to the FAQ.


Comments and suggestions are welcome, and may be sent to Phil Edwards or Gabriel Dos Reis.
$Id: howto.html,v 1.3 2000/07/07 21:13:28 pme Exp $