使用特征:在向量和矩阵之间执行元素乘法的错误

发布于 2025-02-13 07:13:47 字数 1615 浏览 0 评论 0原文

我正在尝试使用矩阵对行矢量进行元素乘法。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-02-20 07:13:47

EIGEN不会像Matlab或Numpy那样广播,例如使用matrix.Array()。rowwise() * vector.aray()

除非您明确要求它,否则 将矢量解释为对角线矩阵。

Eigen::VectorXd eK = ...;
Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> U = ...;

Eigen::MatrixXcd result = U * (eK * 1i).asDiagonal();

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.

Eigen::VectorXd eK = ...;
Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> U = ...;

Eigen::MatrixXcd result = U * (eK * 1i).asDiagonal();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文