oILAB
Loading...
Searching...
No Matches
LLL.cpp
Go to the documentation of this file.
1/* This file is part of gbLAB.
2 *
3 * gbLAB is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef gbLAB_LLL_cpp_
8#define gbLAB_LLL_cpp_
9
10#include "../../include/Math/LLL.h"
11// http://www.arageli.org/download
12// https://www.mathworks.com/matlabcentral/fileexchange/49457-lattice-reduction-mimo?focused=3859922&tab=function
13
14namespace oILAB {
19/**********************************************************************/
20void LLL::lll_gram_schmidt_int(const int &k) {
21
22 for (int j = 0; j <= k; j++) {
23 int u = B.col(k).dot(B.col(j));
24 for (int i = 0; i < j; i++) {
25 u = (d(i + 1) * u - Lambda(k, i) * Lambda(j, i)) / d(i);
26 }
27 if (j < k) {
28 Lambda(k, j) = u;
29 } else {
30 d(k + 1) = u;
31 }
32 }
33 }
34
35 /**********************************************************************/
36 void LLL::lll_size_reduction_int(const int &k,
37 const int &l)
38 {
39
40 int q = (2 * Lambda(k, l) + d[l + 1]) / (2 * d(l + 1)); //quotient
41 B.col(k) -= B.col(l) * q;
42 H.col(k) -= H.col(l) * q;
43 Lambda(k, l) -= q * d(l + 1);
44
45 for (int i = 0; i < l; i++)
46 {
47 Lambda(k, i) -= q * Lambda(l, i);
48 }
49 }
50
51 /**********************************************************************/
52 void LLL::lll_interchange_int(const int &k,
53 const int &k_max)
54 {
55 // typedef typename Lambda_type::value_type T;
56 std::cout << "1" << std::endl;
57
58 Eigen::VectorXi tempCol = B.col(k);
59 B.col(k) = B.col(k - 1);
60 B.col(k - 1) = tempCol;
61
62 std::cout << "2" << std::endl;
63
64 tempCol = H.col(k);
65 H.col(k) = H.col(k - 1);
66 H.col(k - 1) = tempCol;
67
68 // B.swap_cols(k, k - 1);
69 // H.swap_cols(k, k - 1);
70
71 std::cout << "3" << std::endl;
72
73 for (int j = 0; j < k - 1; j++)
74 {
75 std::swap(Lambda(k, j), Lambda(k - 1, j));
76 }
77
78 std::cout << "4" << std::endl;
79
80 int lambda = Lambda(k, k - 1);
81 int b = (d(k - 1) * d(k + 1) + pow(lambda, 2)) / d(k);
82
83 std::cout << "5" << std::endl;
84 std::cout << "d(k+1)=" << d(k + 1) << std::endl;
85
86 for (int i = k + 1; i <= k_max; i++)
87 {
88 int t = Lambda(i, k);
89 Lambda(i, k) = (d(k + 1) * Lambda(i, k - 1) - lambda * t) / d(k);
90 Lambda(i, k - 1) = (b * t + lambda * Lambda(i, k)) / d(k + 1);
91 }
92 std::cout << "6" << std::endl;
93
94 d(k) = b;
95 std::cout << "7" << std::endl;
96 }
97
98 template <int m, int n>
99 LLL::LLL(const Eigen::Matrix<int, m, n> &B_in) : /* init */ B(B_in),
100 /* init */ d(Eigen::Matrix<int, 1, n + 1>::Ones()),
101 /* init */ H(Eigen::Matrix<int, n, n>::Identity()),
102 /* init */ Lambda(Eigen::Matrix<int, n, n>::Identity())
103 {
104
105 d(0) = 1;
106 d(1) = B.col(0).squaredNorm();
107
108 int k_max = 0;
109 for (int k = 1; k < n;)
110 {
111 std::cout << "k=" << k << std::endl;
112 if (k > k_max)
113 {
114 std::cout << "0" << std::endl;
115 k_max = k;
116
118
119 // if (is_null(d[k + 1]))
120 if (d(k + 1) == 0)
121 {
122 assert(0 && "B(i) did not form the basis");
123 // return false; // B(i) did not form the basis
124 }
125 }
126
127 if (2 * std::abs(Lambda(k, k - 1)) > d(k))
128 {
129 std::cout << "A" << std::endl;
130 lll_size_reduction_int(k, k - 1);
131 }
132
133 if (4 * d(k + 1) * d(k - 1) < 3 * pow(d(k), 2) - 4 * pow(Lambda(k, k - 1), 2))
134 {
135 std::cout << "B" << std::endl;
136 lll_interchange_int(k, k_max);
137 k = std::max(1, k - 1);
138 std::cout << "B1" << std::endl;
139 }
140 else
141 {
142 std::cout << "C" << std::endl;
143 for (int l = k - 1; l > 0; l--)
144 if (2 * std::abs(Lambda(k, l - 1)) > d(l))
145 lll_size_reduction_int(k, l - 1);
146 k++;
147 }
148 std::cout << "B=" << B << std::endl;
149 std::cout << "d=" << d.transpose() << std::endl;
150 }
151
152 std::cout << B << std::endl;
153 }
154
155 } // namespace oILAB
156#endif
LLL(const Eigen::Matrix< int, m, n > &B_in)
Definition LLL.cpp:99
void lll_interchange_int(const int &k, const int &k_max)
Definition LLL.cpp:52
void lll_size_reduction_int(const int &k, const int &l)
Definition LLL.cpp:36
Eigen::VectorXi d
Definition LLL.h:34
Eigen::MatrixXi B
Definition LLL.h:33
Eigen::MatrixXi Lambda
Definition LLL.h:36
Eigen::MatrixXi H
Definition LLL.h:35
void lll_gram_schmidt_int(const int &k)
Definition LLL.cpp:20