[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 11. Using the library

For the following discussion, we will assume that you have installed the CLN source in `$CLN_DIR` and built it in `$CLN_TARGETDIR`. For example, for me it's `CLN_DIR="$HOME/cln"` and `CLN_TARGETDIR="$HOME/cln/linuxelf"`. You might define these as environment variables, or directly substitute the appropriate values.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 11.1 Compiler options

Until you have installed CLN in a public place, the following options are needed:

When you compile CLN application code, add the flags

       -I$CLN_DIR/include -I$CLN_TARGETDIR/include

to the C++ compiler's command line (`make` variable CFLAGS or CXXFLAGS). When you link CLN application code to form an executable, add the flags

       $CLN_TARGETDIR/src/libcln.a

to the C/C++ compiler's command line (`make` variable LIBS).

If you did a `make install`, the include files are installed in a public directory (normally `/usr/local/include`), hence you don't need special flags for compiling. The library has been installed to a public directory as well (normally `/usr/local/lib`), hence when linking a CLN application it is sufficient to give the flag `-lcln`.

To make the creation of software packages that use CLN easier, the `pkg-config` utility can be used. CLN provides all the necessary metainformation in a file called `cln.pc` (installed in `/usr/local/lib/pkgconfig` by default). A program using CLN can be compiled and linked using (1)

    g++ `pkg-config --libs cln` `pkg-config --cflags cln` prog.cc -o prog

Software using GNU autoconf can check for CLN with the `PKG_CHECK_MODULES` macro supplied with `pkg-config`.

    PKG_CHECK_MODULES([CLN], [cln >= MIN-VERSION])

This will check for CLN version at least MIN-VERSION. If the required version was found, the variables CLN_CFLAGS and CLN_LIBS are set. Otherwise the configure script aborts. If this is not the desired behaviour, use the following code instead (2)

    PKG_CHECK_MODULES([CLN], [cln >= MIN-VERSION], [],
     [AC_MSG_WARNING([No suitable version of CLN can be found])])

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 11.2 Include files

Here is a summary of the include files and their contents.

`<cln/object.h>`

  General definitions, reference counting, garbage collection.

`<cln/number.h>`

  The class cl_number.

`<cln/complex.h>`

  Functions for class cl_N, the complex numbers.

`<cln/real.h>`

  Functions for class cl_R, the real numbers.

`<cln/float.h>`

  Functions for class cl_F, the floats.

`<cln/sfloat.h>`

  Functions for class cl_SF, the short-floats.

`<cln/ffloat.h>`

  Functions for class cl_FF, the single-floats.

`<cln/dfloat.h>`

  Functions for class cl_DF, the double-floats.

`<cln/lfloat.h>`

  Functions for class cl_LF, the long-floats.

`<cln/rational.h>`

  Functions for class cl_RA, the rational numbers.

`<cln/integer.h>`

  Functions for class cl_I, the integers.

`<cln/io.h>`

  Input/Output.

`<cln/complex_io.h>`

  Input/Output for class cl_N, the complex numbers.

`<cln/real_io.h>`

  Input/Output for class cl_R, the real numbers.

`<cln/float_io.h>`

  Input/Output for class cl_F, the floats.

`<cln/sfloat_io.h>`

  Input/Output for class cl_SF, the short-floats.

`<cln/ffloat_io.h>`

  Input/Output for class cl_FF, the single-floats.

`<cln/dfloat_io.h>`

  Input/Output for class cl_DF, the double-floats.

`<cln/lfloat_io.h>`

  Input/Output for class cl_LF, the long-floats.

`<cln/rational_io.h>`

  Input/Output for class cl_RA, the rational numbers.

`<cln/integer_io.h>`

  Input/Output for class cl_I, the integers.

`<cln/input.h>`

  Flags for customizing input operations.

`<cln/output.h>`

  Flags for customizing output operations.

`<cln/malloc.h>`

  `malloc_hook`, `free_hook`.

`<cln/exception.h>`

  Exception base class.

`<cln/condition.h>`

  Conditions.

`<cln/string.h>`

  Strings.

`<cln/symbol.h>`

  Symbols.

`<cln/proplist.h>`

  Property lists.

`<cln/ring.h>`

  General rings.

`<cln/null_ring.h>`

  The null ring.

`<cln/complex_ring.h>`

  The ring of complex numbers.

`<cln/real_ring.h>`

  The ring of real numbers.

`<cln/rational_ring.h>`

  The ring of rational numbers.

`<cln/integer_ring.h>`

  The ring of integers.

`<cln/numtheory.h>`

  Number threory functions.

`<cln/modinteger.h>`

  Modular integers.

`<cln/V.h>`

  Vectors.

`<cln/GV.h>`

  General vectors.

`<cln/GV_number.h>`

  General vectors over cl_number.

`<cln/GV_complex.h>`

  General vectors over cl_N.

`<cln/GV_real.h>`

  General vectors over cl_R.

`<cln/GV_rational.h>`

  General vectors over cl_RA.

`<cln/GV_integer.h>`

  General vectors over cl_I.

`<cln/GV_modinteger.h>`

  General vectors of modular integers.

`<cln/SV.h>`

  Simple vectors.

`<cln/SV_number.h>`

  Simple vectors over cl_number.

`<cln/SV_complex.h>`

  Simple vectors over cl_N.

`<cln/SV_real.h>`

  Simple vectors over cl_R.

`<cln/SV_rational.h>`

  Simple vectors over cl_RA.

`<cln/SV_integer.h>`

  Simple vectors over cl_I.

`<cln/SV_ringelt.h>`

  Simple vectors of general ring elements.

`<cln/univpoly.h>`

  Univariate polynomials.

`<cln/univpoly_integer.h>`

  Univariate polynomials over the integers.

`<cln/univpoly_rational.h>`

  Univariate polynomials over the rational numbers.

`<cln/univpoly_real.h>`

  Univariate polynomials over the real numbers.

`<cln/univpoly_complex.h>`

  Univariate polynomials over the complex numbers.

`<cln/univpoly_modint.h>`

  Univariate polynomials over modular integer rings.

`<cln/timing.h>`

  Timing facilities.

`<cln/cln.h>`

  Includes all of the above.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 11.3 An Example

A function which computes the nth Fibonacci number can be written as follows.

    #include <cln/integer.h>
    #include <cln/real.h>
    using namespace cln;

    // Returns F_n, computed as the nearest integer to
    // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
    const cl_I fibonacci (int n)
    {
            // Need a precision of ((1+sqrt(5))/2)^-n.
            float_format_t prec = float_format((int)(0.208987641*n+5));
            cl_R sqrt5 = sqrt(cl_float(5,prec));
            cl_R phi = (1+sqrt5)/2;
            return round1( expt(phi,n)/sqrt5 );
    }

Let's explain what is going on in detail.

The include file `<cln/integer.h>` is necessary because the type `cl_I` is used in the function, and the include file `<cln/real.h>` is needed for the type `cl_R` and the floating point number functions. The order of the include files does not matter. In order not to write out `cln::`foo in this simple example we can safely import the whole namespace `cln`.

Then comes the function declaration. The argument is an `int`, the result an integer. The return type is defined as ``const cl_I'`, not simply ``cl_I'`, because that allows the compiler to detect typos like ``fibonacci(n) = 100'`. It would be possible to declare the return type as `const cl_R` (real number) or even `const cl_N` (complex number). We use the most specialized possible return type because functions which call ``fibonacci'` will be able to profit from the compiler's type analysis: Adding two integers is slightly more efficient than adding the same objects declared as complex numbers, because it needs less type dispatch. Also, when linking to CLN as a non-shared library, this minimizes the size of the resulting executable program.

The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest integer. In order to get a correct result, the absolute error should be less than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)). To this end, the first line computes a floating point precision for sqrt(5) and phi.

