{"record":{"id":"9f9d31b77c25e2aa","repo":"Yalantis/uCrop","slug":"solve-tridiagonal-instance-and-tridiagonal-matr","errorCode":null,"errorMessage":"solve_tridiagonal(): Instance and tridiagonal matrix (%u,%u,%u,%u,%p) have incompatible dimensions.","messagePattern":"solve_tridiagonal\\(\\): Instance and tridiagonal 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":33113,"sourceCode":"        sum = (*this)(i);\n        for (int j = i + 1; j<N; ++j) sum-=A(j,i)*(*this)(j);\n        (*this)(i) = (T)(sum/A(i,i));\n      }\n      return *this;\n    }\n\n    //! Solve a tridiagonal system of linear equations.\n    /**\n       \\param A Coefficients of the tridiagonal system.\n       A is a tridiagonal matrix A = [ b0,c0,0,...; a1,b1,c1,0,... ; ... ; ...,0,aN,bN ],\n       stored as a 3 columns matrix\n       \\note Solve AX=B where \\c B=*this, using the Thomas algorithm.\n    **/\n    template<typename t>\n    CImg<T>& solve_tridiagonal(const CImg<t>& A) {\n      const unsigned int siz = (unsigned int)size();\n      if (A._width!=3 || A._height!=siz)\n        throw CImgArgumentException(_cimg_instance\n                                    \"solve_tridiagonal(): Instance and tridiagonal matrix \"\n                                    \"(%u,%u,%u,%u,%p) have incompatible dimensions.\",\n                                    cimg_instance,\n                                    A._width,A._height,A._depth,A._spectrum,A._data);\n      typedef _cimg_Ttfloat Ttfloat;\n      const Ttfloat eps = 1e-4f;\n      CImg<Ttfloat> B = A.get_column(1), V(*this,false);\n      for (int i = 1; i<(int)siz; ++i) {\n        const Ttfloat m = A(0,i)/(B[i - 1]?B[i - 1]:eps);\n        B[i] -= m*A(2,i - 1);\n        V[i] -= m*V[i - 1];\n      }\n      (*this)[siz - 1] = (T)(V[siz - 1]/(B[siz - 1]?B[siz - 1]:eps));\n      for (int i = (int)siz - 2; i>=0; --i) (*this)[i] = (T)((V[i] - A(2,i)*(*this)[i + 1])/(B[i]?B[i]:eps));\n      return *this;\n    }\n\n    //! Solve a tridiagonal system of linear equations \\newinstance.","sourceCodeStart":33095,"sourceCodeEnd":33131,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L33095-L33131","documentation":"CImg::solve_tridiagonal() solves AX=B using the Thomas algorithm, where A is encoded as an n x 3 matrix (three diagonals as columns) and B is *this of size n. It throws CImgArgumentException when A._width!=3 or A._height!=this->size(), i.e. the tridiagonal encoding or system size is wrong.","triggerScenarios":"Passing A as a full n x n matrix instead of the n x 3 diagonal encoding; passing an n x 3 matrix whose n differs from the instance's total element count; passing an empty A.","commonSituations":"Misunderstanding the compact storage format (sub/diag/super diagonals in the 3 columns); building B as a vector whose length does not match n.","solutions":["Repack A into the required n x 3 layout (columns = lower diagonal, main diagonal, upper diagonal).","Make the instance (B) contain exactly A._height elements.","Check before calling: if (A.width()==3 && (ulongT)A.height()==b.size()) b.solve_tridiagonal(A);"],"exampleFix":"// before\nCImg<float> A(n,n); // full matrix -> throws\nb.solve_tridiagonal(A);\n// after\nCImg<float> A(n,3); // 3 diagonals per row\nA(0,1)=a1; A(k,0)=sub; A(k,1)=diag; A(k,2)=super; ...\nb.solve_tridiagonal(A);","handlingStrategy":"validation","validationCode":"bool ok = (A.width()==3) && (A.height()==(int)b.size()) && b.depth()==1 && b.spectrum()==1; if (ok) b.solve_tridiagonal(A);","typeGuard":"bool isTridiagonalEncoding(const CImg<T>& A, const CImg<T>& b){ return A.width()==3 && (ulongT)A.height()==b.size(); }","tryCatchPattern":"try { b.solve_tridiagonal(A); } catch (CImgArgumentException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Encode the tridiagonal matrix as n x 3 (sub, main, super diagonals), never as a full n x n matrix","Match B's element count to the number of rows of the encoded A","Document the compact storage convention in team code"],"tags":["cimg","tridiagonal","thomas-algorithm","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"}