Lesson15 Reductions

来源:互联网 发布:梦龙网络计划2016 编辑:程序博客网 时间:2024/05/21 07:50

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():       10Here is mat.prod():      24Here is mat.mean():      2.5Here is mat.minCoeff():  1Here is mat.maxCoeff():  4Here 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().

0 0
原创粉丝点击