-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3992 from pleroy/LLL
An implementation of the LLL algorithm
- Loading branch information
Showing
8 changed files
with
207 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "numerics/concepts.hpp" | ||
|
||
namespace principia { | ||
namespace numerics { | ||
namespace _lattices { | ||
namespace internal { | ||
|
||
using namespace principia::numerics::_concepts; | ||
|
||
template<typename Matrix> | ||
requires two_dimensional<Matrix> | ||
Matrix LenstraLenstraLovász(Matrix const& L); | ||
|
||
} // namespace internal | ||
|
||
using internal::LenstraLenstraLovász; | ||
|
||
} // namespace _lattices | ||
} // namespace numerics | ||
} // namespace principia | ||
|
||
#include "numerics/lattices_body.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#pragma once | ||
|
||
#include "numerics/lattices.hpp" | ||
|
||
#include <algorithm> | ||
|
||
#include "numerics/fixed_arrays.hpp" | ||
#include "numerics/matrix_computations.hpp" | ||
#include "numerics/matrix_views.hpp" | ||
#include "numerics/unbounded_arrays.hpp" | ||
#include "quantities/elementary_functions.hpp" | ||
|
||
namespace principia { | ||
namespace numerics { | ||
namespace _lattices { | ||
namespace internal { | ||
|
||
using namespace principia::numerics::_fixed_arrays; | ||
using namespace principia::numerics::_matrix_computations; | ||
using namespace principia::numerics::_matrix_views; | ||
using namespace principia::numerics::_unbounded_arrays; | ||
using namespace principia::quantities::_elementary_functions; | ||
|
||
template<typename Matrix> | ||
struct LenstraLenstraLovászGenerator; | ||
|
||
template<typename Scalar> | ||
struct LenstraLenstraLovászGenerator<UnboundedMatrix<Scalar>> { | ||
using Vector = UnboundedVector<Scalar>; | ||
}; | ||
|
||
template<typename Scalar, int rows, int columns> | ||
struct LenstraLenstraLovászGenerator< | ||
FixedMatrix<Scalar, rows, columns>> { | ||
using Vector = FixedVector<Scalar, rows>; | ||
}; | ||
|
||
// This implements [HPS], theorem 7.71, figure 7.8. Note that figures 7.9 and | ||
// 7.10 are supposedly more efficient, but they are significantly more | ||
// complicated. If performance is an issue, we should look into recent | ||
// improvements of LLL. | ||
template<typename Matrix> | ||
requires two_dimensional<Matrix> | ||
Matrix LenstraLenstraLovász(Matrix const& L) { | ||
using G = LenstraLenstraLovászGenerator<Matrix>; | ||
auto const n = L.columns(); | ||
auto const m = L.rows(); | ||
auto v = L; | ||
for (int k = 1; k < n;) { | ||
auto qr = UnitriangularGramSchmidt(v); | ||
auto vₖ = ColumnView{.matrix = v, | ||
.first_row = 0, | ||
.last_row = m - 1, | ||
.column = k}; | ||
for (int j = k - 1; j >= 0; --j) { | ||
auto const μₖⱼ = qr.R(j, k); | ||
auto vⱼ = ColumnView{.matrix = v, | ||
.first_row = 0, | ||
.last_row = m - 1, | ||
.column = j}; | ||
auto const round_μₖⱼ = std::round(μₖⱼ); | ||
if (round_μₖⱼ != 0) { | ||
vₖ -= round_μₖⱼ * typename G::Vector(vⱼ); | ||
qr = UnitriangularGramSchmidt(v); | ||
} | ||
} | ||
auto const μₖₖ₋₁ = qr.R(k - 1, k); | ||
auto v𐌟ₖ = ColumnView{.matrix = qr.Q, | ||
.first_row = 0, | ||
.last_row = m - 1, | ||
.column = k}; | ||
auto v𐌟ₖ₋₁ = ColumnView{.matrix = qr.Q, | ||
.first_row = 0, | ||
.last_row = m - 1, | ||
.column = k - 1}; | ||
if (v𐌟ₖ.Norm²() >= (0.75 - Pow<2>(μₖₖ₋₁)) * v𐌟ₖ₋₁.Norm²()) { | ||
++k; | ||
} else { | ||
auto vₖ₋₁ = ColumnView{.matrix = v, | ||
.first_row = 0, | ||
.last_row = m - 1, | ||
.column = k - 1}; | ||
SwapColumns(vₖ₋₁, vₖ); | ||
k = std::max(k - 1, 1); | ||
} | ||
} | ||
return v; | ||
} | ||
|
||
} // namespace internal | ||
} // namespace _lattices | ||
} // namespace numerics | ||
} // namespace principia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "numerics/lattices.hpp" | ||
|
||
#include "gtest/gtest.h" | ||
#include "numerics/fixed_arrays.hpp" | ||
#include "testing_utilities/almost_equals.hpp" | ||
|
||
namespace principia { | ||
namespace numerics { | ||
namespace _lattices { | ||
|
||
using namespace principia::numerics::_fixed_arrays; | ||
using namespace principia::numerics::_lattices; | ||
using namespace principia::testing_utilities::_almost_equals; | ||
|
||
class LatticesTest : public ::testing::Test {}; | ||
|
||
// [HPS14], example 7.75. | ||
TEST_F(LatticesTest, Example_7_75) { | ||
FixedMatrix<double, 6, 6> l({19, 15, 43, 20, 0, 48, | ||
2, 42, 15, 44, 48, 33, | ||
32, 11, 0, 44, 35, 32, | ||
46, 0, 24, 0, 16, 9, | ||
3, 3, 4, 18, 31, 1, | ||
33, 24, 16, 15, 31, 29}); | ||
|
||
auto const reduced = LenstraLenstraLovász(l); | ||
EXPECT_THAT(reduced, | ||
AlmostEquals( | ||
(FixedMatrix<double, 6, 6>({ 7, -20, 5, -6, -10, 7, | ||
-12, 4, 2, -7, -24, 4, | ||
-8, -9, 33, -20, 21, -9, | ||
4, 16, 0, -21, -15, -11, | ||
19, 13, 15, 8, -6, 1, | ||
9, 16, -9, -12, -11, 31})), | ||
0)); | ||
} | ||
|
||
} // namespace _lattices | ||
} // namespace numerics | ||
} // namespace principia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters