Re: [eigen] Matrix2i mean

[ Thread Index | Date Index | More Archives ]

Adding an additional template method would not be a problem (minimal example:, though I don't see any real advantage vs writing


Sure, `m.sum<int>();` would be shorter, but also more confusing.

Your "another template parameter for the matrices" formulation sounded to me like you wanted to add another template parameter to `Eigen::Matrix` (which is not an option).

Any patches clarifying the documentation are welcome!


On 05/11/2019 16.44, Petr Kubánek wrote:

I know what's the problem. I am just looking either to document it or
for a solution.

Why would:

template <typename dt> dt sum()

break API/ABI? You will be able to call either sum() or sum<int32_t>(),
shouldn't you? I will try that and submit a patch.

Having sum<dt>() working, one can hopefully create mean<dt_sum,
dt_mean>(), so one can code:



On Tue, 2019-11-05 at 16:36 +0100, Christoph Hertzberg wrote:
On 05/11/2019 16.11, Peter wrote:

actually, this would also be interesting for the scalar products
general, namely a different
type for accumulating the sums within a scalar product, e.g. as
another template parameter for the matrices.

Adding another template parameter to basic types is not an option.
would break ABI and API compatibility (even if the parameter has a

You could create your own custom type `my_int16` for which `my_int16
my_int16` results in a `my_int32` (this needs to be told to Eigen,
similar to how real*complex products are handled).


I think it's more subtle than that.

    int16_t  Two =  2;
    int16_t  Max =  INT16_MAX;
    int16_t mean = ( Max/2 + Max + Two + Two ) / int16_t(4);

doesn't produce an overflow.

Yes, because `Max/2` gets implicitly converted to `int`. Actually,
adding two `int16_t` get implicitly converted to `int` (search for
integer promotion rules -- I don't know them entirely either). And
dividing an `int` by an `int16_t` results in an `int` which is only
afterwards converted to `int16_t`.

In contrast, Eigen::DenseBase::sum() does something (more or less)
equivalent to:

      T sum = T(0);
      for(Index i=0; i< size(); ++i)
      return sum;

i.e., after each addition the result gets reduced to the scalar type
the matrix, thus it will immediately overflow.

And mean() essentially just takes the return value of `sum()` and
divides it by the size.


 Dr.-Ing. Christoph Hertzberg

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

 Postadresse der Hauptgeschäftsstelle Standort Bremen:
 Robotics Innovation Center
 Robert-Hooke-Straße 1
 28359 Bremen, Germany

 Tel.:     +49 421 178 45-4021
 Zentrale: +49 421 178 45-0
 E-Mail:   christoph.hertzberg@xxxxxxx

 Weitere Informationen:
  Deutsches Forschungszentrum für Künstliche Intelligenz GmbH
  Trippstadter Strasse 122, D-67663 Kaiserslautern, Germany

  Prof. Dr. Jana Koehler (Vorsitzende)
  Dr. Walter Olthoff

  Vorsitzender des Aufsichtsrats:
  Prof. Dr. h.c. Hans A. Aukes
  Amtsgericht Kaiserslautern, HRB 2313

Mail converted by MHonArc 2.6.19+