How this calculation works
The closest lattice vector problem asks which integer combination of basis vectors lies nearest a target. This bounded calculator derives a certified coefficient box from an initial lattice point, enumerates every candidate in that box and retains all exact-distance ties. It returns a completed nearest-point result rather than an unqualified rounded-coordinate approximation.
min_(z in Z^n)||Bz−t||², with exact rational squared-distance comparisons.A worked example
For the identity basis and target (3/5,1/5), the closest lattice point is (1,0). Its squared distance is (2/5)²+(−1/5)²=1/5. The target’s nearest integer coordinates happen to work here, but a skew basis can make simple coordinate rounding fail.
Good to know
Full-rank rational square bases of dimension one through three. Exact inverse and L1 search bounds, at most 200,000 candidates. Basis vectors are columns. Cap exhaustion is inconclusive, and no high-dimensional lattice-reduction or Babai optimality guarantee is claimed.
Reference: Sage integer lattices and closest vectors (opens in a new tab) ↗
The basis vectors are columns
The matrix maps an integer coefficient column to a lattice point, so each basis vector occupies a column of the entered square matrix. Transposing a nonorthogonal basis can define a different lattice. The target is an ordinary rational point in the ambient coordinates and is not assumed to have integral basis coordinates.
The basis must be invertible in dimensions one through three. An exact inverse maps the target into coefficient space, where rounded coefficients supply one initial feasible lattice point. That point is only an incumbent: on a skew basis, coordinate rounding need not identify the Euclidean nearest point.
The LLL lattice basis reduction calculator with steps covers the nearby task. LLL improves a basis; it does not solve the closest-vector problem for an external target.
A feasible initial point creates a certified search box
Its residual provides an L1 radius that safely bounds its Euclidean distance. Multiplying inverse-row L1 bounds by that radius gives a coefficient box containing every lattice point that could improve or tie the incumbent. Conservative bounds may include unnecessary candidates, but excluding a possible winner would invalidate the exact optimum claim.
The box size is checked before enumeration. If it exceeds the declared cap, the page requests a smaller problem or improved basis without presenting the incumbent as an optimum. This separates a useful approximate starting point from the completed exact search promised by the visible result label.
Exact squared distances retain all ties
Squared Euclidean distances are compared as exact rational numbers, avoiding square-root rounding in tie decisions. The result includes every candidate attaining the same minimum squared value. A target on a Voronoi boundary can legitimately have multiple nearest lattice points, so choosing one arbitrarily would omit part of the supported answer.
Identity-lattice examples permit hand checks, while skew examples test that the rounded incumbent can be improved. Independent tests enumerate separately certified small coefficient ranges and verify every returned point equals its basis combination. The method does not claim practical high-dimensional closest-vector performance or convert an LLL-reduced basis into an automatic exact-nearest guarantee.
Read the basis as columns and the coefficients as integers
The lattice is {Bz:z∈Z^n}, where the columns of the square full-rank rational matrix B are basis vectors. The target can be any rational point. The objective is Euclidean distance in the displayed physical coordinates, not distance between the coefficient vectors z and B⁻¹target.
Transposing an entered matrix usually changes the lattice because it exchanges rows and columns. A unimodular integer change of basis preserves the lattice while changing coefficient labels and search effort. The exact squared distance is often more useful than a rounded square root because it preserves rational equality and allows ties to be recognized without a floating-point threshold.
See why coordinate rounding can fail for a skew basis
Use matrix rows 1,1/2;0,1/10 and target 49/100,0. Its basis columns are (1,0) and (1/2,1/10). Fractional basis coordinates are (0.49,0), whose independent rounding gives z=(0,0), point (0,0) and squared distance 0.2401.
But z=(0,1) gives point (0.5,0.1) and squared distance 0.01²+0.1²=0.0101. The point (0.5,−0.1), represented by z=(1,−1), ties it. Those are the nearest points: even second coefficients place x on integers with horizontal error at least 0.49, while odd second coefficients have vertical magnitude at least 0.1. Rounding coefficients supplied only an initial candidate, not a proof.
Understand how the finite search box gives a certificate
An initial lattice candidate provides a distance bound R. Any improving coefficient vector satisfies z−B⁻¹target=B⁻¹(Bz−target). Row-wise norm bounds on B⁻¹ enclose each integer coordinate of z in a finite interval. The implementation uses conservative exact L1 bounds so that no potentially better candidate is omitted.
It then enumerates the certified box with exact rational distance comparisons, retaining every equal minimum. Completion proves the nearest squared distance for the supported lattice. A large or ill-conditioned basis can make the box huge even in dimension three; exceeding 200,000 candidates raises an inconclusive resource error rather than converting the initial rounded point into a certified answer.
Interpret ties and the scope of low-dimensional exactness
For the identity basis in two dimensions and target (1/2,1/2), the four points (0,0),(1,0),(0,1),(1,1) all have squared distance 1/2. Multiple returned points are valid nearest neighbors, not duplicated solutions. Applications that require a unique choice must specify an external tie rule.
The form supports rational square bases in dimensions one through three only. It does not solve a rectangular embedded lattice or claim an efficient high-dimensional closest-vector algorithm. Decimal measurements are treated as the rational values entered, so a tiny measurement perturbation can break a mathematical tie. Report the exact input model if that distinction matters.
A few questions, answered
Are basis vectors rows or columns?
Columns; the matrix multiplies an integer coefficient vector.
Is rounded coefficient space always nearest?
No. It supplies an incumbent, followed by a certified exact-distance search.
Is rounding B⁻¹target always optimal?
No. It can fail for a skew basis. Here it supplies only an initial bound; complete bounded enumeration certifies the result.
Why return squared distance instead of only distance?
For rational inputs the square is rational and can be compared exactly, including ties. Taking a square root can introduce unnecessary rounding.