/usr/include/linbox/algorithms/minpoly-integer.h is in liblinbox-dev 1.1.6~rc0-4.1.
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | /* -*- mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*better filter out repeated primes*/
/* Compute the minpoly of a matrix over an integer ring using modular arithmetic */
/* author: Zhendong Wan*/
#ifndef _LINBOX_MINPOLY_INTEGER_H__
#define _LINBOX_MINPOLY_INTEGER_H__
#include <iostream>
#include <math.h>
#include <linbox/field/field-traits.h>
#include <linbox/algorithms/matrix-hom.h>
#include <linbox/vector/vector-domain.h>
#include <linbox/randiter/random-prime.h>
//#include <linbox/solutions/minpoly.h>
#include <linbox/util/commentator.h>
#include <linbox/ffpack/ffpack.h>
#include <linbox/algorithms/cra-early-multip.h>
namespace LinBox {
/* compute the minpoly of a matrix over the Integer ring
* via modular method over Field.
*/
template <class _Integer, class _Field>
class MinPoly {
public:
typedef _Field Field;
//typedef _Integer Integer;
typedef typename Field::Element Element;
/* Blackbox case */
template<class Poly, class IMatrix>
static Poly& minPoly(Poly& y, const IMatrix& M);
template <class IMatrix>
static int minPolyDegree (const IMatrix& M, int n_try = 1);
template<class Poly, class IMatrix>
static Poly& minPoly(Poly& y, const IMatrix& M, int degree);
template<class Poly, class IMatrix>
static Poly& minPolyNonSymmetric(Poly& y, const IMatrix& M, int degree);
template<class Poly, class IMatrix>
static Poly& minPolySymmetric(Poly& y, const IMatrix& M, int degree);
template <class IMatrix>
static bool isSymmetric(const IMatrix& M, int n_try = 1);
};
template <class _Integer, class _Field>
class MinPolyBlas {
public:
typedef _Field Field;
typedef _Integer Integer;
typedef typename Field::Element Element;
template <class Poly, class Ring>
static Poly& minPolyBlas (Poly& y, const DenseMatrix<Ring>& M);
template <class Poly, class Ring>
static Poly& minPolyBlas (Poly& y, const DenseMatrix<Ring>& M, int degree);
template <class Ring>
static int minPolyDegreeBlas (const DenseMatrix<Ring>& M, int n_try = 1);
};
template<class _Integer, class _Field>
template<class Poly, class IMatrix>
Poly& MinPoly<_Integer, _Field>::minPoly(Poly& y, const IMatrix& M) {
int degree = minPolyDegree (M);
minPoly(y, M, degree);
return y;
}
template <class _Integer, class _Field>
template <class IMatrix>
int MinPoly<_Integer, _Field>::minPolyDegree (const IMatrix& M, int n_try) {
int degree = 0;
typedef typename IMatrix::template rebind<Field>::other FBlackbox;
typedef std::vector<Element> FPoly;
FBlackbox* fbb; FPoly fp;
integer mmodulus;
FieldTraits<Field>::maxModulus(mmodulus);
long bits = (long) floor (log((double)mmodulus)/M_LN2);
RandomPrimeIterator primeg(bits);
for (int i = 0; i < n_try; ++ i) {
++primeg;
Field F(*primeg);
MatrixHom::map (fbb, M, F);
//LinBox::minpoly (fp, *fbb); delete fbb;
minpoly (fp, *fbb); delete fbb;
if (degree < ((int) fp.size() - 1)) degree = fp.size() -1;
}
return degree;
}
template <class _Integer, class _Field>
template<class Poly, class IMatrix>
Poly& MinPoly<_Integer, _Field>::minPoly(Poly& y, const IMatrix& M, int degree) {
if (isSymmetric(M)) {
//std::cout << "Symmetric:\n";
minPolySymmetric(y, M, degree);
}
else {
//std::cout << "NonSymmetric:\n";
minPolyNonSymmetric(y, M, degree);
}
return y;
}
template <class _Integer, class _Field>
template<class Poly, class IMatrix>
Poly& MinPoly<_Integer, _Field>::minPolyNonSymmetric(Poly& y, const IMatrix& M, int degree) {
typedef typename IMatrix::template rebind<Field>::other FBlackbox;
typedef std::vector<Element> FPoly;
integer mmodulus;
FieldTraits<Field>::maxModulus(mmodulus);
long bits = (long) floor (log((double)mmodulus)/M_LN2);
RandomPrimeIterator primeg(bits);
FBlackbox* fbb;
FPoly fp (degree + 1);
typename FPoly::iterator fp_p;
y.resize (degree + 1);
EarlyMultipCRA< _Field > cra(3UL);
do {
++primeg;
Field F(*primeg);
MatrixHom::map (fbb, M, F);
minpoly (fp, *fbb); delete fbb;
cra.initialize(F, fp);
} while( (int)fp.size() - 1 != degree); // Test for Bad primes
while(! cra.terminated()) {
++primeg; while(cra.noncoprime(*primeg)) ++primeg;
Field F(*primeg);
MatrixHom::map (fbb, M, F);
minpoly (fp, *fbb); delete fbb;
if ((int)fp.size() - 1 != degree) {
commentator.report (Commentator::LEVEL_IMPORTANT,
INTERNAL_DESCRIPTION) << "Bad prime.\n";
continue;
}
cra.progress(F, fp);
}
cra. result (y);
// commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION) << "Number of primes needed: " << cra. steps() << std::endl;
return y;
}
template <class _Integer, class _Field>
template<class Poly, class IMatrix>
Poly& MinPoly<_Integer, _Field>::minPolySymmetric(Poly& y, const IMatrix& M, int degree) {
typedef typename IMatrix::template rebind<Field>::other FBlackbox;
typedef std::vector<Element> FPoly;
integer mmodulus;
FieldTraits<Field>::maxModulus(mmodulus);
long bits = (long) floor (log((double)mmodulus)/M_LN2);
RandomPrimeIterator primeg(bits);
FBlackbox* fbb;
FPoly fp (degree + 1);
typename FPoly::iterator fp_p;
y.resize (degree + 1);
EarlyMultipCRA< _Field > cra(3UL);
do {
++primeg;
Field F(*primeg);
MatrixHom::map (fbb, M, F);
minpolySymmetric (fp, *fbb); delete fbb;
cra.initialize(F, fp);
} while( (int)fp.size() - 1 != degree); // Test for Bad primes
while(! cra.terminated()) {
++primeg; while(cra.noncoprime(*primeg)) ++primeg;
Field F(*primeg);
MatrixHom::map (fbb, M, F);
minpolySymmetric (fp, *fbb); delete fbb;
if ((int)fp.size() - 1 != degree) {
commentator.report (Commentator::LEVEL_IMPORTANT,
INTERNAL_DESCRIPTION) << "Bad prime.\n";
continue;
}
cra.progress(F, fp);
}
cra. result (y);
//std::cout << "Number of primes needed: " << cra. steps() << std::endl;
return y;
}
template <class _Integer, class _Field>
template <class IMatrix>
bool MinPoly<_Integer, _Field>::isSymmetric(const IMatrix& M, int n_try) {
typedef typename IMatrix::Field Ring;
typedef typename Ring::Element Element;
Ring R(M. field()); int order = M. rowdim();
std::vector<Element> x(order), mx (order), xm (order);
typename std::vector<Element>::iterator x_p;
VectorDomain<Ring> RVD (R);
if (M. rowdim() != M. coldim()) return false;
for (int i = 0; i < n_try; ++ i) {
for (x_p = x. begin(); x_p != x. end(); ++ x_p)
R. init (*x_p, rand());
M. apply (mx, x);
M. applyTranspose (xm, x);
if (!RVD.areEqual(mx, xm)) return false;
}
return true;
}
template <class _Integer, class _Field>
template <class Poly, class Ring>
Poly& MinPolyBlas<_Integer, _Field>::minPolyBlas (Poly& y, const DenseMatrix<Ring>& M) {
int degree = minPolyDegreeBlas (M);
minPolyBlas (y, M, degree);
return y;
}
template <class _Integer, class _Field>
template <class Poly, class Ring>
Poly& MinPolyBlas<_Integer, _Field>::minPolyBlas (Poly& y, const DenseMatrix<Ring>& M, int degree) {
y. resize (degree + 1);
size_t n = M. rowdim();
integer mmodulus;
FieldTraits<Field>::maxModulus(mmodulus);
long bit1 = (long) floor (log((double)mmodulus)/M_LN2);
long bit2 = (long) floor (log(sqrt(double(4503599627370496LL/n)))/M_LN2);
RandomPrimeIterator primeg(bit1 < bit2 ? bit1 : bit2);
Element* FA = new Element [n*n];
Element* X = new Element [n*(n+1)];
size_t* Perm = new size_t[n];
Element* p;
typename DenseMatrix<Ring>::ConstRawIterator raw_p;
std::vector<Element> poly (degree + 1);
typename std::vector<Element>::iterator poly_ptr;
EarlyMultipCRA< _Field > cra(3UL);
do {
++primeg; while(cra.noncoprime(*primeg)) ++primeg;
Field F(*primeg);
for (p = FA, raw_p = M. rawBegin();
p != FA + (n*n); ++ p, ++ raw_p)
F. init (*p, *raw_p);
FFPACK::MinPoly( F, poly, n, FA, n, X, n, Perm);
cra.initialize(F, poly);
} while( poly. size() != degree + 1) ; // Test for Bad primes
while (! cra. terminated()) {
++primeg; while(cra.noncoprime(*primeg)) ++primeg;
Field F(*primeg);
for (p = FA, raw_p = M. rawBegin();
p != FA + (n*n); ++ p, ++ raw_p)
F. init (*p, *raw_p);
FFPACK::MinPoly( F, poly, n, FA, n, X, n, Perm);
if(poly. size() != degree + 1) {
commentator.report (Commentator::LEVEL_IMPORTANT,
INTERNAL_DESCRIPTION) << "Bad prime.\n";
continue;
}
cra.progress(F, poly);
}
cra. result(y);
//std::cout << "Number of primes needed: " << cra. steps() << std::endl;
delete FA; delete X; delete Perm;
return y;
}
template <class _Integer, class _Field>
template <class Ring>
int MinPolyBlas<_Integer, _Field>::minPolyDegreeBlas (const DenseMatrix<Ring>& M, int n_try) {
size_t n = M. rowdim();
int degree = 0;
Element* FA = new Element [n*n];
Element* X = new Element [n*(n+1)];
size_t* Perm = new size_t[n];
Element* p;
std::vector<Element> Poly;
integer mmodulus;
FieldTraits<Field>::maxModulus(mmodulus);
long bit1 = (long) floor (log((double)mmodulus)/M_LN2);
long bit2 = (long) floor (log(sqrt(double(4503599627370496LL/n)))/M_LN2);
RandomPrimeIterator primeg(bit1 < bit2 ? bit1 : bit2);
typename DenseMatrix<Ring>::ConstRawIterator raw_p;
for (int i = 0; i < n_try; ++ i) {
++primeg;
Field F(*primeg);
for (p = FA, raw_p = M. rawBegin();
p!= FA + (n*n); ++ p, ++ raw_p)
F. init (*p, *raw_p);
FFPACK::MinPoly( F, Poly, n, FA, n, X, n, Perm);
if (degree < Poly. size() - 1)
degree = Poly. size() -1;
}
delete FA; delete X; delete Perm;
return degree;
}
} // LinBox
#endif
|