/usr/include/fflas-ffpack/ffpack/ffpack_charpoly.inl is in fflas-ffpack-common 1.6.0-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 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
/* ffpack/ffpack_charpoly.inl
* Copyright (C) 2005 Clement Pernet
*
* Written by Clement Pernet <Clement.Pernet@imag.fr>
*
*
* ========LICENCE========
* This file is part of the library FFLAS-FFPACK.
*
* FFLAS-FFPACK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*.
*/
#ifndef __FFLASFFPACK_charpoly_INL
#define __FFLASFFPACK_charpoly_INL
namespace FFPACK {
template <class Field, class Polynomial>
std::list<Polynomial>&
CharPoly (const Field& F, std::list<Polynomial>& charp, const size_t N,
typename Field::Element * A, const size_t lda,
const FFPACK_CHARPOLY_TAG CharpTag)
{
switch (CharpTag) {
case FfpackLUK:{
typename Field::Element * X = new typename Field::Element[N*(N+1)];
Protected::LUKrylov (F, charp, N, A, lda, X, N);
delete[] X;
return charp;
}
case FfpackKG:{
return Protected::KellerGehrig (F, charp, N, A, lda);
// break;
}
case FfpackDanilevski:{
return Danilevski (F, charp, N, A, lda);
// break;
}
case FfpackKGFast:{
size_t mc, mb, j;
if (Protected::KGFast (F, charp, N, A, lda, &mc, &mb, &j)){
std::cerr<<"NON GENERIC MATRIX PROVIDED TO KELLER-GEHRIG-FAST"<<std::endl;
}
return charp;
// break;
}
case FfpackKGFastG:{
return Protected::KGFast_generalized (F, charp, N, A, lda);
}
case FfpackHybrid:{
typename Field::Element * X = new typename Field::Element[N*(N+1)];
Protected::LUKrylov_KGFast (F, charp, N, A, lda, X, N);
delete[] X;
return charp;
}
case FfpackArithProg:{
size_t attempts=0;
bool cont = false;
FFLAS_INT_TYPE p;
F.characteristic(p);
// Heuristic condition (the pessimistic theoretical one being p<2n^2.
if ((unsigned long) (p) < N)
return CharPoly (F, charp, N, A, lda, FfpackLUK);
do{
try {
CharpolyArithProg (F, charp, N, A, lda, __FFPACK_CHARPOLY_THRESHOLD);
}
catch (CharpolyFailed){
if (attempts++ < 2)
cont = true;
else
return CharPoly(F, charp, N, A, lda, FfpackLUK);
}
} while (cont);
return charp;
}
default:{
typename Field::Element * X = new typename Field::Element[N*(N+1)];
Protected::LUKrylov (F, charp, N, A, lda, X, N);
delete[] X;
return charp;
}
}
}
namespace Protected {
template <class Field, class Polynomial>
std::list<Polynomial>&
LUKrylov (const Field& F, std::list<Polynomial>& charp, const size_t N,
typename Field::Element * A, const size_t lda,
typename Field::Element * X, const size_t ldx)
{
typedef typename Field::Element elt;
elt* Ai, *Xi, *X2=X;
int Ncurr=int(N);
charp.clear();
int nbfac = 0;
while (Ncurr > 0){
size_t *P = new size_t[(size_t)Ncurr];
Polynomial minP;//=new Polynomial();
FFPACK::MinPoly (F, minP, Ncurr, A, lda, X2, ldx, P);
int k = int(minP.size()-1); // degre of minpoly
if ((k==1) && F.isZero ((minP)[0])){ // minpoly is X
Ai = A;
int j = Ncurr*Ncurr;
while (j-- && F.isZero(*(Ai++))) ;
if (!j){ // A is 0, CharPoly=X^n
minP.resize(Ncurr+1);
(minP)[1] = F.zero;
(minP)[Ncurr] = F.one;
k=Ncurr;
}
}
nbfac++;
charp.push_front (minP);
if (k==Ncurr){
return charp;
}
size_t Nrest = (size_t)(Ncurr-k);
elt * X21 = X2 + k*ldx;
elt * X22 = X21 + k;
// Compute the n-k last rows of A' = PA^tP^t in X2_
// A = A . P^t
applyP (F, FFLAS::FflasRight, FFLAS::FflasTrans,
Ncurr, 0, (int)k, A, lda, P);
// Copy X2_ = (A'_2)^t
for (Xi = X21, Ai = A+k; Xi != X21 + Nrest*ldx; Ai++, Xi+=ldx-Ncurr)
for (size_t jj=0; jj<(size_t)Ncurr*lda; jj+=lda)
*(Xi++) = *(Ai+jj);
// A = A . P : Undo the permutation on A
applyP (F, FFLAS::FflasRight, FFLAS::FflasNoTrans,
Ncurr, 0, (int)k, A, lda, P);
// X2_ = X2_ . P^t (= (P A^t P^t)2_)
applyP (F, FFLAS::FflasRight, FFLAS::FflasTrans,
Nrest, 0, (int)k, X21, ldx, P);
delete[] P ;
// X21 = X21 . S1^-1
ftrsm(F, FFLAS::FflasRight, FFLAS::FflasUpper, FFLAS::FflasNoTrans, FFLAS::FflasUnit, Nrest, k,
F.one, X2, ldx, X21, ldx);
// Creation of the matrix A2 for recurise call
for (Xi = X22, Ai = A;
Xi != X22 + Nrest*ldx;
Xi += (ldx-Nrest), Ai += (lda-Nrest))
for (size_t jj=0; jj<Nrest; ++jj)
*(Ai++) = *(Xi++);
fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, Nrest, Nrest, k, F.mOne,
X21, ldx, X2+k, ldx, F.one, A, lda);
X2 = X22;
Ncurr = int(Nrest);
}
return charp;
}
template <class Field, class Polynomial>
std::list<Polynomial>&
LUKrylov_KGFast (const Field& F, std::list<Polynomial>& charp, const size_t N,
typename Field::Element * A, const size_t lda,
typename Field::Element * X, const size_t ldx)
{
typedef typename Field::Element elt;
size_t kg_mc, kg_mb, kg_j;
if (!KGFast (F, charp, N, A, lda, &kg_mc, &kg_mb, &kg_j))
return charp;
else{// Matrix A is not generic
Polynomial *minP = new Polynomial();
const elt* Ai;
elt* A2i, *Xi;
size_t *P = new size_t[N];
FFPACK::MinPoly (F, *minP, N, A, lda, X, ldx, P, FfpackKGF, kg_mc, kg_mb, kg_j);
size_t k = minP->size()-1; // degre of minpoly
if ((k==1) && F.isZero ((*minP)[0])){ // minpoly is X
Ai = A;
int j = int(N*N);
while (j-- && F.isZero(*(Ai++))) ;
if (!j){ // A is 0, CharPoly=X^n
minP->resize(N+1);
(*minP)[1] = F.zero;
(*minP)[N] = F.one;
k=N;
}
}
if (k==N){
charp.clear();
charp.push_front(*minP); // CharPoly = MinPoly
delete[] P;
return charp;
}
size_t Nrest = N-k;
elt * X21 = X + k*ldx;
elt * X22 = X21 + k;
// Creates the matrix A
//size_t lambda = MAX(0,N - kg_mc*(kg_j+1) - kg_mb); // uint >= 0 !!!
size_t lambda = kg_mc*(kg_j+1) + kg_mb;
if (lambda > N)
lambda = 0 ;
else
lambda = N - lambda ;
size_t imax = kg_mc+kg_mb;
// First Id
for (size_t j = 0; j < lambda; ++j){
for (size_t i=0; i<imax; ++i)
F.assign (*(A+j+i*lda), F.zero);
F.assign (*(A+j+imax*lda), F.one);
for (size_t i=imax+1; i<N; ++i)
F.assign (*(A+j+i*lda), F.zero);
++imax;
}
// Column block B
for (typename Field::Element* Aj=A; Aj<A+N*lda; Aj+=lda)
FFLAS::fcopy (F, kg_mb, Aj+lambda, 1, Aj+N-kg_mc-kg_mb, 1);
// Second Id block
imax = N- kg_j*kg_mc;
for (size_t j = 0; j< kg_j*kg_mc; ++j){
for (size_t i = 0; i<imax; ++i)
F.assign (*(A+lambda+kg_mb+j+i*lda), F.zero);
F.assign (*(A+lambda+kg_mb+j+imax*lda), F.one);
for (size_t i = imax+1; i<N; ++i)
F.assign (*(A+lambda+kg_mb+j+i*lda), F.zero);
++imax;
}
// Compute the n-k last rows of A' = PA^tP^t in X2_
// A = P . A
applyP (F, FFLAS::FflasLeft, FFLAS::FflasNoTrans,
N, 0,(int) k,
const_cast<typename Field::Element* &>(A), lda, P);
// Copy X2_ = (A'2_)
for (Xi = X21, Ai = A+k*lda; Xi != X21 + Nrest*ldx; Ai+=lda-N, Xi+=ldx-N){
for (size_t jj=0; jj<N; ++jj){
*(Xi++) = *(Ai++);
}
}
// A = P^t . A : Undo the permutation on A
applyP (F, FFLAS::FflasLeft, FFLAS::FflasTrans,
N, 0,(int) k,
const_cast<typename Field::Element* &>(A), lda, P);
// X2_ = X2_ . P^t (= (P A P^t)2_)
applyP (F, FFLAS::FflasRight, FFLAS::FflasTrans,
Nrest, 0,(int) k, X21, ldx, P);
// X21 = X21 . S1^-1
ftrsm(F, FFLAS::FflasRight, FFLAS::FflasUpper, FFLAS::FflasNoTrans, FFLAS::FflasUnit, Nrest, k,
F.one, X, ldx, X21, ldx);
// Creation of the matrix A2 for recurise call
elt * A2 = new elt[Nrest*Nrest];
for (Xi = X22, A2i = A2;
Xi != X22 + Nrest*ldx;
Xi += (ldx-Nrest)){
for (size_t jj=0; jj<Nrest; ++jj){
*(A2i++) = *(Xi++);
}
}
fgemm (F, FFLAS::FflasNoTrans, FFLAS::FflasNoTrans, Nrest, Nrest, k, F.mOne,
X21, ldx, X+k, ldx, F.one, A2, Nrest);
// Recursive call on X22
LUKrylov_KGFast (F, charp, Nrest, A2, Nrest, X22, ldx);
charp.push_front (*minP);
delete[] P;
delete[] A2;
return charp;
}
}
} // Protected
} // FFPACK
#endif // __FFLASFFPACK_charpoly_INL
|