1

I am unfamiliar with templates and C++ generally but want to get into using Eigen with gmp. I followed the Eigen tutorial on custom types and want to output to the terminal. I get a compilation error that makes reference to __gmp_expr<__mpq_struct [1], __mpq_struct [1]> but I am using mpq_class. It's beyond me to resolve what is going on.

I read gmp literature which convinced me there is more to using gmpxx than I can understand at this point. I have managed to correctly output individual matrix elements and scalar calculations (determinants) correctly.

My header file mpfr_type.h is based on the Eigen tutorial,

#include <gmpxx.h>
#include <boost/operators.hpp>

namespace Eigen {
  template<> struct NumTraits<mpq_class> : GenericNumTraits<mpq_class>
  {
    typedef mpq_class Real;
    typedef mpq_class NonInteger;
    typedef mpq_class Nested;
    static inline Real epsilon() { return 0; }
    static inline Real dummy_precision() { return 0; }
    static inline Real digits10() { return 0; }
    enum {
      IsInteger = 0,
      IsSigned = 1,
      IsComplex = 0,
      RequireInitialization = 1,
      ReadCost = 6,
      AddCost = 150,
      MulCost = 100
    };
  };
}

and my code is,

#include <Eigen/Dense>
#include <typeinfo>

#include "mpfr_type.h"

using namespace Eigen;

int main()
{
  // rational matrix
  Matrix<mpq_class,2,2> my_mat;

  my_mat(0,0) = mpq_class (2,3);
  my_mat(0,1) = mpq_class (7,1);
  my_mat(1,0) = mpq_class (5,1);
  my_mat(1,1) = mpq_class (11,1);

  auto my_det = my_mat.determinant();
  std::cout << my_det << std::endl; //works

  std::cout << "Here is my matrix:\n" << my_mat << std::endl; // error
  Return 0;
}

I simply expect my code to compile but get,

g++ -c -g example1.cpp -o example1.o -I ~/Documents/eigen/
In file included from /home/user/Documents/eigen/Eigen/Core:433:0,
                 from /home/user/Documents/eigen/Eigen/Dense:1,
                 from example1.cpp:2:
/home/user/Documents/eigen/Eigen/src/Core/IO.h: In instantiation of ‘static int Eigen::internal::significant_decimals_impl<Scalar>::run() [with Scalar = __gmp_expr<__mpq_struct [1], __mpq_struct [1]>]’:
/home/user/Documents/eigen/Eigen/src/Core/IO.h:155:66:   required from ‘std::ostream& Eigen::internal::print_matrix(std::ostream&, const Derived&, const Eigen::IOFormat&) [with Derived = Eigen::Matrix<__gmp_expr<__mpq_struct [1], __mpq_struct [1]>, 2, 2>; std::ostream = std::basic_ostream<char>]’
/home/user/Documents/eigen/Eigen/src/Core/IO.h:220:32:   required from ‘std::ostream& Eigen::operator<<(std::ostream&, const Eigen::DenseBase<Derived>&) [with Derived = Eigen::Matrix<__gmp_expr<__mpq_struct [1], __mpq_struct [1]>, 2, 2>; std::ostream = std::basic_ostream<char>]’
example1.cpp:44:42:   required from here
/home/user/Documents/eigen/Eigen/src/Core/IO.h:122:40: error: cannot convert ‘Eigen::NumTraits<__gmp_expr<__mpq_struct [1], __mpq_struct [1]> >::Real {aka __gmp_expr<__mpq_struct [1], __mpq_struct [1]>}’ to ‘int’ in return
     return NumTraits<Scalar>::digits10();
                                        ^
Makefile:12: recipe for target 'example1.o' failed
make: *** [example1.o] Error 1
2
  • 1
    This looks like a documentation bug ... Can you try changing the return type of digits10() to int?
    – chtz
    Commented May 31, 2019 at 13:36
  • static inline int digits10() { return 0; } compiled and ran successfully. If you want the points add it as an answer. Either way, thank you for taking the time to help me. I will contact Eigen about it. Commented May 31, 2019 at 13:42

1 Answer 1

2

This was a documentation bug. digits10() is supposed to return an integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.