{"record":{"id":"8b2af944c38d04dd","repo":"Yalantis/uCrop","slug":"invert-specified-lambda-g-should-be-0","errorCode":null,"errorMessage":"invert(): Specified lambda (%g) should be >=0.","messagePattern":"invert\\(\\): Specified lambda \\(%g\\) should be >=0\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32837,"sourceCode":"    CImg<_cimg_Tt> get_cross(const CImg<t>& img) const {\n      return CImg<_cimg_Tt>(*this).cross(img);\n    }\n\n    //! Invert the instance image, viewed as a matrix.\n    /**\n       If the instance matrix is not square, the Moore-Penrose pseudo-inverse is computed instead.\n       \\param use_LU Choose the inverting algorithm. Can be:\n       - \\c true: LU solver (faster but sometimes less precise).\n       - \\c false: SVD solver (more precise but slower).\n       \\param lambda is used only in the Moore-Penrose pseudoinverse for estimating A^t.(A^t.A + lambda.Id)^-1.\n    **/\n    CImg<T>& invert(const bool use_LU=false, const float lambda=0) {\n      if (_depth!=1 || _spectrum!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"invert(): Instance is not a matrix.\",\n                                    cimg_instance);\n      if (lambda<0)\n        throw CImgArgumentException(_cimg_instance\n                                    \"invert(): Specified lambda (%g) should be >=0.\",\n                                    cimg_instance);\n\n      if (_width!=_height) return get_invert(use_LU,lambda).move_to(*this); // Non-square matrix: Pseudoinverse\n\n      // Square matrix.\n      const double dete = _width>3?-1.:det();\n      if (dete!=0. && _width==2) {\n        const double\n          a = _data[0], c = _data[1],\n          b = _data[2], d = _data[3];\n        _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete);\n        _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete);\n      } 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];","sourceCodeStart":32819,"sourceCodeEnd":32855,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32819-L32855","documentation":"CImg::invert() accepts a lambda parameter used only for the Moore-Penrose pseudoinverse of non-square matrices, as a regularization term in A^t(A^t.A + lambda.Id)^-1. It throws CImgArgumentException when lambda is negative, because a negative regularization constant is mathematically invalid.","triggerScenarios":"Calling img.invert(use_LU, lambda) with a negative float, e.g. an uninitialized/NaN-adjacent variable, a computed damping term that underflowed negative, or swapped arguments.","commonSituations":"Computing lambda from data (e.g. scaled noise estimate) that can be negative; typo passing -1 as a sentinel; confusing lambda with another tuning parameter.","solutions":["Pass lambda >= 0 (or use the default 0).","Clamp: img.invert(use_LU, std::max(0.0f, lambda)).","Use the default overload img.invert() if no regularization is needed.","Note the argument order: invert(use_LU, lambda)."],"exampleFix":"// before\nimg.invert(false, -1e-3f); // negative lambda\n// after\nfloat lambda = std::max(0.0f, computedLambda);\nimg.invert(false, lambda);","handlingStrategy":"validation","validationCode":"if (!(lambda >= 0)) lambda = 0; // guard NaN and negatives\nimg.invert(useLU, lambda);","typeGuard":null,"tryCatchPattern":"try { img.invert(useLU, lambda); } catch (CImgArgumentException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Clamp computed regularization values to >= 0","Use the default lambda=0 unless doing pseudoinverse regularization","Double-check argument order invert(use_LU, lambda)"],"tags":["cimg","matrix","pseudoinverse","invalid-argument"],"backgroundTag":"argument-out-of-range","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"}