TheAlgorithms/C-Sharp · error · ArgumentException
Both points should have the same dimensionality
Error message
Both points should have the same dimensionality
What it means
Manhattan.Distance sums |x_i - y_i| across paired coordinates, which requires both arrays to have the same length. On a dimensionality mismatch it throws ArgumentException instead of allowing Zip to silently truncate the shorter point.
Solutions
- Check point lengths match before calling Distance
- Normalize data loading so every vector has a fixed width
- Impute/pad missing coordinates before distance computation
- Catch ArgumentException to flag malformed records in bulk computations
Example fix
// before
Manhattan.Distance(new double[]{1,2}, new double[]{3}); // throws
// after
Manhattan.Distance(new double[]{1,2}, new double[]{3,0}); Defensive patterns
Strategy: validation
Validate before calling
bool canCompare = point1 != null && point2 != null && point1.Length == point2.Length;
Type guard
bool SameDimension(double[] a, double[] b) => a.Length == b.Length;
Try / catch
try { d = Manhattan.Distance(a, b); }
catch (ArgumentException) { markRecordMalformed(recordId); } Prevention
- Validate record column counts before vectorization
- Use a shared vector type that enforces fixed dimension
- Impute missing coordinates instead of dropping them
When it happens
Trigger: Calling Manhattan.Distance(double[] point1, double[] point2) with arrays of different lengths — e.g., grid positions of differing dimension, or feature vectors built with different numbers of attributes.
Common situations: Pathfinding heuristics over grids where one point omits a coordinate; tabular data with missing columns; concatenating vectors from different encoders.
Related errors
- Both points should have the same dimensionality
- Both points should have the same dimensionality
- Both points should have the same dimensionality
- The order must be greater than or equal to 1.
- The source matrix is not square-shaped.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/0ccadac83258ce7c.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/LinearAlgebra/Distances/Manhattan.cs:22
/// Implementation fo Manhattan distance.
/// It is the sum of the lengths of the projections of the line segment between the points onto the coordinate axes.
/// In other words, it is the sum of absolute difference between the measures in all dimensions of two points.
///
/// Its commonly used in regression analysis.
/// </summary>
public static class Manhattan
{
/// <summary>
/// Calculate Manhattan 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 Manhattan 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 = |x1-y1| + |x2-y2| + ... + |xn-yn|
return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Sum();
}
}
View on GitHub (pinned to 96e2905cab)