gso_gram.h
Go to the documentation of this file.
1/* Copyright (C) 2005-2008 Damien Stehle.
2 Copyright (C) 2007 David Cade.
3 Copyright (C) 2011 Xavier Pujol.
4 Copyright (C) 2019 Koen de Boer
5
6 This file is part of fplll. fplll is free software: you
7 can redistribute it and/or modify it under the terms of the GNU Lesser
8 General Public License as published by the Free Software Foundation,
9 either version 2.1 of the License, or (at your option) any later version.
10
11 fplll is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with fplll. If not, see <http://www.gnu.org/licenses/>. */
18
19#ifndef FPLLL_GSOGRAM_H
20#define FPLLL_GSOGRAM_H
21
22#include "gso_interface.h"
23#include "nr/matrix.h"
24#include "util.h"
25
27
34template <class ZT, class FT> class MatGSOGram : public MatGSOInterface<ZT, FT>
35{
36public:
37 using MatGSOInterface<ZT, FT>::d;
40 using MatGSOInterface<ZT, FT>::u;
42 using MatGSOInterface<ZT, FT>::cols_locked; // maybe scratch.
46 using MatGSOInterface<ZT, FT>::u_inv_t;
47 using MatGSOInterface<ZT, FT>::sym_g;
48 using MatGSOInterface<ZT, FT>::mu;
49 using MatGSOInterface<ZT, FT>::r;
50 using MatGSOInterface<ZT, FT>::ztmp1;
51 using MatGSOInterface<ZT, FT>::ztmp2;
53 using MatGSOInterface<ZT, FT>::alloc_dim;
54 using MatGSOInterface<ZT, FT>::get_mu;
55 using MatGSOInterface<ZT, FT>::get_r;
56 using MatGSOInterface<ZT, FT>::gptr;
58
59 using MatGSOInterface<ZT, FT>::create_row;
63 using MatGSOInterface<ZT, FT>::update_gso;
66
67#ifdef DEBUG
68 /* Used only in debug mode. */
69 using MatGSOInterface<ZT, FT>::row_op_first;
70 using MatGSOInterface<ZT, FT>::row_op_last;
71 using MatGSOInterface<ZT, FT>::in_row_op_range;
72#endif
73
74 MatGSOGram(Matrix<ZT> &arg_g, Matrix<ZT> &arg_u, Matrix<ZT> &arg_uinv_t, int flags = GSO_INT_GRAM)
75 : MatGSOInterface<ZT, FT>(arg_u, arg_uinv_t, flags)
76 {
77 if (flags != GSO_INT_GRAM)
78 {
79 throw std::invalid_argument("flags must be equal to GSO_INT_GRAM");
80 }
81 gptr = &arg_g;
82 if (gptr == nullptr)
83 {
84 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
85 }
86 d = gptr->get_rows();
87 size_increased();
88
89#ifdef DEBUG
90 row_op_first = row_op_last = -1;
91#endif
92 }
93
94public:
95 virtual inline long get_max_exp_of_b();
96 virtual inline bool b_row_is_zero(int i);
97 virtual inline int get_cols_of_b() const;
98 virtual inline int get_rows_of_b() const;
99 virtual inline void negate_row_of_b(int i);
100
101 // inline void set_g(Matrix<ZT> arg_g)
102 virtual inline void create_rows(int n_new_rows);
103 virtual inline void remove_last_rows(int n_removed_rows);
104
105 /*
106 virtual inline void dump_mu_d(double *mu, int offset = 0, int block_size = -1);
107 virtual inline void dump_mu_d(vector<double> mu, int offset = 0, int block_size = -1);
108
109 virtual inline void dump_r_d(double *r, int offset = 0, int block_size = -1);
110 virtual inline void dump_r_d(vector<double> r, int offset = 0, int block_size = -1);
111 */
112 virtual void move_row(int old_r, int new_r);
113
114 virtual void row_addmul_we(int i, int j, const FT &x, long expo_add);
115
116 // b[i] += b[j] / b[i] -= b[j] (i > j)
117 virtual void row_add(int i, int j);
118 virtual void row_sub(int i, int j);
119
120 // virtual inline void printparam(ostream &os);
121
122 virtual inline ZT &sqnorm_coordinates(ZT &sqnorm, vector<ZT> coordinates);
123
124 virtual inline FT &get_gram(FT &f, int i, int j);
125
126 virtual inline ZT &get_int_gram(ZT &z, int i, int j);
127
128 // b[i] <-> b[j] (i < j)
129 virtual void row_swap(int i, int j);
130
131private:
132 /* Allocates matrices and arrays whose size depends on d (all but tmp_col_expo).
133 When enable_int_gram=false, initializes bf. */
134 virtual void size_increased();
135
136 virtual void discover_row();
137
138 /* Upates the i-th row of bf. It does not invalidate anything, so the caller
139 must take into account that it might change row_expo. */
140 virtual void update_bf(int i);
141 /* Marks g(i, j) for all j <= i (but NOT for j > i) */
142 virtual void invalidate_gram_row(int i);
143
144 // b[i] <- b[i] + x * b[j] (i > j)
145 virtual void row_addmul_si(int i, int j, long x);
146 // b[i] <- b[i] + (2^expo * x) * b[j] (i > j)
147
148 virtual void row_addmul_si_2exp(int i, int j, long x, long expo);
149 virtual void row_addmul_2exp(int i, int j, const ZT &x, long expo);
150 // virtual void apply_transform(const Matrix<FT> &transform, int src_base, int target_base);
151};
152
153template <class ZT, class FT>
154inline ZT &MatGSOGram<ZT, FT>::sqnorm_coordinates(ZT &sqnorm, vector<ZT> coordinates)
155{
156 vector<ZT> tmpvec;
157 Matrix<ZT> &g = *gptr;
158 vector_matrix_product(tmpvec, coordinates, g);
159
160 sqnorm = 0;
161 for (int i = 0; i < g.get_cols(); i++)
162 {
163 ztmp1.mul(tmpvec[i], coordinates[i]);
164 sqnorm.add(sqnorm, ztmp1);
165 }
166 return sqnorm;
167}
168
169template <class ZT, class FT> inline long MatGSOGram<ZT, FT>::get_max_exp_of_b()
170{
171 if (gptr == nullptr)
172 {
173 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
174 }
175 Matrix<ZT> &g = *gptr;
176 // normally, this returns the maximum
177 // exponent of b. We approximate this temporarily
178 // by the half of the maximum exponent of g.
179 // beware of errors, because maybe g is
180 // zero in the upper half of the matrix.
181 return g.get_max_exp() / 2;
182}
183template <class ZT, class FT> inline bool MatGSOGram<ZT, FT>::b_row_is_zero(int i)
184{
185 if (gptr == nullptr)
186 {
187 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
188 }
189 Matrix<ZT> &g = *gptr;
190 // normally this returns whether the
191 // ith row of the basis is the zero row.
192 // now, we just check whether g[i][i] is zero.
193 return g[i][i].is_zero();
194}
195template <class ZT, class FT> inline int MatGSOGram<ZT, FT>::get_cols_of_b() const
196{
197 if (gptr == nullptr)
198 {
199 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
200 }
201 // in this gram-matrix version, the number
202 // of columns of b is the same as the
203 // number of colums of g.
204 return gptr->get_cols();
205}
206
207template <class ZT, class FT> inline int MatGSOGram<ZT, FT>::get_rows_of_b() const
208{
209 if (gptr == nullptr)
210 {
211 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
212 }
213 // in this gram-matrix version, the number
214 // of columns of b is the same as the
215 // number of colums of g.
216 return gptr->get_rows();
217}
218
219template <class ZT, class FT> inline void MatGSOGram<ZT, FT>::negate_row_of_b(int i)
220{
221 if (enable_int_gram)
222 {
223
224 for (int j = 0; j < get_rows_of_b(); j++)
225 {
226 if (j != i)
227 {
228 sym_g(i, j).neg(sym_g(i, j));
229 }
230 }
231 }
232}
233
234template <class ZT, class FT> inline FT &MatGSOGram<ZT, FT>::get_gram(FT &f, int i, int j)
235{
236 FPLLL_DEBUG_CHECK(i >= 0 && i < n_known_rows && j >= 0 && j <= i && j < n_source_rows &&
237 !in_row_op_range(i));
238 if (enable_int_gram)
239 {
240 if (gptr == nullptr)
241 {
242 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
243 }
244 f.set_z((*gptr)(i, j));
245 }
246 return f;
247}
248
249template <class ZT, class FT> inline ZT &MatGSOGram<ZT, FT>::get_int_gram(ZT &z, int i, int j)
250{
251 FPLLL_DEBUG_CHECK(i >= 0 && i < n_known_rows && j >= 0 && j <= i && j < n_source_rows &&
252 !in_row_op_range(i));
253 if (enable_int_gram)
254 {
255 if (gptr == nullptr)
256 {
257 throw std::runtime_error("Error: gptr is equal to the nullpointer.");
258 }
259 z = (*gptr)[i][j];
260 }
261 return z;
262}
263
264template <class ZT, class FT> inline void MatGSOGram<ZT, FT>::remove_last_rows(int n_removed_rows)
265{
266 FPLLL_DEBUG_CHECK(!cols_locked && d >= n_removed_rows);
267 d -= n_removed_rows;
268 n_known_rows = min(n_known_rows, d);
269 n_source_rows = n_known_rows;
270 if (enable_transform)
271 u.set_rows(d);
272}
273
274template <class ZT, class FT> inline void MatGSOGram<ZT, FT>::create_rows(int n_new_rows)
275{
276 FPLLL_DEBUG_CHECK(!cols_locked);
277 int old_d = d;
278 d += n_new_rows;
279
280 if (enable_transform)
281 {
282 u.set_rows(d);
283 for (int i = old_d; i < d; i++)
284 for (int j = 0; j < u.get_cols(); j++)
285 u[i][j] = 0;
286 }
287 size_increased();
288 if (n_known_rows == old_d)
290}
291
293
294#endif
Definition: gso_gram.h:35
virtual void create_rows(int n_new_rows)
Definition: gso_gram.h:274
virtual void remove_last_rows(int n_removed_rows)
Definition: gso_gram.h:264
virtual void move_row(int old_r, int new_r)
Definition: gso_gram.cpp:283
virtual int get_cols_of_b() const
Definition: gso_gram.h:195
virtual bool b_row_is_zero(int i)
Definition: gso_gram.h:183
virtual void row_swap(int i, int j)
Definition: gso_gram.cpp:254
virtual int get_rows_of_b() const
Definition: gso_gram.h:207
MatGSOGram(Matrix< ZT > &arg_g, Matrix< ZT > &arg_u, Matrix< ZT > &arg_uinv_t, int flags=GSO_INT_GRAM)
Definition: gso_gram.h:74
virtual void row_add(int i, int j)
Definition: gso_gram.cpp:51
virtual void negate_row_of_b(int i)
Definition: gso_gram.h:219
virtual long get_max_exp_of_b()
Definition: gso_gram.h:169
virtual void row_addmul_we(int i, int j, const FT &x, long expo_add)
Definition: gso_gram.cpp:228
virtual FT & get_gram(FT &f, int i, int j)
Definition: gso_gram.h:234
virtual ZT & get_int_gram(ZT &z, int i, int j)
Definition: gso_gram.h:249
virtual ZT & sqnorm_coordinates(ZT &sqnorm, vector< ZT > coordinates)
Definition: gso_gram.h:154
virtual void row_sub(int i, int j)
Definition: gso_gram.cpp:80
Definition: gso_interface.h:60
int alloc_dim
Definition: gso_interface.h:558
bool cols_locked
Definition: gso_interface.h:557
vector< int > gso_valid_cols
Definition: gso_interface.h:605
const bool enable_int_gram
Definition: gso_interface.h:480
Matrix< FT > mu
Definition: gso_interface.h:572
FT & get_r(FT &f, int i, int j)
Definition: gso_interface.h:721
const bool enable_inverse_transform
Definition: gso_interface.h:493
bool update_gso()
Definition: gso_interface.h:764
int n_known_rows
Definition: gso_interface.h:554
void invalidate_gso_row(int i, int new_valid_cols=0)
Definition: gso_interface.cpp:26
int n_source_rows
Definition: gso_interface.h:555
bool update_gso_row(int i, int last_j)
Definition: gso_interface.cpp:131
Matrix< ZT > * gptr
Definition: gso_interface.h:597
void remove_last_row()
Definition: gso_interface.h:753
void discover_all_rows()
Definition: gso_interface.h:758
FT & get_mu(FT &f, int i, int j)
Definition: gso_interface.h:691
ZT ztmp1
Definition: gso_interface.h:611
Matrix< ZT > & u_inv_t
Definition: gso_interface.h:548
const bool row_op_force_long
Definition: gso_interface.h:499
int d
Definition: gso_interface.h:114
void create_row()
Definition: gso_interface.h:751
Matrix< FT > r
Definition: gso_interface.h:586
void print_mu_r_g(ostream &os)
Definition: gso_interface.h:645
void symmetrize_g()
Definition: gso_interface.h:629
ZT & sym_g(int i, int j)
Definition: gso_interface.h:661
const bool enable_transform
Definition: gso_interface.h:486
Matrix< ZT > & u
Definition: gso_interface.h:547
ZT ztmp2
Definition: gso_interface.h:613
int get_cols() const
Definition: matrix.h:156
int get_rows() const
Definition: matrix.h:154
long get_max_exp()
Definition: matrix.cpp:127
#define FPLLL_END_NAMESPACE
Definition: defs.h:117
#define FPLLL_DEBUG_CHECK(x)
Definition: defs.h:109
#define FPLLL_BEGIN_NAMESPACE
Definition: defs.h:114
@ GSO_INT_GRAM
Definition: gso_interface.h:29
void vector_matrix_product(vector< ZT > &result, const vector< ZT > &x, const Matrix< ZT > &m)
Definition: util.h:41