Re: [eigen] new types / NumTraits

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi,

Eigen needs to know how many significant digits a type has, which by default it does using:

cast<RealScalar,int>(numext::ceil(-numext::log(NumTraits<RealScalar>::epsilon())/numext::log(RealScalar(10))))

In your case the easiest solution is to specialize:

namespace Eigen { namespace internal {
  template<>
  struct significant_decimals_impl<TpFloat>
      : public significant_decimals_impl<TpFloatBase>
  { };
} }

We should probably add this to the documentation (and perhaps consider not putting it into the internal namespace).

However, there are likely other functions which fail if you can't convert your type to int.

If you want to implement a cast from your type to int, but can't add an operator to your class, you can specialize this struct (in Eigen::internal):

  template<typename OldType, typename NewType>  struct cast_impl;

See the last line of your error to locate the default implementation.


Christoph



On 2016-07-22 16:38, Peter wrote:
Dear All,

first I would like to thank everyone involved in the Eigen library.
I'm using Eigen more  and more for projects I traditionally used octave.
It's really fun using Eigen.

In return I've started to look closer into possible applications. One
interest
of mine consists in using Eigen for user defined types. In the attached
example
I'm trying to to use Eigen for the following struct:


struct  TpFloat
{
     TpFloat() {};

     explicit TpFloat( const int r) : v(r), Counter(0) {};
     TpFloat( const TpFloatBase r) : v(r), Counter(0) {};
     TpFloat( const TpFloatBase r, const int n ) : v(r), Counter(n) {};

     TpFloatBase v;
     int         Counter;

     TpFloat operator-() { return TpFloat( -v, Counter+1) ; }

};

The initialization of a matrix works fine, however I have compilation
problems for

std::cout << A << std::endl;


getting errors like

$  g++ -std=c++11  -march=native -DNDEBUG -I $HOME/Eigen/eigen E.C -o E
-lm && ./E

In file included from /home/peter/Eigen/eigen/Eigen/Core:259:0,
                  from /home/peter/Eigen/eigen/Eigen/Dense:1,
                  from E.C:5:
/home/peter/Eigen/eigen/Eigen/src/Core/MathFunctions.h: In instantiation
of ‘static NewType Eigen::internal::cast_impl<OldType,
NewType>::run(const OldType&) [with OldType = TpFloat; NewType = int]’:
/home/peter/Eigen/eigen/Eigen/src/Core/MathFunctions.h:339:44:
required from ‘NewType Eigen::internal::cast(const OldType&) [with
OldType = TpFloat; NewType = int]’
/home/peter/Eigen/eigen/Eigen/src/Core/IO.h:132:97:   required from
‘static int Eigen::internal::significant_decimals_default_impl<Scalar,
IsInteger>::run() [with Scalar = TpFloat; bool IsInteger = false]’
/home/peter/Eigen/eigen/Eigen/src/Core/IO.h:180:67:   required from
‘std::ostream& Eigen::internal::print_matrix(std::ostream&, const
Derived&, const Eigen::IOFormat&) [with Derived = Eigen::Matrix<TpFloat,
-1, -1, 1>; std::ostream = std::basic_ostream<char>]’
/home/peter/Eigen/eigen/Eigen/src/Core/IO.h:245:69:   required from
‘std::ostream& Eigen::operator<<(std::ostream&, const
Eigen::DenseBase<Derived>&) [with Derived = Eigen::Matrix<TpFloat, -1,
-1, 1>; std::ostream = std::basic_ostream<char>]’
E.C:78:15:   required from here
/home/peter/Eigen/eigen/Eigen/src/Core/MathFunctions.h:330:34: error:
invalid static_cast from type ‘const TpFloat’ to type ‘int’
      return static_cast<NewType>(x);


Is it necessary to define a cast from the user defined type to int?
I'm using g++ v. 4.8 and eigen v. 3.2.9.

Any help is appreciated,
thanks in advance,
Peter


--
 Dipl. Inf., Dipl. Math. Christoph Hertzberg

 Universität Bremen
 FB 3 - Mathematik und Informatik
 AG Robotik
 Robert-Hooke-Straße 1
 28359 Bremen, Germany

 Zentrale: +49 421 178 45-6611

 Besuchsadresse der Nebengeschäftsstelle:
 Robert-Hooke-Straße 5
 28359 Bremen, Germany

 Tel.:    +49 421 178 45-4021
 Empfang: +49 421 178 45-6600
 Fax:     +49 421 178 45-4150
 E-Mail:  chtz@xxxxxxxxxxxxxxxxxxxxxxxx

 Weitere Informationen: http://www.informatik.uni-bremen.de/robotik



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/