{"record":{"id":"71b8d8b483e633d6","repo":"Yalantis/uCrop","slug":"eigen-eigenvalues-computation-of-general-matric","errorCode":null,"errorMessage":"eigen(): Eigenvalues computation of general matrices is limited to 2x2 matrices.","messagePattern":"eigen\\(\\): Eigenvalues computation of general matrices is limited to 2x2 matrices\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":33174,"sourceCode":"          const double a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3], e = a + d;\n          double f = e*e - 4*(a*d - b*c);\n          if (f<0) cimg::warn(_cimg_instance\n                              \"eigen(): Complex eigenvalues found.\",\n                              cimg_instance);\n          f = std::sqrt(f);\n          const double\n            l1 = 0.5*(e - f),\n            l2 = 0.5*(e + f),\n            b2 = b*b,\n            norm1 = std::sqrt(cimg::sqr(l2 - a) + b2),\n            norm2 = std::sqrt(cimg::sqr(l1 - a) + b2);\n          val[0] = (t)l2;\n          val[1] = (t)l1;\n          if (norm1>0) { vec(0,0) = (t)(b/norm1); vec(0,1) = (t)((l2 - a)/norm1); } else { vec(0,0) = 1; vec(0,1) = 0; }\n          if (norm2>0) { vec(1,0) = (t)(b/norm2); vec(1,1) = (t)((l1 - a)/norm2); } else { vec(1,0) = 1; vec(1,1) = 0; }\n        } break;\n        default :\n          throw CImgInstanceException(_cimg_instance\n                                      \"eigen(): Eigenvalues computation of general matrices is limited \"\n                                      \"to 2x2 matrices.\",\n                                      cimg_instance);\n        }\n      }\n      return *this;\n    }\n\n    //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.\n    /**\n       \\return A list of two images <tt>[val; vec]</tt>, whose meaning is similar as in eigen(CImg<t>&,CImg<t>&) const.\n    **/\n    CImgList<Tfloat> get_eigen() const {\n      CImgList<Tfloat> res(2);\n      eigen(res[0],res[1]);\n      return res;\n    }\n","sourceCodeStart":33156,"sourceCodeEnd":33192,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L33156-L33192","documentation":"CImg's eigen() computes eigenvalues/eigenvectors of a matrix treated as a general (non-symmetric) matrix, but the closed-form algorithm shipped in this header only implements the 2x2 case. For any other width the library throws CImgInstanceException rather than iterating numerically.","triggerScenarios":"Calling CImg<T>::eigen()/get_eigen() (or symmetric_eigen dispatch path) on an instance whose _width is not 2 (e.g. a 3x3 or NxN general matrix).","commonSituations":"Using eigen() on a 3x3 covariance or rotation matrix expecting LAPACK-like behavior; confusing eigen() with symmetric_eigen(), which supports arbitrary square sizes; upgrading CImg versions where general-N eigen support is absent.","solutions":["Restrict general eigen() calls to 2x2 matrices, or reshape/decompose your problem into 2x2 blocks","For symmetric matrices of any size, call symmetric_eigen(val, vec) instead, which supports NxN","Implement an external eigensolver (e.g. Eigen, LAPACK via JNI) for general NxN matrices","Guard the call: if (m.width() != 2) use an alternative solver before calling eigen()"],"exampleFix":"// before\nCImg<float> A(3,3); CImg<float> val, vec;\nA.eigen(val, vec); // throws\n// after\nCImg<float> val, vec;\nif (A.width() == 2 && A.height() == 2) A.eigen(val, vec);\nelse A.symmetric_eigen(val, vec); // for symmetric NxN","handlingStrategy":"validation","validationCode":"if (m.width() == 2 && m.height() == 2) { m.eigen(val, vec); } else { /* use symmetric_eigen or external solver */ }","typeGuard":"bool isGeneralEigenSupported(const CImg<T>& m) { return m.width() == 2 && m.height() == 2 && m.depth() == 1 && m.spectrum() == 1; }","tryCatchPattern":"try { m.eigen(val, vec); } catch (CImgInstanceException& e) { /* fall back to symmetric_eigen or external library */ }","preventionTips":["Only call eigen() on 2x2 general matrices","Prefer symmetric_eigen() for symmetric NxN matrices","Document the 2x2 limitation where eigen() is wrapped"],"tags":["cimg","eigenvalues","linear-algebra","unsupported-matrix-size"],"backgroundTag":"unsupported-operation","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}