{"record":{"id":"aac1bce40006efee","repo":"Yalantis/uCrop","slug":"solve-lapack-library-function-dgetrf-returne","errorCode":null,"errorMessage":"solve(): LAPACK library function dgetrf_() returned error code %d.","messagePattern":"solve\\(\\): LAPACK library function dgetrf_\\(\\) returned error code (.+?)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":33017,"sourceCode":"            (*this)(k,0) = (T)x; (*this)(k,1) = (T)((v - c*x)/d);\n          }\n        return *this;\n      }\n\n      if (A._width==A._height) { // Square linear system\n#ifdef cimg_use_lapack\n        char TRANS = 'N';\n        int INFO, N = _height, LWORK = 4*N, *const IPIV = new int[N];\n        Ttfloat\n          *const lapA = new Ttfloat[N*N],\n          *const lapB = new Ttfloat[N],\n          *const WORK = new Ttfloat[LWORK];\n        cimg_forXY(A,k,l) lapA[k*N + l] = (Ttfloat)(A(k,l));\n        cimg_forX(*this,i) {\n          cimg_forY(*this,j) lapB[j] = (Ttfloat)((*this)(i,j));\n          cimg::getrf(N,lapA,IPIV,INFO);\n          if (INFO)\n            cimg::warn(_cimg_instance\n                       \"solve(): LAPACK library function dgetrf_() returned error code %d.\",\n                       cimg_instance,\n                       INFO);\n          else {\n            cimg::getrs(TRANS,N,lapA,IPIV,lapB,INFO);\n            if (INFO)\n              cimg::warn(_cimg_instance\n                         \"solve(): LAPACK library function dgetrs_() returned error code %d.\",\n                         cimg_instance,\n                         INFO);\n          }\n          if (!INFO) cimg_forY(*this,j) (*this)(i,j) = (T)(lapB[j]); else cimg_forY(*this,j) (*this)(i,j) = (T)0;\n        }\n        delete[] IPIV; delete[] lapA; delete[] lapB; delete[] WORK;\n#else\n        CImg<Ttfloat> lu(A,false);\n        CImg<Ttfloat> indx;\n        bool d;","sourceCodeStart":32999,"sourceCodeEnd":33035,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32999-L33035","documentation":"CImg::solve() solves A*X = B by LU-factoring A with LAPACK dgetrf_(); a nonzero INFO means the factorization failed because the matrix is singular (zero pivot) or an argument was invalid. CImg warns, leaves the solution column zeroed, and continues rather than throwing.","triggerScenarios":"Calling solve() on a square system whose coefficient matrix A is singular or rank-deficient; passing mismatched dimensions between this (B) and A.","commonSituations":"Solving systems with duplicated/dependent equations; poorly conditioned matrices from noisy data; constructing A with wrong dimensions so LAPACK sees an invalid N.","solutions":["Check that A is square and that B's rows equal A's rows before calling solve()","Test A for singularity (determinant/rank) and regularize (A + eps*I) if needed","Use double precision to survive near-singular systems","Fall back to the least-squares path (sgels) or a pseudo-inverse when the system has no unique solution","Verify the solution is not the zero-filled fallback before using it"],"exampleFix":"// before\nCImg<float> x = B.solve(A);\n// after\nif (std::fabs(A.determinant()) < 1e-12) {\n  for (int i = 0; i < A.width(); ++i) A(i,i) += 1e-8f; // Tikhonov regularization\n}\nCImg<float> x = B.solve(A);","handlingStrategy":"validation","validationCode":"bool solvable(const CImg<float>& A, const CImg<float>& B) {\n  return A.width() == A.height() && B.height() == A.height() &&\n         std::fabs(A.determinant()) > 1e-12f;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check dimensions and singularity before solve()","Regularize singular systems instead of solving directly","Compare the solution against the zero-fill fallback","Prefer least-squares paths for over-determined systems"],"tags":["cimg","lapack","linear-system","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"}