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