Here is how I would do it with Eigen::Map
(assuming that real matrices can be expanded to complex using conjugate), where rows
and cols
denote the number of rows / columns:
#include <Eigen/Dense> #include <iostream> int main() { Eigen::MatrixXd A(2, 2); Eigen::MatrixXd B(2, 2); A << 1, 2, 3, 4; B << 5, 6, 7, 8; int rows = 2, cols = 2; Eigen::VectorXd vA = Eigen::Map<Eigen::VectorXd>( const_cast<double *>(A.data()), rows * cols, 1); Eigen::VectorXd vB = Eigen::Map<Eigen::VectorXd>( const_cast<double *>(B.data()), rows * cols, 1); double inner_prod = (vA.transpose() * vB).sum(); std::cout << inner_prod << std::endl; }
source share