Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Lattices/RLLL.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_RLLL_cpp_
9#define model_RLLL_cpp_
10
11#include <RLLL.h>
12
13namespace model
14{
15
16
17 /**********************************************************************/
18 void RLLL::update(VectorType &H,
19 MatrixType &M,
20 const int &k)
21 {
22 const int n = B.cols();
23
24 // B1=B;
25 // B1(:,k-1)=B(:,k);
26 // B1(:,k)=B(:,k-1);
27 B.col(k).swap(B.col(k - 1));
28
29 MatrixType H1 = H;
30 MatrixType M1 = M;
31 H1(k - 1) = H(k) + std::pow(M(k, k - 1), 2) * H(k - 1);
32 // M1(k,k-1)=M(k,k-1)*H(k-1)/H1(k-1);
33 M1(k, k - 1) *= H(k - 1) / H1(k - 1);
34 H1(k) = H(k - 1) - std::pow(M1(k, k - 1), 2) * H1(k - 1);
35 // for i=k+1:n
36 for (int i = k + 1; i < n; ++i)
37 {
38 M1(i, k - 1) = M(i, k - 1) * M1(k, k - 1) + M(i, k) * H(k) / H1(k - 1);
39 M1(i, k) = M(i, k - 1) - M(i, k) * M(k, k - 1);
40 }
41 // for j=1:k-2
42 for (int j = 0; j <= k - 2; ++j)
43 {
44 M1(k - 1, j) = M(k, j);
45 M1(k, j) = M(k - 1, j);
46 }
47 H = H1;
48 M = M1;
49 }
50
51 /**********************************************************************/
52 void RLLL::size_reduce(MatrixType &M,
53 const int &k,
54 const int &j)
55 {
56
57 const long int c = std::round(M(k, j));
58 B.col(k) -= c * B.col(j);
59 U.col(k) -= c * U.col(j);
60 for (int l = 0; l <= j; ++l) // j is already 0-based, so use <=
61 {
62 M(k, l) -= c * M(j, l);
63 }
64 }
65
66 /**********************************************************************/
67 RLLL::RLLL(const MatrixType &B0,
68 const double &delta) : /* init */ B(B0),
69 /* init */ U(Eigen::Matrix<long int, Eigen::Dynamic, Eigen::Dynamic>::Identity(B0.cols(), B0.cols()))
70 {
71 assert(delta >= 0.5 && delta <= 1.0 && "delta must be in [0.5 1]");
72
73 const int n = B0.cols();
74 assert(n <= B0.rows() && "B.rows() must be >= B.cols()");
75
76 // //std::cout<<"here 0"<<std::endl;
77 VectorType H(VectorType::Zero(n));
78 for (int j = 0; j < n; ++j)
79 {
80 H(j) = B.col(j).squaredNorm();
81 }
82
83 //std::cout<<H.transpose()<<std::endl;
84
85 //std::cout<<"here 1"<<std::endl;
86 MatrixType M(MatrixType::Identity(n, n));
87 for (int j = 0; j < n; ++j)
88 {
89 for (int i = j + 1; i < n; ++i)
90 {
91 double temp = 0.0;
92 //std::cout<<i<<" "<<j<<std::endl;
93
94 for (int k = 0; k <= j - 1; ++k) // j is already 0-based, so use <=
95 {
96 //std::cout<<i<<" "<<j<<" "<<k<<std::endl;
97
98 temp += M(j, k) * M(i, k) * H(k);
99 }
100 M(i, j) = (B.col(i).dot(B.col(j)) - temp) / H(j);
101 H(i) -= std::pow(M(i, j), 2) * H(j);
102 }
103 }
104
105 //std::cout<<std::setprecision(15)<<std::scientific<<M<<std::endl;
106 //std::cout<<std::setprecision(15)<<std::scientific<<H<<std::endl;
107
108 // MatrixType M(MatrixType::Identity(n,n));
109 int k = 1;
110 while (k < n)
111 {
112 if (fabs(M(k, k - 1)) > 0.5)
113 {
114 size_reduce(M, k, k - 1);
115 }
116
117 if (H(k) < (delta - std::pow(M(k, k - 1), 2)) * H(k - 1))
118 {
119 update(H, M, k);
120 U.col(k).swap(U.col(k - 1));
121 k = std::max(1, k - 1);
122 }
123 else
124 {
125 // for j=k-2:-1:1
126 for (int j = k - 2; j >= 0; --j)
127 {
128 if (fabs(M(k, j)) > 0.5)
129 {
130 size_reduce(M, k, j);
131 }
132 }
133 k = k + 1;
134 }
135 }
136
137 const double err = (B0.lu().solve(B) - U.cast<double>()).norm() / U.cast<double>().norm();
138 if (err > FLT_EPSILON)
139 {
140 std::cout << "RLLL relative error= " << std::setprecision(15) << std::scientific << err << " > " << FLT_EPSILON << std::endl;
141 assert(false && "Relative error too large. RLLL failed.");
142 }
143
144 const double absDetU(fabs(U.cast<double>().determinant()));
145 if (fabs(absDetU - 1.0) > FLT_EPSILON)
146 {
147 std::cout << "|det(U)|= " << std::setprecision(15) << std::scientific << absDetU << std::endl;
148 assert(false && "U is not unimodular. RLLL failed.");
149 }
150 }
151
152 /**********************************************************************/
153 const typename RLLL::MatrixType& RLLL::reducedBasis() const
154 {
155 return B;
156 }
157
158 /**********************************************************************/
159 const Eigen::Matrix<long int, Eigen::Dynamic, Eigen::Dynamic>& RLLL::unimodularMatrix() const
160 {
161 return U;
162 }
163
164} // end namespace
165#endif
Eigen::Matrix3d M
RLLL(const MatrixType &B0, const double &delta)
const MatrixType & reducedBasis() const
const Eigen::Matrix< long int, Eigen::Dynamic, Eigen::Dynamic > & unimodularMatrix() const
c
Definition int_R.m:15
int j
int k
int i
vertex temp