{"record":{"id":"b82e801b6dcabf97","repo":"Yalantis/uCrop","slug":"invert-lapack-function-dgetrf-returned-error","errorCode":null,"errorMessage":"invert(): LAPACK function dgetrf_() returned error code %d.","messagePattern":"invert\\(\\): LAPACK function dgetrf_\\(\\) returned error code (.+?)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32869,"sourceCode":"      } else if (dete!=0. && _width==3) {\n        const double\n          a = _data[0], d = _data[1], g = _data[2],\n          b = _data[3], e = _data[4], h = _data[5],\n          c = _data[6], f = _data[7], i = _data[8];\n        _data[0] = (T)((i*e - f*h)/dete), _data[1] = (T)((g*f - i*d)/dete), _data[2] = (T)((d*h - g*e)/dete);\n        _data[3] = (T)((h*c - i*b)/dete), _data[4] = (T)((i*a - c*g)/dete), _data[5] = (T)((g*b - a*h)/dete);\n        _data[6] = (T)((b*f - e*c)/dete), _data[7] = (T)((d*c - a*f)/dete), _data[8] = (T)((a*e - d*b)/dete);\n      } else {\n\n#ifdef cimg_use_lapack\n        int INFO = (int)use_LU, N = _width, LWORK = 4*N, *const IPIV = new int[N];\n        Tfloat\n          *const lapA = new Tfloat[N*N],\n          *const WORK = new Tfloat[LWORK];\n        cimg_forXY(*this,k,l) lapA[k*N + l] = (Tfloat)((*this)(k,l));\n        cimg::getrf(N,lapA,IPIV,INFO);\n        if (INFO)\n          cimg::warn(_cimg_instance\n                     \"invert(): LAPACK function dgetrf_() returned error code %d.\",\n                     cimg_instance,\n                     INFO);\n        else {\n          cimg::getri(N,lapA,IPIV,WORK,LWORK,INFO);\n          if (INFO)\n            cimg::warn(_cimg_instance\n                       \"invert(): LAPACK function dgetri_() returned error code %d.\",\n                       cimg_instance,\n                       INFO);\n        }\n        if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)(lapA[k*N + l]); else fill(0);\n        delete[] IPIV; delete[] lapA; delete[] WORK;\n#else\n        if (use_LU) { // LU solver\n          CImg<Tfloat> A(*this,false), indx;\n          bool d;\n          A._LU(indx,d);","sourceCodeStart":32851,"sourceCodeEnd":32887,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32851-L32887","documentation":"CImg::invert() with LU decomposition calls LAPACK dgetrf_() to factor the matrix before inverting; a nonzero INFO from dgetrf_ means the LU factorization failed, typically because a diagonal pivot was exactly zero (singular matrix). On failure CImg warns and fills the matrix with zeros instead of throwing.","triggerScenarios":"Calling invert(use_LU=true) on a square matrix that is singular or numerically rank-deficient; also possible if N exceeds LAPACK integer limits.","commonSituations":"Inverting covariance/geometry matrices built from degenerate input data (collinear points, duplicate rows); float32 precision issues making an ill-conditioned matrix effectively singular.","solutions":["Check the determinant or rank of the matrix before inverting; reject/regularize singular inputs","Use double-precision data (CImg<double>) so near-singular matrices still factor","Add a small regularization term (e.g. A + eps*I) before inverting","Handle the zero-filled result: check INFO behavior and fall back to a pseudo-inverse (e.g. solve via SVD/symmetric eigen)","Avoid inverting at all — solve A*x=b with solve() instead of computing A^-1"],"exampleFix":"// before\nCImg<float> inv = A.invert(true);\n// after\nif (std::fabs(A.determinant()) < 1e-10f) {\n  // singular: regularize instead of inverting\n  for (int i = 0; i < A.width(); ++i) A(i,i) += 1e-6f;\n}\nCImg<float> inv = A.invert(true);","handlingStrategy":"validation","validationCode":"bool invertible(const CImg<float>& A) {\n  if (A.width() != A.height() || A.width() == 0) return false;\n  return std::fabs(A.determinant()) > 1e-10f;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never invert without checking determinant/rank first","Prefer solve() over computing an explicit inverse","Regularize ill-conditioned matrices (A + eps*I)","Use double precision for near-singular data"],"tags":["cimg","lapack","linear-algebra","singular-matrix"],"backgroundTag":"lapack-solver-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}