This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Impossible to print a matrix of MPFReal type

Tags: None
(comma "," separated)
tatsuya1
Registered Member
Posts
3
Karma
0
OS
Thank you for your convenient library.

When I added a line to print a matrix in your sample program for MPRealSupport, then I got errors in compiling.
Here are this code and the errors.
Code: Select all
#include <iostream>
#include <unsupported/Eigen/MPRealSupport>
#include <Eigen/LU>
using namespace mpfr;
using namespace Eigen;
using namespace std;

int main()
{
  // set precision to 256 bits (double has only 53 bits)
  mpreal::set_default_prec(256);
  // Declare matrix and vector types with multi-precision scalar type
  typedef Matrix<mpreal,Dynamic,Dynamic>  MatrixXmp;
  typedef Matrix<mpreal,Dynamic,1>        VectorXmp;

  MatrixXmp A = MatrixXmp::Random(100,100);
  VectorXmp b = VectorXmp::Random(100);

  // Solve Ax=b using LU
  VectorXmp x = A.lu().solve(b);
  std::cout << "relative error: " << (A*x - b).norm() / b.norm() << std::endl;

  std::cout << A << std::endl;        // added by me to print the matrix A.
  return 0;
}


g++ -c MPRealSupport_example.cpp -I ~/lib/eigen-3.1.0-rc1
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h: In static member function ‘static NewType Eigen::internal::cast_impl<OldType, NewType>::run(const OldType&) [with OldType = mpfr::mpreal, NewType = int]’:
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h:419: instantiated from ‘NewType Eigen::internal::cast(const OldType&) [with OldType = mpfr::mpreal, NewType = int]’
....eigen-3.1.0-rc1/Eigen/src/Core/IO.h:147: instantiated from ‘static int Eigen::internal::significant_decimals_default_impl<Scalar, IsInteger>::run() [with Scalar = mpfr::mpreal, bool IsInteger = false]’
..../eigen-3.1.0-rc1/Eigen/src/Core/IO.h:195: instantiated from ‘std::ostream& Eigen::internal::print_matrix(std::ostream&, const Derived&, const Eigen::IOFormat&) [with Derived = Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/IO.h:259: instantiated from ‘std::ostream& Eigen::operator<<(std::ostream&, const Eigen::DenseBase<Derived>&) [with Derived = Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
MPRealSupport_example.cpp:25: instantiated from here
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h:410: error: invalid static_cast from type ‘const mpfr::mpreal’ to type ‘int’
make: *** [MPRealSupport_example.o] Error 1

Could you tell me a solution?

Best Regards.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
if you don't need the extra precision when printing I suggest you to cast to double:

cout << mat.cast<double>();
tatsuya1
Registered Member
Posts
3
Karma
0
OS
Thank you for your reply!

By your advice, I changed the above code to the following:
Code: Select all
std::cout << A.cast<double>() << std::endl;      // added by me to print the matrix A in double precision.                                                                                                                                                                                                                                                                         


Then I got errors below.
g++ -c MPRealSupport_example.cpp -I ~/lib/eigen-3.1.0-rc1
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h: In static member function ‘static NewType Eigen::internal::cast_impl<OldType, NewType>::run(const OldType&) [with OldType = mpfr::mpreal, NewType = doublee’:
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h:419: instantiated from ‘NewType Eigen::internal::cast(const OldType&) [with OldType = mpfr::mpreal, NewType = doublee’
..../eigen-3.1.0-rc1/Eigen/src/Core/Functors.h:358: instantiated from ‘const NewType Eigen::internal::scalar_cast_op<Scalar, NewType>::operator()(const Scalar&) const [with Scalar = mpfr::mpreal, NewType = doublee’
..../eigen-3.1.0-rc1/Eigen/src/Core/CwiseUnaryOp.h:129: instantiated from ‘const typename Eigen::internal::traits<Eigen::CwiseUnaryOp<UnaryOp, XprType> >::Scalar Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::coeff(typename Eigen::internal::traits<Eigen::CwiseUnaryOp<UnaryOp, XprType> >::Index) const [with UnaryOp = Eigen::inteenal::scalar:\
_cast_op<mpfr::mpreal, double>, XprType = const Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/DenseCoeffsBase.h:510: instantiated from ‘void Eigen::DenseCoeffsBase<Derived, 1>::copyCoeff(typename Eigen::internal::traits<T>::Index, const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x000000\
00000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/Assign.h:319: instantiated from ‘static void Eigen::internal::assign_impl<Derived1, Derived2, 1, 0, Version>::run(Derived1&, const Derived2&) [with Derived1 = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>, Derived2 = Eigen::CwiseUnaryOp<Eigen::intee\
rnal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >, int Version = 0]’
..../eigen-3.1.0-rc1/Eigen/src/Core/Assign.h:514: instantiated from ‘Derived& Eigen::DenseBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x00000000000000001, -0x00000000000000001, 0, -0x0000000000000000\
1, -0x00000000000000001> >, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/PlainObjectBase.h:408: instantiated from ‘Derived& Eigen::PlainObjectBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x00000000000000001, -0x00000000000000001, 0, -0x00\
0000000000000001, -0x00000000000000001> >, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/Assign.h:537: instantiated from ‘static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::run(Derived&, const OtherDerived&) [with Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>, OtherDerived = Eigen::Cwiseena\
ryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >]’
..../eigen-3.1.0-rc1/Eigen/src/Core/PlainObjectBase.h:614: instantiated from ‘Derived& Eigen::PlainObjectBase<Derived>::_set_noalias(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x00000000000000001, -0x00000000000000001, 0, -0\
x00000000000000001, -0x00000000000000001> >, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001>]’
..../eigen-3.1.0-rc1/Eigen/src/Core/Matrix.h:296: instantiated from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x00000000000000001, -0x000000000000\
00001, 0, -0x00000000000000001, -0x00000000000000001> >, _Scalar = double, int _Rows = -0x00000000000000001, int _Cols = -0x00000000000000001, int _Options = 0, int _MaxRows = -0x00000000000000001, int _MaxCols = -0x00000000000000001]’
..../eigen-3.1.0-rc1/Eigen/src/Core/DenseBase.h:392: instantiated from ‘typename Eigen::internal::add_const_on_value_type<typename Eigen::internal::eval<T, typename Eigen::internal::traits<T>::StorageKind>::type>::type Eigen::DenseBase<Derived>::eval() const [with Derived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, doublee, c\
onst Eigen::Matrix<mpfr::mpreal, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000000000001> >]’
..../eigen-3.1.0-rc1/Eigen/src/Core/IO.h:259: instantiated from ‘std::ostream& Eigen::operator<<(std::ostream&, const Eigen::DenseBase<Derived>&) [with Derived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<mpfr::mpreal, double>, const Eigen::Matrix<mpfr::mpreel, -0x00000000000000001, -0x00000000000000001, 0, -0x00000000000000001, -0x00000000\
000000001> >]’
MPRealSupport_example.cpp:25: instantiated from here
..../eigen-3.1.0-rc1/Eigen/src/Core/MathFunctions.h:410: error: invalid static_cast from type ‘const mpfr::mpreal’ to type ‘double’
make: *** [MPRealSupport_example.o] Error 1


Please tell me a suggestion.
In addition, If possible, I'd like to print out matrix in the full (and any) precision.

Best Regards
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
alright, same problem. Fixed in devel branch.
tatsuya1
Registered Member
Posts
3
Karma
0
OS
Thank you for your immediate modification!
Now, it works correctly.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell