matrix.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
5 This file is part of fplll. fplll is free software: you
6 can redistribute it and/or modify it under the terms of the GNU Lesser
7 General Public License as published by the Free Software Foundation,
8 either version 2.1 of the License, or (at your option) any later version.
9
10 fplll is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with fplll. If not, see <http://www.gnu.org/licenses/>. */
17
18#ifndef FPLLL_MATRIX_H
19#define FPLLL_MATRIX_H
20
21#include "fplll/nr/numvect.h"
22
24
26{
29};
30
31template <class T> class Matrix;
32
35template <class T> class MatrixRow
36{
37public:
39 T &operator[](int i) { return row[i]; }
42 const T &operator[](int i) const { return row[i]; }
44 int size() const { return row.size(); }
46 void print(ostream &os) const { os << row; }
47
48 bool is_zero(int from = 0) const { return row.is_zero(from); }
49 int size_nz() const { return row.size_nz(); }
50 void fill(long value) { row.fill(value); }
51 void add(const MatrixRow<T> &v) { row.add(v.row); }
52 void add(const MatrixRow<T> &v, int n) { row.add(v.row, n); }
53 void sub(const MatrixRow<T> &v) { row.sub(v.row); }
54 void sub(const MatrixRow<T> &v, int n) { row.sub(v.row, n); }
55 void mul(const MatrixRow<T> &v, int b, int n, T x) { row.mul(v.row, b, n, x); }
56 void div(const MatrixRow<T> &v, int b, int n, T x) { row.div(v.row, b, n, x); }
57 void addmul(const MatrixRow<T> &v, T x) { row.addmul(v.row, x); }
58 void addmul(const MatrixRow<T> &v, T x, int n) { row.addmul(v.row, x, n); }
59 void addmul(const MatrixRow<T> &v, T x, int b, int n) { row.addmul(v.row, x, b, n); }
60 void addmul_2exp(const MatrixRow<T> &v, const T &x, long expo, T &tmp)
61 {
62 row.addmul_2exp(v.row, x, expo, tmp);
63 }
64 void addmul_2exp(const MatrixRow<T> &v, const T &x, long expo, int /*n*/, T &tmp)
65 {
66 row.addmul_2exp(v.row, x, expo, tmp);
67 }
68 void addmul_si(const MatrixRow<T> &v, long x) { row.addmul_si(v.row, x); }
69 void addmul_si(const MatrixRow<T> &v, long x, int n) { row.addmul_si(v.row, x, n); }
70 void addmul_si_2exp(const MatrixRow<T> &v, long x, long expo, T &tmp)
71 {
72 row.addmul_si_2exp(v.row, x, expo, tmp);
73 }
74 void addmul_si_2exp(const MatrixRow<T> &v, long x, long expo, int n, T &tmp)
75 {
76 row.addmul_si_2exp(v.row, x, expo, n, tmp);
77 }
78
82 inline void dot_product(T &result, const MatrixRow<T> &v0, int beg, int n) const
83 {
84 fplll::dot_product(result, this->row, v0.row, beg, n);
85 }
86
88 inline void dot_product(T &result, const MatrixRow<T> &v0, int n) const
89 {
90 fplll::dot_product(result, this->row, v0.row, n);
91 }
92
94 inline void dot_product(T &result, const MatrixRow<T> &v0) const
95 {
96 fplll::dot_product(result, this->row, v0.row);
97 }
98
99 friend class Matrix<T>;
100
101private:
102 MatrixRow(const NumVect<T> &row) : row(const_cast<NumVect<T> &>(row)) {}
103 NumVect<T> &row;
104};
105
107template <class T> ostream &operator<<(ostream &os, const MatrixRow<T> &row)
108{
109 row.print(os);
110 return os;
111}
112
117template <class T> class Matrix
118{
119public:
121 Matrix() : r(0), c(0) {}
124 Matrix(int rows, int cols) : r(0), c(0) { resize(rows, cols); }
125
127 void clear()
128 {
129 r = c = 0;
130 matrix.clear();
131 }
133 bool empty() const { return r == 0; }
136 void resize(int rows, int cols);
139 void set_rows(int rows) { resize(rows, c); }
142 void set_cols(int cols) { resize(r, cols); }
144 template <class U> void fill(U value);
147 {
148 matrix.swap(m.matrix);
149 std::swap(r, m.r);
150 std::swap(c, m.c);
151 }
152
154 int get_rows() const { return r; }
156 int get_cols() const { return c; }
158 T &operator()(int i, int j)
159 {
160 FPLLL_DEBUG_CHECK(i >= 0 && i < r && j >= 0 && j < c);
161 return matrix[i][j];
162 }
164 const T &operator()(int i, int j) const
165 {
166 FPLLL_DEBUG_CHECK(i >= 0 && i < r && j >= 0 && j < c);
167 return matrix[i][j];
168 }
171 {
172 FPLLL_DEBUG_CHECK(i >= 0 && i < r);
173 return MatrixRow<T>(matrix[i]);
174 }
177 const MatrixRow<T> operator[](int i) const
178 {
179 FPLLL_DEBUG_CHECK(i >= 0 && i < r);
180 return MatrixRow<T>(matrix[i]);
181 }
183 void swap_rows(int r1, int r2) { matrix[r1].swap(matrix[r2]); }
186 void rotate_left(int first, int last) { rotate_left_by_swap(matrix, first, last); }
189 void rotate_right(int first, int last) { rotate_right_by_swap(matrix, first, last); }
193 void rotate(int first, int middle, int last) { rotate_by_swap(matrix, first, middle, last); }
196 void rotate_gram_left(int first, int last, int n_valid_rows);
199 void rotate_gram_right(int first, int last, int n_valid_rows);
201 void transpose();
202 T get_max();
203 long get_max_exp();
208 void print(ostream &os, int nrows = -1, int ncols = -1) const;
210 void read(istream &is);
212 ostream &print_comma(ostream &os) const;
214 static int set_print_mode(int new_print_mode)
215 {
216 int old_mode = print_mode;
217 print_mode = new_print_mode;
218 return old_mode;
219 }
220
221protected:
222 int r, c;
223 vector<NumVect<T>> matrix;
224
225 static int print_mode;
226};
227
228template <class T> int Matrix<T>::print_mode = MAT_PRINT_COMPACT;
229
230template <class T> ostream &operator<<(ostream &os, const Matrix<T> &m)
231{
232 m.print(os);
233 return os;
234}
235
236template <class T> istream &operator>>(istream &is, Matrix<T> &m)
237{
238 m.read(is);
239 return is;
240}
241
243template <class ZT> class ZZ_mat : public Matrix<Z_NR<ZT>>
244{
245public:
246 typedef Z_NR<ZT> T;
247 using Matrix<T>::r;
248 using Matrix<T>::c;
249 using Matrix<T>::matrix;
250 using Matrix<T>::resize;
251 using Matrix<T>::get_cols;
252 using Matrix<T>::get_rows;
253
255 ZZ_mat() : Matrix<T>() {}
258 ZZ_mat(int rows, int cols) : Matrix<T>(rows, cols) {}
259
261 void gen_zero(int d, int n)
262 {
263 resize(d, n);
264 for (int i = 0; i < d; i++)
265 matrix[i].fill(0);
266 }
268 void gen_identity(int d)
269 {
270 gen_zero(d, d);
271 for (int i = 0; i < d; i++)
272 matrix[i][i] = 1;
273 }
275 void gen_intrel(int bits);
277 void gen_simdioph(int bits, int bits2);
279 void gen_uniform(int bits);
280
286 void gen_ntrulike(int bits);
287 void gen_ntrulike_withq(int q);
288
295 void gen_ntrulike2(int bits);
296 void gen_ntrulike2_withq(int q);
297
301 void gen_qary(int k, Z_NR<ZT> &q);
302
303 void gen_qary(int k, int bits)
304 {
305 Z_NR<ZT> q;
306 q.randb(bits);
307 gen_qary(k, q);
308 }
309
310 void gen_qary_withq(int k, int q)
311 {
312 Z_NR<ZT> q2;
313 q2 = q;
314 gen_qary(k, q2);
315 }
316
317 void gen_qary_prime(int k, int bits)
318 {
319 Z_NR<ZT> q;
320 q.randb(bits);
321 q.nextprime(q);
322 gen_qary(k, q);
323 }
324
329 void gen_trg(double alpha);
330 void gen_trg2(FP_NR<mpfr_t> *w);
331};
332
340template <class ZTto, class ZTfrom>
341bool convert(ZZ_mat<ZTto> &Ato, const ZZ_mat<ZTfrom> &Afrom, int buffer = 0);
342
344template <class FT> class FP_mat : public Matrix<FP_NR<FT>>
345{
346public:
347 typedef FP_NR<FT> T;
349 FP_mat() : Matrix<T>() {}
352 FP_mat(int rows, int cols) : Matrix<T>(rows, cols) {}
353};
354
356
357#include "fplll/nr/matrix.cpp"
358
359#endif
Definition: matrix.h:345
FP_mat()
Definition: matrix.h:349
FP_mat(int rows, int cols)
Definition: matrix.h:352
FP_NR< FT > T
Definition: matrix.h:347
Definition: matrix.h:36
void dot_product(T &result, const MatrixRow< T > &v0, int beg, int n) const
Definition: matrix.h:82
void addmul(const MatrixRow< T > &v, T x)
Definition: matrix.h:57
int size_nz() const
Definition: matrix.h:49
void dot_product(T &result, const MatrixRow< T > &v0) const
Definition: matrix.h:94
void print(ostream &os) const
Definition: matrix.h:46
void addmul_2exp(const MatrixRow< T > &v, const T &x, long expo, T &tmp)
Definition: matrix.h:60
void addmul(const MatrixRow< T > &v, T x, int b, int n)
Definition: matrix.h:59
void fill(long value)
Definition: matrix.h:50
void dot_product(T &result, const MatrixRow< T > &v0, int n) const
Definition: matrix.h:88
void addmul_si_2exp(const MatrixRow< T > &v, long x, long expo, T &tmp)
Definition: matrix.h:70
void addmul_si(const MatrixRow< T > &v, long x)
Definition: matrix.h:68
int size() const
Definition: matrix.h:44
void sub(const MatrixRow< T > &v, int n)
Definition: matrix.h:54
bool is_zero(int from=0) const
Definition: matrix.h:48
void addmul_2exp(const MatrixRow< T > &v, const T &x, long expo, int, T &tmp)
Definition: matrix.h:64
T & operator[](int i)
Definition: matrix.h:39
void mul(const MatrixRow< T > &v, int b, int n, T x)
Definition: matrix.h:55
void addmul_si_2exp(const MatrixRow< T > &v, long x, long expo, int n, T &tmp)
Definition: matrix.h:74
void sub(const MatrixRow< T > &v)
Definition: matrix.h:53
void div(const MatrixRow< T > &v, int b, int n, T x)
Definition: matrix.h:56
void add(const MatrixRow< T > &v, int n)
Definition: matrix.h:52
void addmul(const MatrixRow< T > &v, T x, int n)
Definition: matrix.h:58
void addmul_si(const MatrixRow< T > &v, long x, int n)
Definition: matrix.h:69
void add(const MatrixRow< T > &v)
Definition: matrix.h:51
const T & operator[](int i) const
Definition: matrix.h:42
Definition: matrix.h:118
int r
Definition: matrix.h:222
ostream & print_comma(ostream &os) const
Definition: matrix.cpp:205
void rotate_left(int first, int last)
Definition: matrix.h:186
const T & operator()(int i, int j) const
Definition: matrix.h:164
void rotate_gram_left(int first, int last, int n_valid_rows)
Definition: matrix.cpp:65
vector< NumVect< T > > matrix
Definition: matrix.h:223
int get_cols() const
Definition: matrix.h:156
Matrix(int rows, int cols)
Definition: matrix.h:124
int c
Definition: matrix.h:222
void rotate(int first, int middle, int last)
Definition: matrix.h:193
MatrixRow< T > operator[](int i)
Definition: matrix.h:170
void transpose()
Definition: matrix.cpp:95
static int print_mode
Definition: matrix.h:225
void swap(Matrix< T > &m)
Definition: matrix.h:146
int get_rows() const
Definition: matrix.h:154
void resize(int rows, int cols)
Definition: matrix.cpp:26
void print(ostream &os, int nrows=-1, int ncols=-1) const
Definition: matrix.cpp:136
const MatrixRow< T > operator[](int i) const
Definition: matrix.h:177
static int set_print_mode(int new_print_mode)
Definition: matrix.h:214
void set_cols(int cols)
Definition: matrix.h:142
long get_max_exp()
Definition: matrix.cpp:127
void rotate_gram_right(int first, int last, int n_valid_rows)
Definition: matrix.cpp:80
Matrix()
Definition: matrix.h:121
void set_rows(int rows)
Definition: matrix.h:139
void fill(U value)
Definition: matrix.cpp:54
void swap_rows(int r1, int r2)
Definition: matrix.h:183
void clear()
Definition: matrix.h:127
T get_max()
Definition: matrix.cpp:114
void rotate_right(int first, int last)
Definition: matrix.h:189
void read(istream &is)
Definition: matrix.cpp:163
T & operator()(int i, int j)
Definition: matrix.h:158
bool empty() const
Definition: matrix.h:133
Definition: numvect.h:130
Definition: matrix.h:244
void gen_zero(int d, int n)
Definition: matrix.h:261
void gen_simdioph(int bits, int bits2)
Definition: matrix.cpp:252
void gen_trg2(FP_NR< mpfr_t > *w)
Definition: matrix.cpp:602
void gen_uniform(int bits)
Definition: matrix.cpp:276
void gen_ntrulike2_withq(int q)
Definition: matrix.cpp:482
void gen_ntrulike2(int bits)
Definition: matrix.cpp:426
void gen_trg(double alpha)
Definition: matrix.cpp:567
ZZ_mat()
Definition: matrix.h:255
void gen_qary(int k, Z_NR< ZT > &q)
Definition: matrix.cpp:538
void gen_qary_prime(int k, int bits)
Definition: matrix.h:317
ZZ_mat(int rows, int cols)
Definition: matrix.h:258
void gen_identity(int d)
Definition: matrix.h:268
void gen_ntrulike_withq(int q)
Definition: matrix.cpp:358
void gen_intrel(int bits)
Definition: matrix.cpp:229
void gen_qary(int k, int bits)
Definition: matrix.h:303
Z_NR< ZT > T
Definition: matrix.h:246
void gen_ntrulike(int bits)
Definition: matrix.cpp:288
void gen_qary_withq(int k, int q)
Definition: matrix.h:310
Definition: nr_Z.inl:19
void randb(int bits)
void nextprime(const Z_NR< Z > &nbr)
#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
MatPrintMode
Definition: matrix.h:26
@ MAT_PRINT_REGULAR
Definition: matrix.h:28
@ MAT_PRINT_COMPACT
Definition: matrix.h:27
istream & operator>>(istream &is, Matrix< T > &m)
Definition: matrix.h:236
ostream & operator<<(ostream &os, const MatrixRow< T > &row)
Definition: matrix.h:107
bool convert(ZZ_mat< ZTto > &Ato, const ZZ_mat< ZTfrom > &Afrom, int buffer=0)
Definition: matrix.cpp:632
void dot_product(T &result, const NumVect< T > &v1, const NumVect< T > &v2, int beg, int n)
Definition: numvect.h:386
void rotate_right_by_swap(vector< T > &v, int first, int last)
Definition: numvect.h:67
void rotate_by_swap(vector< T > &v, int first, int middle, int last)
Definition: numvect.h:41
void rotate_left_by_swap(vector< T > &v, int first, int last)
Definition: numvect.h:57