This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 1612 - rowwise().mean() gives wrong results for Scalar = std::complex<double>
Summary: rowwise().mean() gives wrong results for Scalar = std::complex<double>
Status: RESOLVED FIXED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: 3.4 (development)
Hardware: All All
: Normal Wrong Result
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-10-15 19:06 UTC by Rasmus Munk Larsen
Modified: 2019-12-04 18:01 UTC (History)
4 users (show)



Attachments

Description Rasmus Munk Larsen 2018-10-15 19:06:52 UTC
Reproducer:

TEST(EigenTest, RowWiseMean) {
  using Scalar = std::complex<double>;
  using Matrix = Eigen::Array<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
  Matrix input;
  input.setRandom(10, 2);

  Matrix mean_output;
  mean_output = input.rowwise().mean();

  Matrix expected_output;
  expected_output = (input.col(0) + input.col(1)) / 2;

  VLOG(2) << "\33[0;36m" << std::endl
          << "ComplexdArrayRowwiseMeanTest:" << std::endl
          << "input:" << std::endl
          << input << std::endl
          << "\33[0;32m" << std::endl
          << "mean_output:" << std::endl
          << mean_output << std::endl
          << "\33[0;33m" << std::endl
          << "expected_output:" << std::endl
          << expected_output << std::endl
          << "\33[0;0m" << std::endl;

  double max_abs_error = (mean_output - expected_output).abs().maxCoeff();
  EXPECT_LT(max_abs_error, 1e-5);
}



Output:

input:
0(0.680375,-0.211234) (-0.967399,-0.514226)
000(0.566198,0.59688) 0(-0.725537,0.608354)
0(0.823295,-0.604897) (-0.686642,-0.198111)
0(-0.329554,0.536459) (-0.740419,-0.782382)
00(-0.444451,0.10794) 0(0.997849,-0.563486)
(-0.0452059,0.257742) 0(0.0258648,0.678224)
(-0.270431,0.0268018) 00(0.22528,-0.407937)
000(0.904459,0.83239) 0(0.275105,0.0485744)
00(0.271423,0.434594) 00(-0.012834,0.94555)
0(-0.716795,0.213938) 0(-0.414966,0.542715)
mean_output:
0(0.340188,-0.105617)
000(0.283099,0.29844)
0(0.411647,-0.302449)
00(-0.164777,0.26823)
00(-0.222225,0.05397)
(-0.0226029,0.128871)
(-0.135216,0.0134009)
000(0.45223,0.416195)
00(0.135712,0.217297)
0(-0.358397,0.106969)

expected_output:
00(-0.143512,-0.36273)
0(-0.0796692,0.602617)
0(0.0683264,-0.401504)
0(-0.534987,-0.122962)
00(0.276699,-0.227773)
(-0.00967055,0.467983)
(-0.0225757,-0.190567)
000(0.589782,0.440482)
000(0.129295,0.690072)
00(-0.565881,0.378327)
Comment 1 Rasmus Munk Larsen 2018-10-15 19:11:08 UTC
FYI: I get the same result compiling in optimized and debug mode.
Comment 2 Gael Guennebaud 2018-10-15 22:24:28 UTC
this only occurs with double (fine with complex<float>), and the error is already there with with .sum(). Looks like an issue when vectorizating with PacketSize==1
Comment 3 Gael Guennebaud 2018-10-15 22:47:55 UTC
damn, I've been tricked by bug 416...
Comment 4 Gael Guennebaud 2018-10-15 23:07:36 UTC
quick fix, by-pass vectorization in this special case:

https://bitbucket.org/eigen/eigen/commits/a79aa7f2f56e/
Summary:     Bug 1612: fix regression in "outer-vectorization" of partial reductions for PacketSize==1 (aka complex<double>)
Comment 5 Rasmus Munk Larsen 2018-10-15 23:17:20 UTC
Thanks for the quick fix, Gael!
Comment 6 Nobody 2019-12-04 18:01:57 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com'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: https://gitlab.com/libeigen/eigen/issues/1612.

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