Skip to content

Singular matrix error for points in non-generic position #6

@OnDraganov

Description

@OnDraganov

The code assumes that points are in generic position in the following sense:
If the points are d-dimensional, then every p-sphere in R^d passes through at most p+1 points

If this is not the case, for example if we have four points in 3D lying on a common circle, then the difference vectors used in get_circumsphere are linearly dependent, and we get LinAlgError from numpy.linalg.solve.

There is a good use-case for non-generic point clouds: you can restrict the center of the minimized bounding ball to an affine space by reflecting the points through that affine space. For this reason, it would be useful if the code allowed for such point clouds.

Suggested solution:
Replace numpy.linalg.solve by numpy.linalg.lstsq.

The linear equation solved is of the form $U^T U x = b$, where we then care about $c = U x$. Therefore, if there is more solutions, it does not matter which one we take: if $x' = x + k$ is a different solution with $k \in \mathrm{Ker}\ U$, then $U x' = U (x + k) = U x = c$.

To treat the potential breaking case where the equation had no solutions and an approximation was returned, we can just check np.isclose(((a @ x - b) ** 2).sum(), 0.), where a, x, b are the matrix, the returned solution, and the right hand side.

Altogether I would replace line 48 with the following:

...
A = numpy.inner(U, U)
x, *_ = numpy.linalg.lstsq(A, B, rcond=None)
if not numpy.isclose(((A @ x - B) ** 2).sum(), 0):
    raise numpy.linalg.LinAlgError('Linear equation has no solution.')
C = numpy.dot(x, U)
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions