Yalantis/uCrop · error · CImgArgumentException

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s %s%s (of t

Error message

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s %s%s (of type '%s') cannot be considered as a square matrix, in expression '%s'.

What it means

CImg's math parser requires operands/arguments used as matrices to be square (n*n == siz). When a scalar or vector operand's size is not a perfect square where a square matrix is required, this CImgArgumentException is thrown during parsing. The message identifies which operand (left-hand/right-hand, or First/Second/... argument for function 'F' operators) and its actual type.

Source

Thrown at ucrop/src/main/jni/CImg.h:25216

                                      "CImg<%s>::%s: %s%s Specified index '%s' is NaN.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s_arg);
        }
      }

      // Check a matrix is square.
      void check_matrix_square(const unsigned int arg, const unsigned int n_arg,
                               char *const ss, char *const se, const char saved_char) {
        _cimg_mp_check_type(arg,n_arg,2,0);
        const unsigned int
          siz = size(arg),
          n = (unsigned int)cimg::round(std::sqrt((float)siz));
        if (n*n!=siz) {
          const char *s_arg;
          if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
          else s_arg = !n_arg?"":n_arg==1?"First":n_arg==2?"Second":n_arg==3?"Third":"One";
          char *s0;
          _cimg_mp_strerr;
          throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                      "CImg<%s>::%s: %s%s %s%s (of type '%s') "
                                      "cannot be considered as a square matrix, in expression '%s'.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
                                      s_arg,*s_op=='F'?(*s_arg?" argument":" Argument"):(*s_arg?" operand":" Operand"),
                                      s_type(arg)._data,s0);
        }
      }

      // Check type compatibility for one argument.
      // Bits of 'mode' tells what types are allowed:
      // { 1 = scalar | 2 = vectorN }.
      // If 'N' is not zero, it also restricts the vectors to be of size N only.
      void check_type(const unsigned int arg, const unsigned int n_arg,
                      const unsigned int mode, const unsigned int N,
                      char *const ss, char *const se, const char saved_char) {
        const bool
          _is_scalar = is_scalar(arg),
          _is_vector = is_vector(arg) && (!N || size(arg)==N);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Resize/reshape the operand to n*n elements before use, e.g. A.resize(n,n).
  2. Verify vector sizes match the expected matrix order in the expression.
  3. Check that file-loaded matrix data has the correct dimensions.
  4. Split chained matrix operations so each operand is independently square.

Example fix

// before
img.fill("mprod(A,B)"); // A is 2x3
// after
A.resize(3,3);
img.fill("mprod(A,B)");
Defensive patterns

Strategy: validation

Validate before calling

if (vec.width()*vec.height() != n*n)
  throw std::invalid_argument("operand is not a square matrix");

Type guard

bool isSquareMatrix(const CImg<double>& m, unsigned n) { return m.size() == n*n; }

Try / catch

try { img.fill(expr); } catch (const cimg_library::CImgArgumentException& e) { /* reshape operand and retry */ }

Prevention

When it happens

Trigger: Matrix multiplication or solving in an MP expression with a non-square operand, e.g. 'mprod(A,B)' where A has 6 elements (2x3), or passing a vector sized 3 where a 4x4 matrix is expected.

Common situations: Loading matrix data from files with wrong dimensions; reshaping errors producing 1xN vectors; mixing rotation matrices of different orders (3x3 vs 4x4) in graphics code.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/f971f0795348763b. Report an issue: GitHub.