{"record":{"id":"a7beef81aaa06fbb","repo":"Yalantis/uCrop","slug":"det-instance-is-not-a-square-matrix","errorCode":null,"errorMessage":"det(): Instance is not a square matrix.","messagePattern":"det\\(\\): Instance is not a square matrix\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32579,"sourceCode":"    //! Compute the trace of the image, viewed as a matrix.\n    /**\n     **/\n    double trace() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"trace(): Empty instance.\",\n                                    cimg_instance);\n      double res = 0;\n      cimg_forX(*this,k) res+=(double)(*this)(k,k);\n      return res;\n    }\n\n    //! Compute the determinant of the image, viewed as a matrix.\n    /**\n     **/\n    double det() const {\n      if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"det(): Instance is not a square matrix.\",\n                                    cimg_instance);\n\n      switch (_width) {\n      case 1 : return (double)((*this)(0,0));\n      case 2 : return (double)((*this)(0,0))*(double)((*this)(1,1)) - (double)((*this)(0,1))*(double)((*this)(1,0));\n      case 3 : {\n        const double\n          a = (double)_data[0], d = (double)_data[1], g = (double)_data[2],\n          b = (double)_data[3], e = (double)_data[4], h = (double)_data[5],\n          c = (double)_data[6], f = (double)_data[7], i = (double)_data[8];\n        return i*a*e - a*h*f - i*b*d + b*g*f + c*d*h - c*g*e;\n      }\n      default : {\n        CImg<Tfloat> lu(*this,false);\n        CImg<uintT> indx;\n        bool d;\n        lu._LU(indx,d);","sourceCodeStart":32561,"sourceCodeEnd":32597,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32561-L32597","documentation":"CImg::det() computes the determinant of the image viewed as a matrix, which requires a 2D square matrix: _width==_height and _depth==1 and _spectrum==1. The library throws CImgInstanceException when the instance is empty or its dimensions do not form a single-channel square matrix, because the determinant is undefined otherwise.","triggerScenarios":"Calling img.det() on a non-square width x height image, on an image with depth>1 (volumetric) or spectrum>1 (multi-channel color), or on an empty instance.","commonSituations":"Passing a color image (spectrum=3) instead of extracting a single channel; passing a volumetric image; passing an N x M rectangular matrix; forgetting that CImg treats an image as width x height x depth x spectrum, not rows x columns.","solutions":["Ensure the instance is square and single-channel before calling det(): img.assign(n,n,1,1).","Extract one channel/plane first: img.get_channel(0).det() or img.get_slice(0).det().","For rectangular matrices, use a decomposition-based approach (e.g. SVD via get_SVD()) instead of det()."],"exampleFix":"// before\nCImg<float> img(3,4); // rectangular -> det() throws\ndouble d = img.det();\n// after\nif (img.width()==img.height() && img.depth()==1 && img.spectrum()==1)\n  double d = img.det();","handlingStrategy":"validation","validationCode":"bool isSquareMatrix = !img.is_empty() && img.width()==img.height() && img.depth()==1 && img.spectrum()==1; if (!isSquareMatrix) img = img.get_channel(0).get_crop(0,0,img.height()-1,img.height()-1);","typeGuard":"bool isSquareMatrix(const CImg<T>& m){ return !m.is_empty() && m.width()==m.height() && m.depth()==1 && m.spectrum()==1; }","tryCatchPattern":"try { double d = img.det(); } catch (CImgInstanceException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Remember CImg stores images as width x height x depth x spectrum; matrix ops need (n,n,1,1)","Extract single channels from color images before matrix math","Use symmetric_eigen/SVD for rectangular data instead of det()"],"tags":["cimg","matrix","determinant","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-14T05:17:10.506Z"}