Skip to content

Implement numerically stable summation methods

Submitted by Lakshay Garg

Assigned to Nobody

Link to original bugzilla bug (#1438)
Version: 3.3 (current stable)

Description

Currently, when asked to sum a tensor, the library naively adds the numbers from left-to-right but this approach can lead to loss of precision in certain cases (example shown below). To prevent this loss of precision, I would like to propose the implementation of summation methods like Kahan or pairwise summation.

#include <iostream>
#include <Eigen/Dense>

int main() {
using std::cout;

Eigen::Matrix2f mat1, mat2;

mat1 << 100000, 0.001, -100000, 0.001;
cout << "sum1: " << mat1.sum() << '\n'; // expected: 0.002, output: 0

mat2 << 0.001, 0.001, -1, 1;
cout << "sum2: " << mat2.sum() << '\n'; // expected: 0.002, output: 0.002
}

Edited by Eigen Bugzilla