TheAlgorithms/C-Sharp · error · ArgumentException

Both points should have the same dimensionality

Error message

Both points should have the same dimensionality

What it means

Chebyshev.Distance computes max(|x_i - y_i|) over paired coordinates, which is only defined when both points have the same number of dimensions. When point1.Length != point2.Length it throws ArgumentException because the pairing (Zip) would silently drop extra coordinates otherwise.

Solutions

  1. Verify point1.Length == point2.Length before calling Distance
  2. Fix feature extraction so all vectors have the same dimensionality
  3. Pad or truncate vectors to a common dimension as part of preprocessing
  4. Catch ArgumentException at the comparison boundary and reject the mismatched pair

Example fix

// before
Chebyshev.Distance(new double[]{1,2}, new double[]{1,2,3}); // throws
// after
var a = new double[]{1,2,0};
var b = new double[]{1,2,3};
if (a.Length == b.Length) Chebyshev.Distance(a, b);
Defensive patterns

Strategy: validation

Validate before calling

if (point1 == null || point2 == null || point1.Length != point2.Length) throw new ArgumentException("Points must be non-null and equally dimensional");

Type guard

bool SameDimension(double[] a, double[] b) => a != null && b != null && a.Length == b.Length;

Try / catch

try { d = Chebyshev.Distance(a, b); }
catch (ArgumentException ex) { metrics.RecordInvalidVectorPair(ex); }

Prevention

When it happens

Trigger: Calling Chebyshev.Distance(double[] point1, double[] point2) with arrays of different lengths, e.g., a 2D point compared with a 3D point, or a null/empty-dimensional mismatch from deserialized feature vectors.

Common situations: Machine-learning feature vectors assembled from different pipelines; merging datasets where one source added a feature column; accidental use of raw vs. normalized vectors of different widths.

Related errors


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

Appendix: source

Thrown at Algorithms/LinearAlgebra/Distances/Chebyshev.cs:22

/// Implementation of Chebyshev distance.
/// It is the maximum absolute difference between the measures in all dimensions of two points.
/// In other words, it is the maximum distance one has to travel along any coordinate axis to get from one point to another.
///
/// It is commonly used in various fields such as chess, warehouse logistics, and more.
/// </summary>
public static class Chebyshev
{
    /// <summary>
    /// Calculate Chebyshev distance for two N-Dimensional points.
    /// </summary>
    /// <param name="point1">First N-Dimensional point.</param>
    /// <param name="point2">Second N-Dimensional point.</param>
    /// <returns>Calculated Chebyshev distance.</returns>
    public static double Distance(double[] point1, double[] point2)
    {
        if (point1.Length != point2.Length)
        {
            throw new ArgumentException("Both points should have the same dimensionality");
        }

        // distance = max(|x1-y1|, |x2-y2|, ..., |xn-yn|)
        return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Max();
    }
}

View on GitHub (pinned to 96e2905cab)