{"record":{"id":"1400f71100fcec6d","repo":"TheAlgorithms/C-Sharp","slug":"the-width-of-a-first-operand-should-match-the-height-of-a","errorCode":null,"errorMessage":"The width of a first operand should match the height of a second.","messagePattern":"The width of a first operand should match the height of a second\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Utilities/Extensions/MatrixExtensions.cs","lineNumber":16,"sourceCode":"namespace Utilities.Extensions;\n\npublic static class MatrixExtensions\n{\n    /// <summary>\n    ///     Performs immutable dot product multiplication on source matrix to operand.\n    /// </summary>\n    /// <param name=\"source\">Source left matrix.</param>\n    /// <param name=\"operand\">Operand right matrix.</param>\n    /// <returns>Dot product result.</returns>\n    /// <exception cref=\"InvalidOperationException\">The width of a first operand should match the height of a second.</exception>\n    public static double[,] Multiply(this double[,] source, double[,] operand)\n    {\n        if (source.GetLength(1) != operand.GetLength(0))\n        {\n            throw new InvalidOperationException(\n                \"The width of a first operand should match the height of a second.\");\n        }\n\n        var result = new double[source.GetLength(0), operand.GetLength(1)];\n\n        for (var i = 0; i < result.GetLength(0); i++)\n        {\n            for (var j = 0; j < result.GetLength(1); j++)\n            {\n                double elementProduct = 0;\n\n                for (var k = 0; k < source.GetLength(1); k++)\n                {\n                    elementProduct += source[i, k] * operand[k, j];\n                }\n\n                result[i, j] = elementProduct;\n            }","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Utilities/Extensions/MatrixExtensions.cs#L1-L34","documentation":"MatrixExtensions.Multiply computes the dot product of two 2D double matrices; multiplication is only defined when the first matrix's width (columns) equals the second's height (rows). When source.GetLength(1) != operand.GetLength(0) the shapes are incompatible, so the extension throws InvalidOperationException up front.","triggerScenarios":"Calling a.Multiply(b) where a is e.g. 2x3 and b is 2x3 (inner dimensions 3 vs 2 mismatch), or passing transposed/mis-ordered operands.","commonSituations":"ML/graph code multiplying feature matrices by weight matrices with mismatched layer sizes; forgetting to transpose one operand; mixing row-major/column-major assumptions after porting from another language.","solutions":["Verify source.GetLength(1) == operand.GetLength(0) before multiplying","Transpose one operand if the math intends a different orientation","Fix upstream matrix dimension definitions so the operands are conformable"],"exampleFix":"// before\nvar c = a.Multiply(b); // a: 2x3, b: 2x3 -> throws\n// after\nif (a.GetLength(1) != b.GetLength(0))\n{\n    throw new ArgumentException($\"Cannot multiply {a.GetLength(0)}x{a.GetLength(1)} by {b.GetLength(0)}x{b.GetLength(1)}.\");\n}\nvar c = a.Multiply(b);","handlingStrategy":"validation","validationCode":"if (a.GetLength(1) != b.GetLength(0))\n    throw new ArgumentException($\"Inner dimensions must match: {a.GetLength(1)} vs {b.GetLength(0)}.\");\nvar c = a.Multiply(b);","typeGuard":null,"tryCatchPattern":"try { var c = a.Multiply(b); }\ncatch (InvalidOperationException) { /* log shapes and abort/recompute with correct dims */ }","preventionTips":["Log matrix shapes at pipeline boundaries to catch drift early","Transpose explicitly (and name the variable accordingly) when orientation matters","Encode expected dimensions in types/wrapper classes where possible"],"tags":["c-sharp","matrix","linear-algebra","dimension-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}