{"record":{"id":"3798897b5abae611","repo":"TheAlgorithms/C-Sharp","slug":"dot-product-arguments-must-have-same-dimension","errorCode":null,"errorMessage":"Dot product arguments must have same dimension","messagePattern":"Dot product arguments must have same dimension","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Utilities/Extensions/VectorExtensions.cs","lineNumber":52,"sourceCode":"                result[i, j] = lhs[i] * rhs[j];\n            }\n        }\n\n        return result;\n    }\n\n    /// <summary>\n    ///     Computes the dot product of two vectors.\n    /// </summary>\n    /// <param name=\"lhs\">The LHS vector.</param>\n    /// <param name=\"rhs\">The RHS vector.</param>\n    /// <returns>The dot product of the two vector.</returns>\n    /// <exception cref=\"ArgumentException\">Dimensions of vectors do not match.</exception>\n    public static double Dot(this double[] lhs, double[] rhs)\n    {\n        if (lhs.Length != rhs.Length)\n        {\n            throw new ArgumentException(\"Dot product arguments must have same dimension\");\n        }\n\n        double result = 0;\n        for (var i = 0; i < lhs.Length; i++)\n        {\n            result += lhs[i] * rhs[i];\n        }\n\n        return result;\n    }\n\n    /// <summary>\n    ///     Computes the magnitude of a vector.\n    /// </summary>\n    /// <param name=\"vector\">The vector.</param>\n    /// <returns>The magnitude.</returns>\n    public static double Magnitude(this double[] vector)\n    {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Utilities/Extensions/VectorExtensions.cs#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar score = embedding.Dot(query); // lengths 384 vs 768 -> throws\n// after\nif (embedding.Length != query.Length)\n{\n    throw new ArgumentException($\"Vector length mismatch: {embedding.Length} vs {query.Length}.\");\n}\nvar score = embedding.Dot(query);","handlingStrategy":"validation","validationCode":"if (lhs.Length != rhs.Length)\n    throw new ArgumentException($\"Dot product needs equal lengths: {lhs.Length} vs {rhs.Length}.\");\nvar d = lhs.Dot(rhs);","typeGuard":null,"tryCatchPattern":"try { var d = lhs.Dot(rhs); }\ncatch (ArgumentException) { /* handle embedding-dimension mismatch, e.g. re-embed inputs */ }","preventionTips":["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"],"tags":["c-sharp","vector","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"}