TheAlgorithms/C-Sharp · error · ArgumentException

Both points should have the same dimensionality

Error message

Both points should have the same dimensionality

What it means

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.

Solutions

  1. Ensure both points have equal length before the call
  2. Standardize the feature pipeline so vector width is constant
  3. Pad or project vectors to a shared dimensionality first
  4. Catch ArgumentException and report the mismatching vector dimensions

Example fix

// before
Minkowski.Distance(new double[]{1,2,3}, new double[]{1,2}, 2); // throws
// after
Minkowski.Distance(new double[]{1,2,3}, new double[]{1,2,0}, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (order < 1) throw new ArgumentOutOfRangeException(nameof(order));
if (point1.Length != point2.Length) throw new ArgumentException("Dimension mismatch for Minkowski distance");

Type guard

bool CanComputeMinkowski(double[] a, double[] b, int order) => order >= 1 && a.Length == b.Length;

Try / catch

try { d = Minkowski.Distance(a, b, p); }
catch (ArgumentException ex) { log.Warn("Skipping vector pair: " + ex.Message); }

Prevention

When it happens

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

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

Related errors


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

Appendix: source

Thrown at Algorithms/LinearAlgebra/Distances/Minkowski.cs:28

public static class Minkowski
{
    /// <summary>
    /// Calculate Minkowski distance for two N-Dimensional points.
    /// </summary>
    /// <param name="point1">First N-Dimensional point.</param>
    /// <param name="point2">Second N-Dimensional point.</param>
    /// <param name="order">Order of the Minkowski distance.</param>
    /// <returns>Calculated Minkowski distance.</returns>
    public static double Distance(double[] point1, double[] point2, int order)
    {
        if (order < 1)
        {
            throw new ArgumentException("The order must be greater than or equal to 1.");
        }

        if (point1.Length != point2.Length)
        {
            throw new ArgumentException("Both points should have the same dimensionality");
        }

        // distance = (|x1-y1|^p + |x2-y2|^p + ... + |xn-yn|^p)^(1/p)
        return Math.Pow(point1.Zip(point2, (x1, x2) => Math.Pow(Math.Abs(x1 - x2), order)).Sum(), 1.0 / order);
    }
}

View on GitHub (pinned to 96e2905cab)