TensorIOFormat::Numpy/Native do not format complex values as expected.

Summary

I'm not sure if this is really a bug per se, but Eigen::TensorIOFormat::Numpy() and Native() don't print complex numbers with the expected style.

Minimal Example

#include <iostream>
#include <eigen3/unsupported/Eigen/CXX11/Tensor>

int main() {
  Eigen::Tensor<std::complex<float>, 2> t(2, 2);
  t.setValues({
    {{1, 1}, {1, 2}},
    {{3, 4}, {5, 9.99}}
  });
  std::cout << t.format(Eigen::TensorIOFormat::Numpy()) << '\n';
  std::cout << t.format(Eigen::TensorIOFormat::Native()) << '\n';
  return 0;
}

What is the current bug behavior?

The MRE Prints

[[   (1,1)    (1,2)]
 [   (3,4) (5,9.99)]]
{{   (1,1),    (1,2)},
 {   (3,4), (5,9.99)}}

complex numbers are always printed like (real,imag), which is not the numpy style and not copy-pastable into C++ code.

What is the expected correct behavior?

when using TensorIOFormat::Numpy() I would expect it print with the style 1+1j. Numpy also uses separate widths for the real/imag parts.

[[1.+1.j   1.+2.j  ]
 [3.+4.j   5.+9.99j]]

when using TensorIOFormat::Native() should have the style {1,1}.

{{   {1,1},    {1,2}},
 {   {3,4}, {5,9.99}}}

Anything else that might help

  • Have a plan to fix this issue.
    • start grad school this week, so depends how much time I have 😢