CAPD DynSys Library  5.2.0
Multiple-precision in CAPD

Requirements

To use multiple-precision you need to install packages

  • libgmp, libgmp-dev
  • libgmpxx
  • libmpfr, libmpfr-dev.

They can be also compiled and installed from sources even under Windows, but in this tutorial we assume for simplicity that you are under Linux.

The configure script will automatically detect those libraries and compile mpcapd library (multiple precision version of the CAPD library).

Multiple-precision data types

The CAPD library uses generic programming (templates) therefore switching from double precision to multiple precision is really simple. You only have to replace the base building blocs (types) of the CAPD classes.

Use capd::multiPrec::MpReal instead of double. MpReal is a C++ class which represents multiple-precision floating point number with correct rounding.

To define multiple-precision interval you can write

Then in your code use MpInterval instead of DInterval (interval with double precision endpoints). To define MpInterval include capd/intervals/MpInterval.h instead of DoubleInterval.h or Interval.h.

If you use typedef's in your programs then most probably you need to change only two lines.

Examples

The following examples shows how to define multiple-precision variables, initiate them and use them as base types for other CAPD classes: Interval, Vector and Function.

These examples can be found in the capd/capdDynSys/examples/multiprecExample directory.

To compile first example one can invoke:

g++ -o mpExample1 mpExample1.cpp `CAPD_DIR/bin/mpcapd-config --cflags --libs`

where CAPD_DIR is a root directory of the CAPD library or directory to which the CAPD library was installed.

You can also use provided Makefile and simply call

make mpExample1

Basic usage of multiple precision floating points

First we include definition of MpReal and define shorter name MpFloat

Then we set precision to 100 mantisa bits (about 30 decimal digits)

and we set MpFloat to round numbers to the nearest representable.

Now we create several MpFloat objects using different possible constructors

and print them on a screen

Intervals with multiple-precision endpoints

}

Multiple-precision vectors and functions

}

Multiple-precision types from the mpcapd library

Instead of including a lot of header files, "building" your own types and defining new names for them (using typedef), you can include just one header file

#include "capd/mpcapdlib.h"

and use types which are included in the mpcapd library. It lets you to start writing your code without repeating every time the same tedious things (i.e. including a lot of files and making many typedefs). It also shorten compilation time, because they are not recompiled when your program is built.

In general names defined in the mpcapd library agree with those defined in examples above, with one difference: they are defined in the capd namespace. Therefore you can simply replace all "includes" and "typedefs" in the above examples by two lines

#include "capd/mpcapdlib.h"
using namespace capd;

and they should compile and work exactly the same.

The naming convention is as follows:

  • MpFloat is the name of the base multiple precision type,
  • if CLASS is the name of the template class then the name of a multiple precision type will be
    • MpCLASS for "nonrigorous" type
      e.g. MpInterval, MpVector, MpMatrix, MpFunction, MpMap, MpTaylor etc.
    • MpICLASS for rigorous, interval type
      e.g. MpIVector, MpIMatrix, MpIFunction, MpIMap, MpITaylor etc.

Remark: Not all possible templates are compiled into mpcapd library e.g vectors of a fixed size are not included. Instead we include only those that are often used. The list of types and their names can be found in the mplib.h file in the "include" directory of the corresponding module (e.g. capd/include/intervals/mplib.h).

#include "capd/mpcapdlib.h"
using namespace std;
using namespace capd;
int main(){
MpFloat::setDefaultPrecision(100);
MpFloat::setDefaultRndMode(MpFloat::RoundUp);
MpFloat a(1),
b("0.1",MpFloat::RoundNearest), // 0.1 is not representable and
// we want round it to nearest representable
c(0.125); // 0.125 is representable so no rounding is needed
MpVector v(3);
v[0] = a; v[1] = b; v[2] = c;
cout << "\n v = " << v;
MpFunction f("var:a,b,c;fun:a+b+c;");
cout << "\n f(a,b,c) = " << f(v) << endl;
return 0;
}
capd::rounding::RoundNearest
@ RoundNearest
Definition: DoubleRounding.h:28
capd::map::Function
Class Function represents a function .
Definition: Function.h:40
capd::vectalg::Vector
Definition: ColumnVector.h:177
capd::multiPrec::MpReal
MpReal represents multiple precision real number with controlled rounding.
Definition: MpReal.h:65
capd
Definition: atom.h:31
capd::intervals::MpInterval
Interval< MpReal, MpReal > MpInterval
Definition: MpInterval.h:26
capd::rounding::RoundUp
@ RoundUp
Definition: DoubleRounding.h:28
main
#define main()
Definition: krak-lib.h:382
capd::intervals::Interval
Definition of template class Interval.
Definition: Interval.h:41