{"record":{"id":"7de1d30203f33ff5","repo":"Yalantis/uCrop","slug":"eigen-instance-is-not-a-square-matrix","errorCode":null,"errorMessage":"eigen(): Instance is not a square matrix.","messagePattern":"eigen\\(\\): Instance is not a square matrix\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":33147,"sourceCode":"    }\n\n    //! Solve a tridiagonal system of linear equations \\newinstance.\n    template<typename t>\n    CImg<_cimg_Ttfloat> get_solve_tridiagonal(const CImg<t>& A) const {\n      return CImg<_cimg_Ttfloat>(*this,false).solve_tridiagonal(A);\n    }\n\n    //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.\n    /**\n       \\param[out] val Vector of the estimated eigenvalues, in decreasing order.\n       \\param[out] vec Matrix of the estimated eigenvectors, sorted by columns.\n    **/\n    template<typename t>\n    const CImg<T>& eigen(CImg<t>& val, CImg<t> &vec) const {\n      if (is_empty()) { val.assign(); vec.assign(); }\n      else {\n        if (_width!=_height || _depth>1 || _spectrum>1)\n          throw CImgInstanceException(_cimg_instance\n                                      \"eigen(): Instance is not a square matrix.\",\n                                      cimg_instance);\n\n        if (val.size()<(ulongT)_width) val.assign(1,_width);\n        if (vec.size()<(ulongT)_width*_width) vec.assign(_width,_width);\n        switch (_width) {\n        case 1 : { val[0] = (t)(*this)[0]; vec[0] = (t)1; } break;\n        case 2 : {\n          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,","sourceCodeStart":33129,"sourceCodeEnd":33165,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L33129-L33165","documentation":"CImg::eigen() computes eigenvalues and eigenvectors of the image viewed as a matrix, which requires a non-empty, single-channel 2D square matrix (_width==_height, _depth<=1, _spectrum<=1). It throws CImgInstanceException when a non-square or multi-channel/volumetric instance is passed.","triggerScenarios":"Calling img.eigen(val,vec) on a rectangular matrix, on a color image (spectrum>1), or on volumetric data (depth>1).","commonSituations":"Computing eigen-decompositions of covariance images that came out rectangular; passing an RGB image instead of a channel; using eigen() on symmetric-input assumptions but giving non-square data.","solutions":["Ensure the instance is square: img.assign(n,n,1,1).","Extract a channel/plane first: img.get_channel(0).eigen(val,vec).","For non-square matrices, use SVD instead: img.get_SVD(U,S,V) or symmetric_eigen() for square symmetric matrices."],"exampleFix":"// before\nCImg<float> m(4,5); // non-square -> throws\nm.eigen(val,vec);\n// after\nCImg<float> m(5,5);\nif (m.width()==m.height() && m.depth()<=1 && m.spectrum()<=1)\n  m.eigen(val,vec);","handlingStrategy":"validation","validationCode":"if (!img.is_empty() && img.width()==img.height() && img.depth()<=1 && img.spectrum()<=1) img.eigen(val, vec);","typeGuard":"bool isSquareMatrix(const CImg<T>& m){ return !m.is_empty() && m.width()==m.height() && m.depth()<=1 && m.spectrum()<=1; }","tryCatchPattern":"try { img.eigen(val, vec); } catch (CImgInstanceException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Verify squareness before eigen-decomposition","Use get_SVD() for non-square matrices","Extract a single channel from color images first","Prefer symmetric_eigen() when the matrix is known symmetric"],"tags":["cimg","eigenvalues","matrix","dimensions"],"backgroundTag":"shape-mismatch","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"}