Wrong result when assigning output of SparseLU::solve to real part of complex vector

Summary

Assigning the result of SparseLU::solve to the real part of a complex vector gives the wrong result.

Environment

  • Operating System : Linux
  • Architecture : x64
  • Eigen Version : trunk and 3.4.0
  • Compiler Version : Gcc12.2
  • Compile Flags :
  • Vector Extension :

Minimal Example

See also godbolt

#include <iostream>
#include <Eigen/SparseLU>

int main() {
  Eigen::SparseMatrix<double> m(2,2);
  m.coeffRef(0,0) = 4;
  m.coeffRef(1,0) = -1;
  m.coeffRef(0,1) = -1;
  m.coeffRef(1,1) = 4;

  Eigen::SparseLU<decltype(m),Eigen::COLAMDOrdering<int>> lu;
  lu.analyzePattern(m);
  lu.factorize(m);

  Eigen::Vector2d rhs(0,1);
  Eigen::VectorXcd x_complex(2);
  Eigen::VectorXd x_real(2);
  x_complex.setZero();
  x_complex.real() = lu.solve(rhs);
  x_real = lu.solve(rhs);

  std::cout << "x_complex:\n" <<  x_complex << std::endl << std::endl;
  std::cout << "x_real:\n" << x_real << std::endl << std::endl;
}

Output:

x_complex:
(0,0)
(1,0)

x_real:
0.0666667
 0.266667

Steps to reproduce

Compile the program and execute it.

What is the current bug behavior?

The result is wrong for x_complex. For x_real it is correct.

What is the expected correct behavior?

It should output the value for x_real