使用特征:在向量和矩阵之间执行元素乘法的错误
我正在尝试使用矩阵对行矢量进行元素乘法。在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 x 10的行矢量和11 x的矩阵 u 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 \
I am trying to perform an element-wise multiplication of a row vector with matrix. In MATLAB this would be simply done by the "dot" operator or:
deriv = 1i * k .* fk;
where k
is row vector and fk
is a matrix.
Now in C++ I have this code:
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);
Now, I am trying to take the product of eK
which is a row vector of 1 x 10 and the matrix U
of a 11 x 10. I tried few things, none of which seem to work really:
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 \
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EIGEN不会像Matlab或Numpy那样广播,例如使用
matrix.Array()。rowwise() * vector.aray()
除非您明确要求它,否则 将矢量解释为对角线矩阵。
Eigen doesn't do broadcasting the same way Matlab or Numpy do unless you explicitely ask for it, for example with
matrix.array().rowwise() * vector.array()
The IMHO clearer form is to interpret the vector as a diagonal matrix.