TheAlgorithms/C-Sharp · error · InvalidOperationException

The width of a first operand should match the height of a…

Error message

The width of a first operand should match the height of a second.

What it means

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.

Solutions

  1. Verify source.GetLength(1) == operand.GetLength(0) before multiplying
  2. Transpose one operand if the math intends a different orientation
  3. Fix upstream matrix dimension definitions so the operands are conformable

Example fix

// before
var c = a.Multiply(b); // a: 2x3, b: 2x3 -> throws
// after
if (a.GetLength(1) != b.GetLength(0))
{
    throw new ArgumentException($"Cannot multiply {a.GetLength(0)}x{a.GetLength(1)} by {b.GetLength(0)}x{b.GetLength(1)}.");
}
var c = a.Multiply(b);
Defensive patterns

Strategy: validation

Validate before calling

if (a.GetLength(1) != b.GetLength(0))
    throw new ArgumentException($"Inner dimensions must match: {a.GetLength(1)} vs {b.GetLength(0)}.");
var c = a.Multiply(b);

Try / catch

try { var c = a.Multiply(b); }
catch (InvalidOperationException) { /* log shapes and abort/recompute with correct dims */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/1400f71100fcec6d. Report an issue: GitHub.

Appendix: source

Thrown at Utilities/Extensions/MatrixExtensions.cs:16

namespace Utilities.Extensions;

public static class MatrixExtensions
{
    /// <summary>
    ///     Performs immutable dot product multiplication on source matrix to operand.
    /// </summary>
    /// <param name="source">Source left matrix.</param>
    /// <param name="operand">Operand right matrix.</param>
    /// <returns>Dot product result.</returns>
    /// <exception cref="InvalidOperationException">The width of a first operand should match the height of a second.</exception>
    public static double[,] Multiply(this double[,] source, double[,] operand)
    {
        if (source.GetLength(1) != operand.GetLength(0))
        {
            throw new InvalidOperationException(
                "The width of a first operand should match the height of a second.");
        }

        var result = new double[source.GetLength(0), operand.GetLength(1)];

        for (var i = 0; i < result.GetLength(0); i++)
        {
            for (var j = 0; j < result.GetLength(1); j++)
            {
                double elementProduct = 0;

                for (var k = 0; k < source.GetLength(1); k++)
                {
                    elementProduct += source[i, k] * operand[k, j];
                }

                result[i, j] = elementProduct;
            }

View on GitHub (pinned to 96e2905cab)