Using Mathemagix packages in an application

In the example, we show how to create small project test, which construct an application solver for solving univariate polynomial equations.

First, we create folder test:

mkdir test

In this folder, we write a cpp code (eg. solver.cpp) which uses mathemagix packages:

#include <realroot/GMP.hpp>
#include <realroot/polynomial_tensor.hpp>
#include <realroot/solver_continued_fraction.hpp>

int main(int argc, char** argv)
{
  using namespace mmx;

  typedef ZZ coeff_t;
  typedef polynomial<coeff_t,with<MonomialTensor> > Polynomial;
  typedef solver<coeff_t,ContFrac<Approximate> > Solver;

  if(argc>0) {
    Polynomial p(argv[1]);
    std::cout<<"Polynomial: "<<p<<std::endl;

    Solver::Solutions sol; Solver::solve(sol,p);
    std::cout<<"Solutions : "<<sol<<std::endl;
  }
}

Then we write the corresponding CMakeLists.txt configuration file for cmake:

## Preliminaries for cmake.
cmake_minimum_required(VERSION 2.6)

## Path for files used by the command find_package 
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_BINARY_DIR}/CMakeModules/)

## Search the package realroot and add the include path and 
## link the corresponding libraries.
## As REQUIRED, if the package is not found, it stops with an error.
 
find_package(Realroot REQUIRED)

include_directories( ${REALROOT_INCLUDE_DIR} )
link_libraries( ${REALROOT_LIBRARIES} )

## Compile a simple executable solver.
add_executable(solver ./solver.cpp)
target_link_libraries(solver ${REALROOT_LIBRARIES})

The folder test should now contains the following files:

test
|-- CMakeLists.txt
‘-- solver.cpp 

We go in the build folder and configure the package test with cmake:

cd ../build; rm -rf CMakeCache.txt; cmake ../test

Notice that we found the package realroot, which was already built in the folder build.

Then, we compile the code:

make

Now, we can use the application solver as follows:

./solver "x^8-3*x-1"

Polynomial: -1-3*x+x^8
Solutions : [-15367/46108, -2177/6532], [2901/2395, 2557/2111] 
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License. If you don't have this file, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.