我试图用矩阵执行行向量的元素明智乘法。在MATLAB中,这可以简单地通过"。"来完成。运营商或:
deriv = 1i * k .* fk;
,其中k
为行向量,fk
为矩阵。现在在c++中,我有这样的代码:
static const int nx = 10;
static const int ny = 10;
static const int nyk = ny/2 + 1;
static const int nxk = nx/2 + 1;
static const int ncomp = 2;
Matrix <double, 1, nx> eK;
eK.setZero();
for(int i = 0; i < nx; i++){
eK[i] = //some expression
}
fftw_complex *UOut;
UOut= (fftw_complex*) fftw_malloc((((nx)*(ny+1))*nyk)* sizeof(fftw_complex));
for (int i = 0; i < nx; i++){
for (int j = 0; j < ny+1; j++){
for (int k = 0; k < ncomp; k++){
UOut[i*(ny+1)+j][k] = //FFT of some expression
}
}
}
Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> U(reinterpret_cast<std::complex<double>*>(UOut),(ny+1),nx);
现在,我试着取eK
(1 × 10的行向量)和矩阵U
(11 × 10)的乘积。我尝试了一些方法,但似乎没有一个真的有效:
U = 1i * eKX.array() * euhX.array() ; //ERROR
static assertion failed: YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
(
| ~~~
176 | (int(Eigen::internal::size_of_xpr_at_compile_time<TYPE0>::ret)==0 && int(Eigen::internal::size_of_xpr_at_compile_time<TYPE1>::ret)==0)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177 | || (
| ^~~~~
178 | (int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic
除非您明确要求,否则Eigen不会像Matlab或Numpy那样广播,例如使用matrix.array().rowwise() * vector.array()
IMHO更清晰的形式是将向量解释为对角矩阵。
Eigen::VectorXd eK = ...;
Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> U = ...;
Eigen::MatrixXcd result = U * (eK * 1i).asDiagonal();