{"record":{"id":"6e097f3172600edb","repo":"Yalantis/uCrop","slug":"operator-invalid-multiplication-of-instance-by","errorCode":null,"errorMessage":"operator*(): Invalid multiplication of instance by specified matrix (%u,%u,%u,%u,%p).","messagePattern":"operator\\*\\(\\): Invalid multiplication of instance by specified matrix \\(%u,%u,%u,%u,%p\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":14450,"sourceCode":"    /**\n       Similar to operator*=(const char*), except that it returns a new image instance instead of operating in-place.\n       The pixel type of the returned image may be a superset of the initial pixel type \\c T, if necessary.\n    **/\n    CImg<Tfloat> operator*(const char *const expression) const {\n      return CImg<Tfloat>(*this,false)*=expression;\n    }\n\n    //! Multiplication operator.\n    /**\n       Similar to operator*=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.\n       The pixel type of the returned image may be a superset of the initial pixel type \\c T, if necessary.\n    **/\n    template<typename t>\n    CImg<_cimg_Tt> operator*(const CImg<t>& img) const {\n      typedef _cimg_Ttdouble Ttdouble;\n      typedef _cimg_Tt Tt;\n      if (_width!=img._height || _depth!=1 || _spectrum!=1 || img._depth!=1 || img._spectrum!=1)\n        throw CImgArgumentException(_cimg_instance\n                                    \"operator*(): Invalid multiplication of instance by specified \"\n                                    \"matrix (%u,%u,%u,%u,%p).\",\n                                    cimg_instance,\n                                    img._width,img._height,img._depth,img._spectrum,img._data);\n      CImg<Tt> res(img._width,_height);\n\n      // Check for common cases to optimize.\n      if (img._width==1) { // Matrix * Vector\n        if (_height==1) switch (_width) { // Vector^T * Vector\n          case 1 :\n            res[0] = (Tt)((Ttdouble)_data[0]*img[0]);\n            return res;\n          case 2 :\n            res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1]);\n            return res;\n          case 3 :\n            res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +\n                          (Ttdouble)_data[2]*img[2]);","sourceCodeStart":14432,"sourceCodeEnd":14468,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L14432-L14468","documentation":"CImg's operator* between two CImg instances is matrix multiplication: instance (treated as a column vector) times img (treated as a matrix). It requires this image to be a single-column vector (_height==1, _depth==1, _spectrum==1) and img to be a 2D matrix (_depth==1, _spectrum==1) whose row count (_height) equals the vector's _width. Any deviation throws this argument exception.","triggerScenarios":"Calling vec * matrix when vec has height != 1, either image has depth or spectrum != 1, or vec._width != matrix._height; also element-wise '*' confusion where users expected per-pixel multiplication (which is mul()).","commonSituations":"Linear-algebra code with transposed vectors (row vector instead of column vector); RGB images (spectrum=3) or volumes (depth>1) passed as operands; dimension mismatch after resizing one operand.","solutions":["Ensure the left operand is a true column vector: height==1, depth==1, spectrum==1, with width == img._height.","Ensure the right operand is 2D: img._depth==1 and img._spectrum==1.","For element-wise pixel multiplication use img.mul(other) instead of operator*.","get_transpose() or reshape the operand so dimensions satisfy vec(w,1,1,1) * mat(w,h,1,1)."],"exampleFix":"// before\nCImg<float> v(3,3);        // actually a 3x3, not a vector\nCImg<float> m(3,3);\nCImg<float> r = v * m;     // throws\n// after\nCImg<float> v(3,1);        // column vector 3x1\nCImg<float> m(3,3);\nCImg<float> r = v * m;     // OK","handlingStrategy":"validation","validationCode":"if (vec._height == 1 && vec._depth == 1 && vec._spectrum == 1 &&\n    m._depth == 1 && m._spectrum == 1 && vec._width == m._height) {\n  CImg<Tt> r = vec * m;\n}","typeGuard":"bool isColumnVector = img.height() == 1 && img.depth() == 1 && img.spectrum() == 1;\nbool isMatrix = m.depth() == 1 && m.spectrum() == 1;","tryCatchPattern":"try {\n  CImg<float> r = vec * m;\n} catch (CImgArgumentException& e) {\n  // fall back to mul() or fix dimensions\n}","preventionTips":["Remember operator* is matrix product; use mul() for element-wise multiplication.","Keep vectors as Nx1 column vectors and check width == matrix.height() before multiplying.","Assert operand dimensions near call sites handling images with spectrum>1."],"tags":["cimg","matrix-multiplication","dimension-mismatch","linear-algebra"],"backgroundTag":"tensor-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"}