Challenge
Given the Cartesian coordinates of two or more distinct points in Euclidean n-space (\$\mathbb{R}^n\$), output the minimum dimension of a flat (affine) subspace that contains those points, that is 1 for a line, 2 for a plane, and so on.
For example, in 3-space (the 3-dimensional world we live in), there are a few possibilities:
- The points are not coplanar, e.g.
(0,0,0),(0,0,1),(0,1,0),(1,0,0)
. The full 3 dimensions would be needed to describe the points, so the output would be3
- The points are coplanar but not all collinear, e.g.
(0,0,0),(1,0,0),(0,1,0),(1,1,0)
. The points lie on a 2-dimensional surface (a plane), so the output would be2
. - The points are collinear, and there is more than one, e.g.
(0,0,0),(1,0,0)
. They all lie on a line (1-dimensional), so the output is1
. - One or zero points are given. You do not have to handle these degenerate cases.
As @user202729 pointed out in sandbox, this is equivalent to the rank of the matrix whose column vectors are the given points if one of the points is the zero vector.
I encourage upvoting answers that don't have built-ins do most of the work, but they are valid answers.
Details
- The coordinates of each point will always be integers, so errors due to excessive floating-point roundoff are not acceptable
- Again, you do not have to handle fewer than 2 points
- The dimension
n
will be at least 2 - The set of points can be taken in any format that encodes equivalent information to a list of n-tuples. Your program/function may also take
n
as input if you desire. - Note that the subspace may not necessarily pass through the origin*
- This is code-golf, so shortest bytes wins
*Mathematically, if we require the subspace to pass through the origin, then it would be more specifically called a "linear subspace", not just flat.
Testcases
n points -> output
2 (1,0),(0,0) -> 1
2 (0,1),(0,0) -> 1
2 (6,6),(0,-2),(15,18),(12,14) -> 1
2 (0,0),(250,500),(100001,200002) -> 1
2 (0,0),(250,500),(100001,200003) -> 2
2 (3,0),(1,1),(1,0) -> 2
3 (0,0,0),(0,0,1),(0,1,0),(1,0,0) -> 3
3 (0,0,0),(1,0,0),(0,1,0),(1,1,0) -> 2
3 (0,0,0),(1,0,0) -> 1
4 (1,2,3,4),(2,3,4,5),(4,5,6,7),(4,4,4,4),(3,3,3,3),(2,2,2,2) -> 2
5 (5,5,5,5,5),(5,5,6,5,5),(5,6,5,5,5),(6,5,5,5,5),(5,4,3,2,1) -> 4
Related Challenges:
(1,3),(1,3)
so the output is0
? \$\endgroup\$