{"record":{"id":"b952b2ac7a239509","repo":"Yalantis/uCrop","slug":"solve-instance-and-specified-matrix-u-u-u","errorCode":null,"errorMessage":"solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have incompatible dimensions.","messagePattern":"solve\\(\\): Instance and specified matrix \\(%u,%u,%u,%u,%p\\) have incompatible dimensions\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32968,"sourceCode":"      cimg_forX(V,x) {\n        const Tfloat s = S(x), invs = lambda?1/(lambda + s):s>eps?1/s:0;\n        cimg_forY(V,y) V(x,y)*=invs;\n      }\n      return V*U.transpose();\n    }\n\n    //! Solve a system of linear equations.\n    /**\n       \\param A Matrix of the linear system.\n       \\param use_LU In case of non square system (least-square solution),\n                     choose between SVD (\\c false) or LU (\\c true) solver.\n                     LU solver is faster for large matrices, but numerically less stable.\n       \\note Solve \\c AX = B where \\c B=*this.\n    **/\n    template<typename t>\n    CImg<T>& solve(const CImg<t>& A, const bool use_LU=false) {\n      if (_depth!=1 || _spectrum!=1 || _height!=A._height || A._depth!=1 || A._spectrum!=1)\n        throw CImgArgumentException(_cimg_instance\n                                    \"solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have \"\n                                    \"incompatible dimensions.\",\n                                    cimg_instance,\n                                    A._width,A._height,A._depth,A._spectrum,A._data);\n      typedef _cimg_Ttfloat Ttfloat;\n\n      if (A.size()==1) return (*this)/=A[0];\n      if (A._width==2 && A._height==2 && _height==2) { // 2x2 linear system\n        const double a = (double)A[0], b = (double)A[1], c = (double)A[2], d = (double)A[3],\n          fa = std::fabs(a), fb = std::fabs(b), fc = std::fabs(c), fd = std::fabs(d),\n          det = a*d - b*c, fM = cimg::max(fa,fb,fc,fd);\n        if (fM==fa)\n          cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))\n          cimg_forX(*this,k) {\n            const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), y = (a*v - c*u)/det;\n            (*this)(k,0) = (T)((u - b*y)/a); (*this)(k,1) = (T)y;\n          } else if (fM==fc)\n          cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))","sourceCodeStart":32950,"sourceCodeEnd":32986,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32950-L32986","documentation":"CImg::solve() solves AX = B where B is *this and A is the argument; for a valid linear system both must be 2D single-channel matrices and share the same number of rows (_height==A._height). It throws CImgArgumentException listing A's dimensions when any of these preconditions fails.","triggerScenarios":"Calling b.solve(A) where A and b have different heights, or where either operand has depth>1 or spectrum>1.","commonSituations":"Building the RHS vector with the wrong number of rows for the system matrix; passing a color or volumetric image as A or B; mixing a column-vector B (n x 1) with an A of different height.","solutions":["Ensure A and b are both (w,h,1,1) with matching h: b.assign(A._height, k).","Check dims before solving: if (A.height()==b.height() && A.depth()==1 && A.spectrum()==1) b.solve(A);","Use get_solve() for a non-mutating variant and verify dimensions on the copy first."],"exampleFix":"// before\nCImg<float> A(3,4); // 4 rows\nCImg<float> b(1,3); // 3 rows -> throws\nb.solve(A);\n// after\nCImg<float> b(1,4); // matches A._height\nb.solve(A);","handlingStrategy":"validation","validationCode":"bool solvable = b.depth()==1 && b.spectrum()==1 && A.depth()==1 && A.spectrum()==1 && b.height()==A.height(); if (solvable) b.solve(A);","typeGuard":"bool solvableSystem(const CImg<T>& A, const CImg<T>& b){ return A.depth()==1 && A.spectrum()==1 && b.depth()==1 && b.spectrum()==1 && b.height()==A.height(); }","tryCatchPattern":"try { b.solve(A); } catch (CImgArgumentException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Build B with exactly A.height() rows before solving","Never pass color/volumetric images to solve()","Assert dimensions at the boundary where A and B are constructed"],"tags":["cimg","linear-system","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-14T11:17:12.474Z"}