Eigen + gmp 寻找 gmp_expr 而不是 mpq_class

Eigen + gmp looks for gmp_expr not mpq_class

我一般不熟悉模板和 C++,但想开始使用 Eigen 和 gmp。我遵循了关于自定义类型的 Eigen 教程,并希望输出到终端。我收到一个引用 __gmp_expr<__mpq_struct [1]、__mpq_struct [1]> 的编译错误,但我使用的是 mpq_class。我无法解决正在发生的事情。

我阅读了 gmp 文献,这些文献使我相信使用 gmpxx 的意义超出了我目前所能理解的范围。我已经设法正确输出单个矩阵元素和标量计算(行列式)。

我的头文件mpfr_type.h是基于Eigen教程,

#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
    };
  };
}

我的代码是,

#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;
}

我只是希望我的代码能够编译但是得到,

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

这是一个文档错误。 digits10() 应该是 return 一个整数。