{"record":{"id":"ebd955633b64dc31","repo":"TheAlgorithms/C-Sharp","slug":"both-points-should-have-the-same-dimensionality-minkowski","errorCode":null,"errorMessage":"Both points should have the same dimensionality","messagePattern":"Both points should have the same dimensionality","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/LinearAlgebra/Distances/Minkowski.cs","lineNumber":28,"sourceCode":"public static class Minkowski\n{\n    /// <summary>\n    /// Calculate Minkowski distance for two N-Dimensional points.\n    /// </summary>\n    /// <param name=\"point1\">First N-Dimensional point.</param>\n    /// <param name=\"point2\">Second N-Dimensional point.</param>\n    /// <param name=\"order\">Order of the Minkowski distance.</param>\n    /// <returns>Calculated Minkowski distance.</returns>\n    public static double Distance(double[] point1, double[] point2, int order)\n    {\n        if (order < 1)\n        {\n            throw new ArgumentException(\"The order must be greater than or equal to 1.\");\n        }\n\n        if (point1.Length != point2.Length)\n        {\n            throw new ArgumentException(\"Both points should have the same dimensionality\");\n        }\n\n        // distance = (|x1-y1|^p + |x2-y2|^p + ... + |xn-yn|^p)^(1/p)\n        return Math.Pow(point1.Zip(point2, (x1, x2) => Math.Pow(Math.Abs(x1 - x2), order)).Sum(), 1.0 / order);\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Distances/Minkowski.cs#L10-L35","documentation":"After validating the order, Minkowski.Distance requires point1.Length == point2.Length because the p-norm sums over paired coordinate differences. A dimensionality mismatch throws ArgumentException, preventing Zip from silently dropping the extra coordinates.","triggerScenarios":"Calling Minkowski.Distance(double[] point1, double[] point2, int order) with arrays of different lengths and a valid order — e.g., a 3D point vs. a 2D point, or feature vectors of unequal width.","commonSituations":"Vector datasets where one extraction step appended/dropped a feature; embedding dimension changes after a model upgrade; mixing sparse and dense representations flattened to different widths.","solutions":["Ensure both points have equal length before the call","Standardize the feature pipeline so vector width is constant","Pad or project vectors to a shared dimensionality first","Catch ArgumentException and report the mismatching vector dimensions"],"exampleFix":"// before\nMinkowski.Distance(new double[]{1,2,3}, new double[]{1,2}, 2); // throws\n// after\nMinkowski.Distance(new double[]{1,2,3}, new double[]{1,2,0}, 2);","handlingStrategy":"validation","validationCode":"if (order < 1) throw new ArgumentOutOfRangeException(nameof(order));\nif (point1.Length != point2.Length) throw new ArgumentException(\"Dimension mismatch for Minkowski distance\");","typeGuard":"bool CanComputeMinkowski(double[] a, double[] b, int order) => order >= 1 && a.Length == b.Length;","tryCatchPattern":"try { d = Minkowski.Distance(a, b, p); }\ncatch (ArgumentException ex) { log.Warn(\"Skipping vector pair: \" + ex.Message); }","preventionTips":["Check both order and dimensionality before calls","Pin embedding/feature dimensions in one shared constant","Test metric wrappers with mismatched-vector fixtures"],"tags":["csharp","linear-algebra","distance","dimensionality"],"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"}