Then sqrt(5) is computed by first converting the integer 5 to a floating point number and than taking the square root. The converse, first taking the square root of 5, and then converting to the desired precision, would not work in CLN: The square root would be computed to a default precision (normally single-float precision), and the following conversion could not help about the lacking accuracy. This is because CLN is not a symbolic computer algebra system and does not represent sqrt(5) in a non-numeric way.

The type `cl_R` for sqrt5 and, in the following line, phi is the only possible choice. You cannot write `cl_F` because the C++ compiler can only infer that `cl_float(5,prec)` is a real number. You cannot write `cl_N` because a ``round1'` does not exist for general complex numbers.

When the function returns, all the local variables in the function are automatically reclaimed (garbage collected). Only the result survives and gets passed to the caller.

The file `fibonacci.cc` in the subdirectory `examples` contains this implementation together with an even faster algorithm.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 11.4 Debugging support

When debugging a CLN application with GNU `gdb`, two facilities are available from the library:

- The library does type checks, range checks, consistency checks at many places. When one of these fails, an exception of a type derived from `runtime_exception` is thrown. When an exception is cought, the stack has already been unwound, so it is may not be possible to tell at which point the exception was thrown. For debugging, it is best to set up a catchpoint at the event of throwning a C++ exception:

      (gdb) catch throw

  When this catchpoint is hit, look at the stack's backtrace:

      (gdb) where

  When control over the type of exception is required, it may be possible to set a breakpoint at the `g++` runtime library function `__raise_exception`. Refer to the documentation of GNU `gdb` for details.

- The debugger's normal `print` command doesn't know about CLN's types and therefore prints mostly useless hexadecimal addresses. CLN offers a function `cl_print`, callable from the debugger, for printing number objects. In order to get this function, you have to define the macro ``CL_DEBUG'` and then include all the header files for which you want `cl_print` debugging support. For example:

      #define CL_DEBUG
      #include <cln/string.h>

  Now, if you have in your program a variable `cl_string s`, and inspect it under `gdb`, the output may look like this:

      (gdb) print s
      $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
        word = 134568800}}, }
      (gdb) call cl_print(s)
      (cl_string) ""
      $8 = 134568800

  Note that the output of `cl_print` goes to the program's error output, not to gdb's standard output.

  Note, however, that the above facility does not work with all CLN types, only with number objects and similar. Therefore CLN offers a member function `debug_print()` on all CLN types. The same macro ``CL_DEBUG'` is needed for this member function to be implemented. Under `gdb`, you call it like this:

      (gdb) print s
      $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
        word = 134568800}}, }
      (gdb) call s.debug_print()
      (cl_string) ""
      (gdb) define cprint
      >call ($1).debug_print()
      >end
      (gdb) cprint s
      (cl_string) ""

  Unfortunately, this feature does not seem to work under all circumstances.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 11.5 Reporting Problems

If you encounter any problem, please don't hesitate to send a detailed bugreport to the `cln-list@ginac.de` mailing list. Please think about your bug: consider including a short description of your operating system and compilation environment with corresponding version numbers. A description of your configuration options may also be helpful. Also, a short test program together with the output you get and the output you expect will help us to reproduce it quickly. Finally, do not forget to report the version number of CLN.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Richard B. Kreckel on April, 5 2008 using texi2html 1.76.