Error: cannot convert ‘const Scalar*’ {aka ‘const double*’} to ‘const std::complex<double>*’

I am trying to solve for a linear system of equations x = A\b and I am using the following expression in Eigen:
x = A.colPivHouseholderQr().solve(b); but I keep getting an error:

 cannot convert ‘const Scalar*’ {aka ‘const double*’} to ‘const std::complex<double>*’ 

So, my code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static const int nx = 10;
static const int ny = 10; 

//initializations
Matrix <double, 1, nx> kx; 
kx.setZero();
//initialized
Eigen::Matrix< double, (ny+1), (ny+1)> L; 
L.setZero();

Eigen::Matrix<std::complex<double>, (ny+1), (nx)> unewh;
unewh.setZero();
for (int l = 0; l < kx.size(); l++){ // 1:length(kx)= kx.size() 
         L = div_x_act_on_grad_x * std::pow(kxinv(l),2) + div_y_act_on_grad_y;
	unewh(all,l) = L.colPivHouseholderQr().solve(Stilde(all,l)); //ERROR
						
}

where ``kx`` and ``kxinv`` are both row vectors (1 by 10) and ``L`` is a 11 by 11 matrix, ``unewh`` and ``Stilde`` are both 11 by 10 matrices, thus, ``unewh(:,m)`` and ``Stilde(:,m)`` are column vectors with 11 by 1 size.

and the full error message is:

 error: cannot convert ‘const Scalar*’ {aka ‘const double*’} to ‘const std::complex<double>*’
  438 |     internal::product_triangular_matrix_matrix<Scalar, Index,
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  439 |       Mode, LhsIsTriangular,
      |       ~~~~~~~~~~~~~~~~~~~~~~
  440 |       (internal::traits<ActualLhsTypeCleaned>::Flags&RowMajorBit) ? RowMajor : ColMajor, LhsBlasTraits::NeedToConjugate,
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  441 |       (internal::traits<ActualRhsTypeCleaned>::Flags&RowMajorBit) ? RowMajor : ColMajor, RhsBlasTraits::NeedToConjugate,
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  442 |       (internal::traits<Dest          >::Flags&RowMajorBit) ? RowMajor : ColMajor, Dest::InnerStrideAtCompileTime>
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  443 |       ::run(
      |       ~~~~~^
  444 |         stripedRows, stripedCols, stripedDepth,   // sizes
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  445 |         &lhs.coeffRef(0,0), lhs.outerStride(),    // lhs info
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  446 |         &rhs.coeffRef(0,0), rhs.outerStride(),    // rhs info
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  447 |         &dst.coeffRef(0,0), dst.innerStride(), dst.outerStride(),    // result info
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  448 |         actualAlpha, blocking
      |         ~~~~~~~~~~~~~~~~~~~~~
  449 |       );
      |       ~

Last edited on
I can not seem to understand the problem, and I couldn't reproduce my code since it's pretty big. I am just going to add the full code here in case someone would like to take a look or something.

https://www.zipshare.com/download/eyJhcmNoaXZlSWQiOiJjZjhlNTM5OC0yZGVhLTQ4YmQtOTFmMi05NmZjYzYwNzcyZDQiLCJlbWFpbCI6Imx1amFpbkB2dC5lZHUifQ==
std::complex<double> is layout compatible with double[2]; but a reinterpret_cast is required.
https://en.cppreference.com/w/cpp/numeric/complex#Array-oriented_access

The implementation must also ensure that optimizations to array access account for the possibility that a pointer to value_type may be aliasing a std::complex specialization or array thereof.

https://en.cppreference.com/w/cpp/numeric/complex#Implementation_notes

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <complex>

int main()
{
    const std::size_t N = 4 ;
    const double arr_dbl[N*2] { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 } ;
    
    for( std::size_t i = 0 ; i < N ; ++i )
        std::cout << '|' << arr_dbl[i*2] << ' ' << arr_dbl[i*2+1] << "|  " ;
    std::cout << '\n' ;

    const std::complex<double>* pcomplex = reinterpret_cast< const std::complex<double>* >(arr_dbl) ;
    for( std::size_t i = 0 ; i < N ; ++i ) std::cout << pcomplex[i] << "  " ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/fea72f3d83220466
Thanks for the help. I was wondering what is the difference b/w reinterpret_cast vs cast because when I write the following it gets rid off the error for me:
1
2
unewh(all,l) = L.cast<std::complex<double>>().colPivHouseholderQr().solve(Stilde(all,l));


Thanks again
Topic archived. No new replies allowed.