Re: [eigen] DenseBase<Derived>::Zero initialization problem

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


The argument to Zero is a size, but using Constant(10, 0.0) does not work directly either.  The issue isn't really with Jet, it's with std::complex<Jet>.  There's no argument to std::complex<Jet> that accepts an int/float (it must be a Jet), and the conversion to Jet must be explicit.  I don't think we should rely on the default constructor, since this will not work in all cases, and this is not the only place we try to convert from a int/float to custom scalar.

This kind of thing has come up a few times before for custom scalar types - we make assumptions in several places that we can construct the custom scalar from ints (e.g. 0, 1) or floats (e.g. for tolerances).  But not all custom scalars allow such construction in general.  I think the proper solution is to use an explicit `cast` call that the user can specialize for their type.  Currently we have Eigen::internal::cast that could be used for this - though we may want to move this to Eigen::numext and tell people to specialize it.



On Wed, Jun 22, 2022 at 9:00 AM Rasmus Munk Larsen <rmlarsen@xxxxxxxxxx> wrote:
BTW: What happens if you do Mat x = Mat::Zero(10..0); ? Perhaps conversion from double or float work, because those types are "differentiable", as opposed to int.

On Wed, Jun 22, 2022 at 5:13 AM Oleg Shirokobrod <oleg.shirokobrod@xxxxxxxxx> wrote:
Hi,

In the file CwiseNullaryOp.h the functions DenseBase<Derived>::Zero call function DenseBase<Derived>::Constant which uses Scalar(0) for zero-initialization. It gives compilation error for this line 
using Mat = Eigen::Matrix<std::complex<ceres::Jet<double, 3>>, -1, 1, 0, -1, 1>; 
Mat x = Mat::Zero(10);
Here ceres::Jet<double, 3> is autodif type in ceres-solver. The problem is that it is not always possible to make conversion from int. From the other side for non-class-type scalar values, Scalar() provides a temporary which is zero-initialized. It is likely that for custom numerical class types Scalar() provides zero-initialized temporary.. This is the case for Jet. When I replaced 

return Constant(size, Scalar(0)); 

with 

return Constant(size, Scalar()); 

in all Zero and setZero functions, code was compiled without errors.

Best,

Oleg



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