moodle/moodle · error · Exception
Multiplication arguments must be Matrix or array
Error message
Multiplication arguments must be Matrix or array
What it means
Matrix\Operations::multiply() requires every operand to be a Matrix object or a 2-D PHP array that can be wrapped in a Matrix. The FIRST argument is validated after an optional array-to-Matrix conversion; if it is neither (e.g. a scalar, string, null, or object of another class), the library throws this Exception instead of attempting an invalid multiplication. Only the first operand hits this check — later operands are validated inside Multiplication::execute().
Solutions
- Ensure the first argument is a Matrix instance or a rectangular 2-D array, e.g. Operations::multiply(new Matrix($a), $b) or Operations::multiply($aArray, $bArray)
- If you intend to scale a matrix by a scalar, use Matrix::multiply($scalar) or directMatrix/scale operations instead of Operations::multiply
- Validate/convert cell values from spreadsheets before passing: cast numeric strings with (float) and reject null/false
- Inspect the actual runtime type with gettype()/var_dump if unsure what the first operand is
Example fix
// before $result = \Matrix\Operations::multiply(2, $matrixA); // throws // after $result = $matrixA->multiply(2); // scalar scaling on the Matrix object // or, for matrix products: $result = \Matrix\Operations::multiply($arrayA, $arrayB);
Defensive patterns
Strategy: type-guard
Validate before calling
// PHP
function assertMatrixOperand($v): \Matrix\Matrix {
if ($v instanceof \Matrix\Matrix) return $v;
if (is_array($v)) return new \Matrix\Matrix($v);
throw new \InvalidArgumentException('Expected Matrix or 2-D array, got ' . gettype($v));
}
assertMatrixOperand($first); Type guard
function isMatrixOperand($v): bool {
return $v instanceof \Matrix\Matrix || (is_array($v) && count($v) > 0 && is_array($v[0]));
} Try / catch
try {
$result = \Matrix\Operations::multiply($a, $b);
} catch (\Matrix\Exception $e) {
log_error('Bad multiply operand: ' . $e->getMessage());
$result = null; // or rethrow after adding operand context
} Prevention
- Always pass Matrix objects or rectangular 2-D arrays as the first operand
- Use Matrix::multiply() for scalar scaling instead of Operations::multiply
- Sanitize spreadsheet/JSON values (cast numeric strings, reject null/false) before matrix calls
- Unit-test operand edge cases (scalars, null, wrong class)
When it happens
Trigger: Calling Operations::multiply() (or the multiply() helper function) where the first argument is not a Matrix and not an array: e.g. multiply(5, $m), multiply('3', $a, $b), multiply(null, $m), multiply($someOtherObject, $m), or a first argument that is a 1-D/non-rectangular structure accepted as an array but note arrays are auto-wrapped — the failure is specifically non-array, non-Matrix values.
Common situations: Developers pass a scalar because they think multiply() does element-wise scalar multiplication (it only does matrix multiplication); a spreadsheet cell value read as string/null is fed in as the first operand; a function returns false/null on error and its result is passed straight through; mixing up argument order so a number lands first.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Addition arguments must be Matrix or array
- DirectSum arguments must be Matrix or array
- Subtraction arguments must be Matrix or array
- Argument of
- Division arguments must be Matrix or array
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/c2abb4b9ed1c95e7.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/phpspreadsheet/markbaker/matrix/classes/src/Operations.php:122
$result->execute($matrix);
}
return $result->result();
}
public static function multiply(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Multiplication operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Multiplication arguments must be Matrix or array');
}
$result = new Multiplication($matrix);
foreach ($matrixValues as $matrix) {
$result->execute($matrix);
}
return $result->result();
}
public static function subtract(...$matrixValues): Matrix
{
if (count($matrixValues) < 2) {
throw new Exception('Subtraction operation requires at least 2 arguments');
}
$matrix = array_shift($matrixValues);View on GitHub (pinned to 48ea8c3322)