OPENF4
Library for Gröebner basis computations over finite fields.
 All Classes Namespaces Files Functions Variables Friends Pages
matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Antoine Joux, Vanessa Vitse and Titouan Coladon
3  *
4  * This file is part of openf4.
5  *
6  * openf4 is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openf4 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with openf4. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 #ifndef OPENF4_MATRIX_H
27 #define OPENF4_MATRIX_H
28 
30 #include "global.h"
31 #include <iostream>
32 #include <cassert>
33 #include <ctime>
34 #include <chrono>
35 #include <string>
36 #include <fstream>
37 #include <sstream>
38 
39 #ifdef USE_OPENMP
40 #include <omp.h>
41 #endif // USE_OPENMP
42 
43 #ifdef HAVE_FFLAS_FFPACK
44 #include <givaro/modular-balanced.h>
45 #include <fflas-ffpack/ffpack/ffpack.h>
46 #include <fflas-ffpack/fflas/fflas.h>
47 #endif // HAVE_FFLAS_FFPACK
48 
50 #include "element-prime.h"
51 #include "element-gf2.h"
52 #include "element-gf2-extension.h"
53 
54 #ifdef HAVE_GIVARO
55 #include "element-givaro.h"
56 #endif // HAVE_GIVARO
57 
58 #ifdef HAVE_SSE2
59 #include <emmintrin.h>
60 #endif // HAVE_SSE2
61 
62 #ifdef HAVE_SSE4_1
63 #include <smmintrin.h>
64 #endif // HAVE_SSE4_1
65 
66 #ifdef HAVE_SSE4_2
67 #include <nmmintrin.h>
68 #endif // HAVE_SSE4_2
69 
73 namespace F4
74 {
79  template <typename Element>
80  class Matrix
81  {
82  public:
83 
84  /* Constructor */
85 
89  Matrix();
90 
96  Matrix(int height, int width);
97 
102  Matrix(std::string const & filename);
103 
108  Matrix(Matrix const & matrix);
109 
114  Matrix(Matrix && matrix);
115 
116 
117  /* Destructor */
118 
122  ~Matrix();
123 
124 
125  /* Get / Set */
126 
133  Element & operator() (int row, int col);
134 
141  Element operator() (int row, int col) const;
142 
149  Element getElement(int row, int col) const;
150 
157  void setElement (int row, int col, Element const & element);
158 
163  Element * getRow (int row);
164 
168  int getHeight() const;
169 
173  int getWidth() const;
174 
178  void setNbPiv(int nbPiv);
179 
183  int getNbPiv() const;
184 
188  void setTau(int * tau);
189 
193  int * getTau();
194 
198  void setSigma(int * sigma);
199 
203  int * getSigma();
204 
208  void setStartTail(int * startTail);
209 
213  int * getStartTail();
214 
218  void setEndCol(int * encCol);
219 
223  int * getEndCol();
224 
228  void setInfo(int nbPiv, int *tau, int *sigma, int * startTail, int * endCol);
229 
230 
231  /* Miscellaneous */
232 
236  void printMatrix (std::ostream & stream) const;
237 
241  void printMatrix (std::string const & filename) const;
242 
246  void printMatrixTxt (std::string const & filename) const;
247 
251  void printMatrixSage(std::string const & filename) const;
252 
260  bool isZero(int row, int col) const;
261 
268  void normalizeRow(Element * row, int start, int end);
269 
276  void normalizeRowPrime(Element * row, int start, int end);
277 
284  void normalizeRowGF2Extension(Element * row, int start, int end);
285 
293  void multRow(Element * row, Element const & element, int start, int end);
294 
302  void multRowPrime(Element * row, Element const & element, int start, int end);
303 
311  void multRowGF2Extension(Element * row, Element const & element, int start, int end);
312 
321  void addMultRow(Element * row1, Element * row2, Element element, int start, int end);
322 
331  void addMultRowPrime(Element * row1, Element * row2, Element element, int start, int end);
332 
341  void addMultRowGF2Extension(Element * row1, Element * row2, Element element, int start, int end);
342 
353  void doubleAddMultRowGF2Extension(Element * dest1, Element * dest2, Element mult1, Element mult2, Element * vec, int start, int end);
354 
367  void tripleAddMultRowGF2Extension(Element * dest1, Element * dest2, Element * dest3, Element mult1, Element mult2, Element mult3, Element * vec, int start, int end);
368 
383  void quadAddMultRowGF2Extension(Element * dest1, Element * dest2, Element * dest3, Element * dest4, Element mult1, Element mult2, Element mult3, Element mult4, Element * vec, int start, int end);
384 
407  void octAddMultRowGF2Extension(Element * dest1, Element * dest2, Element * dest3, Element * dest4, Element * dest5, Element * dest6, Element * dest7, Element * dest8, Element mult1, Element mult2, Element mult3, Element mult4, Element mult5, Element mult6, Element mult7, Element mult8, Element * vec, int start, int end);
408 
418  void groupAddMultRowGF2Extension(int ll, int dec, int startL2, int endL2, int start, int end);
419 
425  void swapRow(int numRow1, int numRow2);
426 
434  void swapCol(int numCol1, int numCol2, int start, int end);
435 
440  int echelonizeRight (chrono::duration<int,milli> & tmp_ech_db, chrono::duration<int,milli> & tmp_ech_dh);
441 
446  int echelonize ();
447 
452  int echelonizePrime ();
453 
458  int echelonizeGF2Extension ();
459 
460 
461  /* Internal operator */
462 
469  Matrix & operator=(Matrix const & matrix);
470 
477  Matrix & operator=(Matrix && matrix);
478 
479  private:
480  Element ** _matrix;
481  int _height;
482  int _width;
483  int _nbPiv;
484  int * _tau;
485  int * _sigma;
486  int * _startTail;
487  int * _endCol;
488  };
489 
490 
491  /* Internal operator */
492 
497  template <typename Element>
498  std::ostream & operator<<(std::ostream & stream, Matrix<Element> const & matrix);
499 }
500 
502 #include "../src/matrix.inl"
505 #endif // OPENF4_MATRIX_H
void printMatrix(std::ostream &stream) const
Print the matrix.
int getWidth() const
Get the width of the matrix (number of columns).
int * getSigma()
Get the permutation _sigma.
void normalizeRowPrime(Element *row, int start, int end)
Specialisation of normalizeRow for ElementPrime type.
Element ** _matrix
Definition: matrix.h:480
Represent a matrix.
Definition: matrix.h:80
void addMultRow(Element *row1, Element *row2, Element element, int start, int end)
Multiply a slice of the row1-th row by element and add a slice of the row2-th row.
~Matrix()
Destructor.
int echelonize()
Echelonize the matrix using the shape of the F4 matrix.
void normalizeRowGF2Extension(Element *row, int start, int end)
Specialisation of normalizeRow for ElementGF2Extension type.
void setSigma(int *sigma)
Specify the permutation _sigma.
Declaration of class ElementPrime.
int * _tau
Definition: matrix.h:484
Wrapper for config.h in order to avoid multiple definitions.
void normalizeRow(Element *row, int start, int end)
Normalize a slice of the row-th row.
int getHeight() const
Get the height of the matrix (number of rows).
Element getElement(int row, int col) const
Get matrix element.
Declaration of class ElementGF2.
bool isZero(int row, int col) const
Test if _matrix(row,col) is zero.
void multRowPrime(Element *row, Element const &element, int start, int end)
Specialisation of multRow for ElementPrime type.
void printMatrixTxt(std::string const &filename) const
Print the matrix in a format readable by constructor.
void swapRow(int numRow1, int numRow2)
Swap a slice of the row1-th row with a slice of the row2-th row.
void tripleAddMultRowGF2Extension(Element *dest1, Element *dest2, Element *dest3, Element mult1, Element mult2, Element mult3, Element *vec, int start, int end)
dest1 += mult1 * vec, dest2 += mult2 * vec, dest3 += mult3 * vec.
int echelonizeRight(chrono::duration< int, milli > &tmp_ech_db, chrono::duration< int, milli > &tmp_ech_dh)
Echelonize the left slice of the matrix using FFLAS-FFPACK routines.
Matrix & operator=(Matrix const &matrix)
Overload the operator =.
void printMatrixSage(std::string const &filename) const
Print the matrix in a format readable by sage.
void addMultRowGF2Extension(Element *row1, Element *row2, Element element, int start, int end)
Specialisation of addMultRow for ElementGF2Extension type.
void addMultRowPrime(Element *row1, Element *row2, Element element, int start, int end)
Specialisation of addMultRow for ElementPrime type.
Element * getRow(int row)
Get the row-th row of this.
int * getStartTail()
Get the array _startTail.
int getNbPiv() const
Get the number of pivots.
int * _startTail
Definition: matrix.h:486
int _height
Definition: matrix.h:481
void setInfo(int nbPiv, int *tau, int *sigma, int *startTail, int *endCol)
Set the informations required by echelonize.
Matrix()
Constructor.
int * _sigma
Definition: matrix.h:485
int * _endCol
Definition: matrix.h:487
Declaration of class ElementGivaro.
void multRow(Element *row, Element const &element, int start, int end)
Multiply a slice of the row-th row by element.
void doubleAddMultRowGF2Extension(Element *dest1, Element *dest2, Element mult1, Element mult2, Element *vec, int start, int end)
dest1 += mult1 * vec, dest2 += mult2 * vec.
int _width
Definition: matrix.h:482
void setNbPiv(int nbPiv)
Specify the number of pivots.
int * getEndCol()
Get the array _endCol.
void quadAddMultRowGF2Extension(Element *dest1, Element *dest2, Element *dest3, Element *dest4, Element mult1, Element mult2, Element mult3, Element mult4, Element *vec, int start, int end)
dest1 += mult1 * vec, dest2 += mult2 * vec, dest3 += mult3 * vec, dest4 += mult4 * vec...
void setElement(int row, int col, Element const &element)
Modify matrix element.
void setTau(int *tau)
Specify the permutation _tau.
void octAddMultRowGF2Extension(Element *dest1, Element *dest2, Element *dest3, Element *dest4, Element *dest5, Element *dest6, Element *dest7, Element *dest8, Element mult1, Element mult2, Element mult3, Element mult4, Element mult5, Element mult6, Element mult7, Element mult8, Element *vec, int start, int end)
dest1 += mult1 * vec, dest2 += mult2 * vec, dest3 += mult3 * vec, dest4 += mult4 * vec...
void multRowGF2Extension(Element *row, Element const &element, int start, int end)
Specialisation of multRow for ElementGF2Extension type.
void setStartTail(int *startTail)
Specify the array _startTail.
int _nbPiv
Definition: matrix.h:483
void setEndCol(int *encCol)
Specify the array _endCol.
void groupAddMultRowGF2Extension(int ll, int dec, int startL2, int endL2, int start, int end)
Use octAddMultRowGF2Extension, quadAddMultRowGF2Extension, tripleAddMultRowGF2Extension, doubleAddMultRowGF2Extension and addMultRowGF2Extension to suppress elements in column ll+dec from row startL2 to row endL2 using multiple of row ll.
int echelonizeGF2Extension()
Specialisation of echelonize for ElementGF2Extension Type.
Element & operator()(int row, int col)
Modify matrix element.
Declaration of class ElementGF2Extension.
void swapCol(int numCol1, int numCol2, int start, int end)
Swap a slice of the row1-th row with a slice of the row2-th row.
int * getTau()
Get the permutation _tau.
int echelonizePrime()
Specialisation of echelonize for ElementPrime Type, use lazy arithmetic.