This bugzilla service is closed. All entries have been migrated to

Bug 1088

Summary: Sparse setIdentity incorrect if matrix not compressed
Product: Eigen Reporter: Iain <iainwaugh.spam>
Component: SparseAssignee: Nobody <eigen.nobody>
Severity: Wrong Result CC: chtz, gael.guennebaud
Priority: Normal    
Version: 3.2   
Hardware: x86 - 64-bit   
OS: Linux   

Description Iain 2015-10-16 13:13:14 UTC
Using setIdentity() on a sparse matrix leads to incorrect result if there are off-trace terms, i.e. there are still off-trace non-zero terms after setIdentity(). The setIdentity() function does not check if the matrix is in compressed form. Compressing the matrix seems to fix this issue on the test below.


#include <iostream>
#include <Eigen/Sparse>

using namespace std;
using namespace Eigen;

int main()
    //Create 5x5 identity matrix
    SparseMatrix<float> tmp(5,5);
    cout<<"Correct 5x5 identity:\n\n"<<tmp<<endl;        // CORRECT

    //Add a value off the trace
    tmp.coeffRef(3,1) = 5.0;
    //tmp.makeCompressed();                              // UNCOMMENT TO FIX
    cout<<"5x5 identity with additional off-trace value:\n\n"<<tmp<<endl;

    //Reset to the identity
    cout<<"Incorrect 5x5 identity:\n\n"<<tmp<<endl;      // INCORRECT !!!!!!

    return 0;
Comment 1 Gael Guennebaud 2015-10-25 21:10:17 UTC
Thank you for the report.

Comment 2 Nobody 2019-12-04 15:03:02 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: