In Eigen, a reduction is a function taking a matrix or array, and returning a single scalar value. One of the most used reductions is .sum() , returning the sum of all the coefficients inside a given matrix or array.
Example: | Output: |
---|---|
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout <<
"Here is mat.sum(): " << mat.
sum() << endl;
cout <<
"Here is mat.prod(): " << mat.
prod() << endl;
cout <<
"Here is mat.mean(): " << mat.
mean() << endl;
cout <<
"Here is mat.minCoeff(): " << mat.
minCoeff() << endl;
cout <<
"Here is mat.maxCoeff(): " << mat.
maxCoeff() << endl;
cout <<
"Here is mat.trace(): " << mat.
trace() << endl;
}
| Here is mat.sum(): 10 Here is mat.prod(): 24 Here is mat.mean(): 2.5 Here is mat.minCoeff(): 1 Here is mat.maxCoeff(): 4 Here is mat.trace(): 5 |
The trace of a matrix, as returned by the function trace()
, is the sum of the diagonal coefficients and can equivalently be computed a.diagonal().sum()
.