/usr/include/polymake/next/integer_linalg.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
#ifndef POLYMAKE_HERMITE_NORMAL_FORM_H
#define POLYMAKE_HERMITE_NORMAL_FORM_H
#include <iostream>
#include "polymake/client.h"
#include "polymake/pair.h"
#include "polymake/SparseMatrix.h"
#include "polymake/Bitset.h"
#include "polymake/Array.h"
#include "polymake/linalg.h"
#include "polymake/numerical_functions.h"
#include "polymake/list"
#include "polymake/GenericStruct.h"
namespace pm {
template<typename E>
class HermiteNormalForm :
public GenericStruct<HermiteNormalForm<E> > {
public:
DeclSTRUCT( DeclTemplFIELD(hnf, Matrix<E>)
DeclTemplFIELD(companion, SparseMatrix<E>)
DeclTemplFIELD(rank, int) );
};
template <typename MatrixTop, typename E>
int ranked_hermite_normal_form(const GenericMatrix<MatrixTop, E>& M, Matrix<E>& hnf, SparseMatrix<E>& companion, bool reduced = true){
SparseMatrix2x2<E> U;
SparseMatrix<E> R, S;
Matrix<E> N(M);
const int rows = M.rows();
const int cols = M.cols();
R = unit_matrix<E>(cols);
int current_row = 0, current_col = 0;
int rank = -1;
for(int i = 0; i<rows; i++){
bool nonzero = true;
// cout << N(i,current_col) << endl;
// Find a non-zero entry and move it to here.
if(N(i,current_col) == 0){
nonzero = false;
for(int j = current_col; j<cols; j++){
if(N(i,j) != 0){
nonzero = true;
U.i = current_col;
U.j = j;
U.a_ii = 0;
U.a_ij = 1;
U.a_ji = 1;
U.a_jj = 0;
R.multiply_from_right(U);
N.multiply_from_right(U);
}
}
}
// cout << pm::Matrix<E>(N) << endl;
if(!nonzero){
// cout << "Continuing" << endl;
current_row++;
continue;
} else {
rank = current_col;
}
// GCD part of algorithm.
for(int j = current_col+1; j<cols; j++){
// cout << " " << i << " " << j << endl;
if(N(i,j) != 0){
U.i = current_col;
U.j = j;
ExtGCD<E> egcd = ext_gcd(N(i,current_col), N(i,j));
U.a_ii = egcd.p;
U.a_ji = egcd.q;
U.a_ij = egcd.k2;
U.a_jj = -egcd.k1;
R.multiply_from_right(U);
N.multiply_from_right(U);
// cout << U.i<<": "<<U.a_ii <<" " << U.a_ij<<endl<<U.j <<": " <<U.a_ji<<" " << U.a_jj << endl;
}
// cout << pm::Matrix<E>(N) << endl;
}
if(N(i,current_col)<0){
S = unit_matrix<E>(cols);
S(current_col,current_col) = -1;
R = R*S;
N = N*S;
}
if(reduced){
for(int j=0; j<current_col; j++){
U.i = j;
U.j = current_col;
E factor = N(i,j) % N(i,current_col);
if(factor < 0) factor += N(i,current_col);
factor = (N(i,j) - factor)/N(i,current_col);
U.a_ii = 1;
U.a_ji = -factor;
U.a_ij = 0;
U.a_jj = 1;
R.multiply_from_right(U);
N.multiply_from_right(U);
}
}
current_col++;
if(current_col == cols){
break;
}
// cout << i << " " << current_row << endl;
}
rank++;
hnf = N;
companion = R;
return rank;
}
template <typename MatrixTop, typename E>
HermiteNormalForm<E> hermite_normal_form(const GenericMatrix<MatrixTop, E>& M, bool reduced = true){
HermiteNormalForm<E> res;
res.rank = ranked_hermite_normal_form(M, res.hnf, res.companion, reduced);
return res;
}
//returns indices of a minimal rowspace basis of a matrix in an euclidean ring
template<typename MatrixType>
Set<int> basis_rows_integer(MatrixType M){
typedef typename MatrixType::value_type Coeff;
HermiteNormalForm<Coeff> H = hermite_normal_form<MatrixType,Coeff>(M,false); //non-reduced form is faster, and we'll not use the matrix later so big entrys should not be an issue.
int pos = 0;
Set<int> basis;
for(typename Entire<Cols<Matrix<Coeff> > >::iterator cit = entire(cols(H.hnf)); ((*cit) != zero_vector<Coeff>(H.hnf.rows())) && !cit.at_end(); ++cit){
while((*cit)[pos]==0) ++pos; //find uppermost non-null entry in this col
basis += pos;
}
return basis;
}
//returns as rows a basis of the null space in an euclidean ring
template<typename MatrixType>
SparseMatrix<typename MatrixType::value_type> null_space_integer(MatrixType M){
typedef typename MatrixType::value_type Coeff;
Matrix<Coeff> H;
SparseMatrix<Coeff> R;
int r = ranked_hermite_normal_form(M, H, R);
return T(R.minor(All,range(r,R.cols()-1)));
}
} // namespace pm
namespace polymake {
using pm::HermiteNormalForm;
using pm::null_space_integer;
using pm::basis_rows_integer;
}
#endif
|