This bugzilla service is closed. All entries have been migrated to
Bug 1399 - Tensor contraction operates on uninitialized scalars
Summary: Tensor contraction operates on uninitialized scalars
Status: NEW
Alias: None
Product: Eigen
Classification: Unclassified
Component: Tensor (show other bugs)
Version: 3.3 (current stable)
Hardware: All All
: Normal Crash
Assignee: Nobody
Depends on:
Reported: 2017-03-02 13:52 UTC by Jonas Hagmar
Modified: 2019-12-04 16:51 UTC (History)
4 users (show)


Description Jonas Hagmar 2017-03-02 13:52:32 UTC
When using a custom scalar in tensors used in a tensor contraction, the contraction operates on scalars that have not been initialized by the scalar constructor. Consider the following custom scalar:

class CustomScalar {
  CustomScalar() {}
  CustomScalar(int) {}
  CustomScalar(CustomScalar const &other) {}
  CustomScalar &operator=(CustomScalar const &other) {
    return *this;
  CustomScalar& operator+=(const CustomScalar& rhs) {
    return *this;
  ~CustomScalar() {
    std::cout << value << std::endl;
  int value = 42;

CustomScalar operator*(CustomScalar const &o1, CustomScalar const &o2) { return CustomScalar(); }
CustomScalar operator+(CustomScalar const &o1, CustomScalar const &o2) { return CustomScalar(); }

For any CustomScalar instance that has been initialized by a constructor, the string "42" should be written to the console on destruction. However, when the following tensor contraction is performed, a few non-"42" values are written to the console, demonstrating that the contraction operates on memory that has not been initialized by any CustomScalar constructor:

Eigen::Tensor<CustomScalar, 2> A(2,2);
Eigen::array<Eigen::IndexPair<int>, 1> product_dims = { Eigen::IndexPair<int>(1, 0) };
Eigen::Tensor<CustomScalar, 2> AA(A.contract(A, product_dims));

I have checked that NumTraits<CustomScalar>::RequireInitialization == 1, which, as far as I understand, is the flag to indicate that the scalar constructor has to be called. I have also tested that matrix multiplication using MatrixX works using CustomScalar.

This problem showed up when I wanted to have a scalar type that uses dynamic memory allocation, which turns out to be impossible given the current implementation.
Comment 1 Nobody 2019-12-04 16:51:34 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance:

Note You need to log in before you can comment on or make changes to this bug.