TheAlgorithms/C-Sharp · error · ArgumentException
Dot product arguments must have same dimension
Error message
Dot product arguments must have same dimension
What it means
VectorExtensions.Dot computes the dot product of two double[] vectors, which is only defined for vectors of equal length. When lhs.Length != rhs.Length it throws ArgumentException before summing, because element-wise pairing is impossible.
Solutions
- Validate lhs.Length == rhs.Length before calling Dot
- Align vector dimensions upstream (pad, truncate, or regenerate one vector)
- Catch ArgumentException where vector length depends on external data and handle mismatch explicitly
Example fix
// before
var score = embedding.Dot(query); // lengths 384 vs 768 -> throws
// after
if (embedding.Length != query.Length)
{
throw new ArgumentException($"Vector length mismatch: {embedding.Length} vs {query.Length}.");
}
var score = embedding.Dot(query); Defensive patterns
Strategy: validation
Validate before calling
if (lhs.Length != rhs.Length)
throw new ArgumentException($"Dot product needs equal lengths: {lhs.Length} vs {rhs.Length}.");
var d = lhs.Dot(rhs); Try / catch
try { var d = lhs.Dot(rhs); }
catch (ArgumentException) { /* handle embedding-dimension mismatch, e.g. re-embed inputs */ } Prevention
- Pin embedding/model dimensions in config and validate inputs against it
- Pad or truncate vectors explicitly when dimensions are known to differ
- Add length assertions in similarity helper functions
When it happens
Trigger: Calling lhs.Dot(rhs) with arrays of different lengths, e.g. a 3-element direction vector dotted with a 4-element feature vector; also reachable indirectly via Magnitude when a caller supplies a wrong-length comparison vector.
Common situations: Similarity/cosine computations over differently sized embeddings (e.g. changed model dimension); passing weights arrays of stale length after a config change; mixing sparse-representation length with dense length.
Related errors
- The width of a first operand should match the height of a…
- Dimensions of matrices must be the same
- The column vector should have only 1 element in width.
- The length of the start vector doesn't equal the size of…
- Both points should have the same dimensionality
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/3798897b5abae611.
Report an issue: GitHub.
Appendix: source
Thrown at Utilities/Extensions/VectorExtensions.cs:52
result[i, j] = lhs[i] * rhs[j];
}
}
return result;
}
/// <summary>
/// Computes the dot product of two vectors.
/// </summary>
/// <param name="lhs">The LHS vector.</param>
/// <param name="rhs">The RHS vector.</param>
/// <returns>The dot product of the two vector.</returns>
/// <exception cref="ArgumentException">Dimensions of vectors do not match.</exception>
public static double Dot(this double[] lhs, double[] rhs)
{
if (lhs.Length != rhs.Length)
{
throw new ArgumentException("Dot product arguments must have same dimension");
}
double result = 0;
for (var i = 0; i < lhs.Length; i++)
{
result += lhs[i] * rhs[i];
}
return result;
}
/// <summary>
/// Computes the magnitude of a vector.
/// </summary>
/// <param name="vector">The vector.</param>
/// <returns>The magnitude.</returns>
public static double Magnitude(this double[] vector)
{View on GitHub (pinned to 96e2905cab)