This file is indexed.

/usr/include/linbox/algorithms/bbcharpoly.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* linbox/algorithms/bbchapoly.h
 * 
 *  by Clement Pernet <clement.pernet@imag.fr>
 *
 * See COPYING for license information.
 */

#ifndef __BBCHARPOLY_H
#define __BBCHARPOLY_H

#define __MAXITER 5
#include <vector>
#include <map>

#include "linbox/blackbox/scalar-matrix.h"
#include "linbox/blackbox/sum.h"
#include "linbox/ring/givaro-polynomial.h"
#include "linbox/field/modular.h"
#include "linbox/field/field-traits.h"
#include "linbox/solutions/det.h"
#include "linbox/solutions/rank.h"
#include "linbox/solutions/minpoly.h"
#include "linbox/randiter/random-prime.h"
#include "linbox/algorithms/matrix-hom.h"
#include "linbox/blackbox/polynomial.h"

namespace LinBox 
{	
	template<class FieldPoly, class IntPoly=FieldPoly>
	class FactorMult ;

	template < class Blackbox, class Polynomial, class Categorytag >
	Polynomial&
	blackboxcharpoly (Polynomial & P, 
			  const Blackbox                   & A,
			  const Categorytag                & tag,
			  const Method::Blackbox           & M);
	
	/* Algorithm computing the integer characteristic polynomial
	 * of a blackbox.
	 */
	template < class Blackbox >
	GivPolynomial<typename Blackbox::Field::Element>&
	blackboxcharpoly (GivPolynomial<typename Blackbox::Field::Element> & P, 
			  const Blackbox                   & A,
			  const RingCategories::IntegerTag & tag,
			  const Method::Blackbox           & M)
	{
		commentator.start ("Integer Blackbox Charpoly ", "IbbCharpoly");

 		typename Blackbox::Field intRing = A.field();
 		typedef Modular<uint32> Field;
 		typedef typename Blackbox::template rebind<Field>::other FieldBlackbox;
 		typedef GivPolynomialRing<typename Blackbox::Field, Dense> IntPolyDom;
 		typedef GivPolynomial<typename Blackbox::Field::Element> IntPoly;
 		typedef GivPolynomial<typename Field::Element> FieldPoly;
 		// Set of factors-multiplicities sorted by degree
 		typedef FactorMult<FieldPoly,IntPoly> FM;
 		typedef multimap<unsigned long,FM*> FactPoly;
		typedef typename FactPoly::iterator FactPolyIterator;
		multimap<FM*,bool> leadingBlocks;
		//typename multimap<FM*,bool>::iterator lead_it; 
 		FactPoly factCharPoly;
 		size_t n = A.coldim();

 		IntPolyDom IPD(intRing);
		
 		/* Computation of the integer minimal polynomial */
 		IntPoly intMinPoly;
		minpoly (intMinPoly, A, M);
		if (intMinPoly.size() == n+1){
			commentator.stop ("done", NULL, "IbbCharpoly");
 			return P = intMinPoly;
		}
 		/* Factorization over the integers */
 		vector<IntPoly*> intFactors;    
 		vector<unsigned long> exp;
 		IPD.factor (intFactors, exp, intMinPoly);
		size_t factnum = intFactors.size();

 		/* Choose a modular prime field */
 		RandomPrimeIterator primeg (28);
		++primeg;
 		Field F(*primeg);

		/* Building the structure of factors */
		int goal = n;

		for (size_t i = 0; i < intFactors.size(); ++i) {
			unsigned long deg =  (intFactors[i]->size()-1);
			FactorMult<FieldPoly,IntPoly>* FFM=NULL;
			if (exp[i] > 1) {
				IntPoly *tmp = new IntPoly(*intFactors[i]);
				FM* depend = NULL;
				for (size_t j = 1; j <= exp[i]; ++j){
					IntPoly * tmp2 = new IntPoly(*tmp);
					FieldPoly * tmp2p;
					typename IntPoly::template rebind<Field>() (tmp2p, *tmp2, F);

					FFM = new FM (tmp2p, tmp2, 0, depend);
					factCharPoly.insert (pair<size_t, FM*> (deg, FFM));
					factnum++;
					depend = FFM;
					deg += intFactors[i]->size()-1;
					if (j < exp[i])
						IPD.mul (*tmp, *tmp2, *intFactors[i]);
				}
				delete tmp;
				factnum--;
				FFM->multiplicity = 1; // The last factor is present in minpoly
				goal -= deg-intFactors[i]->size()+1;
				leadingBlocks.insert (pair<FM*,bool>(FFM,false));
			} else {
				FieldPoly * fp;
				typename IntPoly::template rebind<Field>() (fp, *intFactors[i], F);
				FFM = new FM (fp,intFactors[i],1,NULL);
				factCharPoly.insert (pair<size_t, FM* > (intFactors[i]->size()-1, FFM));
				leadingBlocks.insert (pair<FM*,bool>(FFM,false));
				goal -= deg;
			}
		}
		 
 		FieldBlackbox * Ap;
 		MatrixHom::map(Ap, A, F);
		
		findMultiplicities (*Ap, factCharPoly, leadingBlocks, goal, M);

 		// Building the integer charpoly
		IntPoly intCharPoly (n+1);
		IntPoly tmpP;
		intRing.init (intCharPoly[0], 1);
		for (FactPolyIterator it_f = factCharPoly.begin(); it_f != factCharPoly.end(); it_f++){
			IPD.pow (tmpP, *it_f->second->intP, it_f->second->multiplicity);
			IPD.mulin (intCharPoly, tmpP);
			delete it_f->second->intP;
			delete it_f->second->fieldP;
			delete it_f->second;
		}
		commentator.stop ("done", NULL, "IbbCharpoly");

		return P = intCharPoly;
	}
	/* Algorithm computing the  characteristic polynomial
	 * of a blackbox over a prime field.
	 */
	template < class Blackbox >
	GivPolynomial<typename Blackbox::Field::Element>& 
	blackboxcharpoly (GivPolynomial<typename Blackbox::Field::Element> & P, 
			  const Blackbox                                   & A,
			  const RingCategories::ModularTag                 & tag,
			  const Method::Blackbox           & M)
	{
		commentator.start ("Modular Blackbox Charpoly ", "MbbCharpoly");
		typedef typename Blackbox::Field Field;
		typedef GivPolynomialRing<Field, Dense> PolyDom;
		typedef typename PolyDom::Element Polynomial;
		// Set of factors-multiplicities sorted by degree
		typedef std::multimap<unsigned long,FactorMult<Polynomial>* > FactPoly;
		typedef typename FactPoly::iterator FactPolyIterator;
		multimap<FactorMult<Polynomial>*,bool> leadingBlocks;
		//typename multimap<FactorMult<Polynomial>*,bool>::iterator lead_it; 

		Field F = A.field();
		PolyDom PD (F);
		FactPoly factCharPoly;
		size_t n = A.coldim();

		/* Computation of the minimal polynomial */
		Polynomial minPoly;
		minpoly (minPoly, A, M);
		//std::cerr<<"Minpoly = "<<minPoly;
		if (minPoly.size() == n+1){
			commentator.stop ("done", NULL, "MbbCharpoly");
			return P = minPoly;
		}
		 
		/* Factorization over the field */
		std::vector<Polynomial*> factors;    
		std::vector<unsigned long> exp;
		PD.factor (factors, exp, minPoly);
		size_t factnum = factors.size();

		/* Building the structure of factors */
		int goal = n;

		for (size_t i = 0; i < factors.size(); ++i) {
			unsigned long deg =  (factors[i]->size()-1);
			FactorMult<Polynomial>* FFM=NULL;
			if (exp[i] > 1) {
				Polynomial* tmp = new Polynomial(*factors[i]);
				FactorMult<Polynomial>* depend = NULL;
				for (size_t j = 1; j <= exp[i]; ++j){
					Polynomial * tmp2 = new Polynomial(*tmp);
					FFM = new FactorMult<Polynomial> (tmp2, tmp2, 0, depend);
					//	std::cerr<<"Inserting new factor (exp>1) : "<<(*tmp2)<<std::endl;

					factCharPoly.insert (pair<size_t, FactorMult<Polynomial>*> (deg, FFM));
					factnum++;
					depend = FFM;
					deg += factors[i]->size()-1;
					if (j < exp[i])
						PD.mul (*tmp, *tmp2, *factors[i]);
				}
				delete tmp;
				factnum--;
				FFM->multiplicity = 1; // The last factor is present in minpoly
				goal -= deg-factors[i]->size()+1;
				leadingBlocks.insert (pair<FactorMult<Polynomial>*,bool>(FFM,false));
			} else {
				FFM = new FactorMult<Polynomial> (factors[i],factors[i],1,NULL);
				//std::cerr<<"Inserting new factor : "<<*factors[i]<<std::endl;
				factCharPoly.insert (pair<size_t, FactorMult<Polynomial>* > (factors[i]->size()-1, FFM));
				leadingBlocks.insert (pair<FactorMult<Polynomial>*,bool>(FFM,false));
				goal -= deg;
			}
		}
		 
		findMultiplicities ( A, factCharPoly, leadingBlocks, goal, M);
		 
		// Building the product 
		Polynomial charPoly (n+1);
		Polynomial tmpP;
		F.init (charPoly[0], 1);
		for (FactPolyIterator it_f = factCharPoly.begin(); it_f != factCharPoly.end(); it_f++){
			PD.pow (tmpP, *it_f->second->fieldP, it_f->second->multiplicity);
			PD.mulin (charPoly, tmpP);
			delete it_f->second->fieldP;
			delete it_f->second;

		}


		commentator.stop ("done", NULL, "MbbCharpoly");

		return P = charPoly;
	}
	
	template<class FieldPoly, class IntPoly>
	class FactorMult {
	public:
		FactorMult():multiplicity(0),dep(NULL){}
		FactorMult( FieldPoly* FP, IntPoly* IP, unsigned long m, FactorMult<FieldPoly,IntPoly>*d)
			: fieldP(FP), intP(IP), multiplicity(m), dep(d) {}
		FactorMult (const FactorMult<FieldPoly>& FM) 
			: fieldP(FM.fieldP), intP(FM.intP), multiplicity(FM.multiplicity), dep(FM.dep) {}
	
		int update (const size_t n, int * goal)
		{
			if (dep->dep != NULL){ 
				FactorMult<FieldPoly,IntPoly>*curr = dep;
				int k = dep->update (n,goal)+1;
				int d = (dep->fieldP->size()-1)/k;
				int tmp = (n-dep->multiplicity) / d;
				int i = k-1;
				while (curr->dep!=NULL){
					curr = curr->dep;
					tmp-=i*curr->multiplicity;
					i--;
				}
				tmp = tmp/k + (multiplicity - dep->multiplicity) / d;
				dep->multiplicity = tmp ;
				//std::cerr<<"Updating "<<*dep->fieldP<<" --> mul = "<<tmp<<std::endl;

				*goal -= tmp * (dep->fieldP->size()-1);
				return k;
			}
			else{
				int tmp =  (n - 2 * dep->multiplicity + multiplicity) / (dep->fieldP->size()-1);
				*goal -= tmp * (dep->fieldP->size()-1);
				//std::cerr<<"Updating (leaf)"<<*dep->fieldP<<" --> mul = "<<tmp<<std::endl;
				dep->multiplicity = tmp;
				return 1;
			}
		}


		FieldPoly                       *fieldP;
		IntPoly                         *intP;
		unsigned long                   multiplicity;
		FactorMult<FieldPoly, IntPoly>  *dep;

		std::ostream& write(std::ostream& os){
			return os<<"  FieldPoly --> "<<fieldP
				 <<"  IntPoly --> "<<intP
				 <<"  multiplicity --> "<<multiplicity<<std::endl
				 <<"  dep --> "<<dep<<std::endl;
		
		}
	};

	template < class FieldPoly,class IntPoly>
	void trials( list<vector<FactorMult<FieldPoly,IntPoly> > >& sols, const int goal,
		     vector<FactorMult<FieldPoly,IntPoly> >& ufv, const int i0 )
	{
		if ( !goal ){
			sols.push_back( ufv);
		}
		else if ( goal > 0 ){
			for (size_t i=i0; i<ufv.size(); ++i){
				ufv[i].multiplicity++;
				trials( sols, goal - ufv[i].fieldP->size()+1, ufv, i );
				ufv[i].multiplicity--;
			}
		}
	}	
	template <class Blackbox, class FieldPoly, class IntPoly>
	void findMultiplicities( const Blackbox& A, 
				 std::multimap<unsigned long, FactorMult<FieldPoly,IntPoly>* >& factCharPoly,
				 std::multimap<FactorMult<FieldPoly,IntPoly>*,bool>& leadingBlocks,
				 int goal,
				 const Method::Blackbox &M)
	{
		typedef multimap<unsigned long, FactorMult<FieldPoly,IntPoly>* > FactPoly;
		typedef typename Blackbox::Field Field;
		typedef GivPolynomialRing<Field, Dense> PolyDom;
		typename FactPoly::iterator itf = factCharPoly.begin();
		typename multimap<FactorMult<FieldPoly,IntPoly>*,bool>::iterator lead_it; 
		Field F = A.field();
		PolyDom PD(F);
		size_t factnum = factCharPoly.size();
		size_t n = A.coldim();
		
		/* Rank for the linear factors */
		while ( ( factnum > 1 ) && ( itf->first == 1) ){
			
			lead_it = leadingBlocks.find(itf->second);
			if ( lead_it != leadingBlocks.end())
				lead_it->second = true;
			long unsigned int r;
			
			/* The matrix Pi (A) */
			if (F.isZero (itf->second->fieldP->operator[](0))){
				rank (r, A, M) ;
			} else {
				PolynomialBB<Blackbox, FieldPoly > PA (A, *itf->second->fieldP);
				rank (r, PA,  M) ;
			}
			itf->second->multiplicity = r;

			//std::cerr<<"Rank 1 : "<<*itf->second->fieldP<<" --> "<<r<<std::endl;
			factnum--;
			itf++;
		}
		
		size_t maxIter = __MAXITER;//MAX( __MAXITER, sqrt(IspBB.mnz() ) );
		// Rank for the other factorspair
		while ( factnum > maxIter ){
			lead_it = leadingBlocks.find (itf->second);
			if ( lead_it != leadingBlocks.end())
				lead_it->second = true;
			
			PolynomialBB<Blackbox, FieldPoly > PA (A, *itf->second->fieldP);
			long unsigned int r;
			rank (r, PA,  M);
						
			itf->second->multiplicity =r;
			//std::cerr<<"Rank > 1 : "<<*itf->second->fieldP<<" --> "<<r<<std::endl;

			factnum--;
			itf++;
		}
		
		// update the multiplicities
		for (lead_it = leadingBlocks.begin(); lead_it != leadingBlocks.end(); lead_it++){
			
			FactorMult<FieldPoly,IntPoly>* currFFM = lead_it->first;
			//std::cerr<<"Updating multiplicities of "<<*lead_it->first->fieldP<<std::endl;
						
			if (!lead_it->second){ // the leading block has not been computed
				
				// go to the last computed multiplicity of the sequence
				while (currFFM->dep!=NULL){
					if (currFFM->dep->multiplicity != 0)
						break;
					currFFM = currFFM->dep;
				}
				if (currFFM->dep != NULL){
					
					// Need one more computation:
					PolynomialBB<Blackbox, FieldPoly > PA (A, *currFFM->fieldP);
					long unsigned int r;
					rank (r, PA, M) ;
					//std::cerr<<"extra factor : "<<*currFFM->fieldP<<" --> "<<r<<std::endl;

					int tmp = currFFM->multiplicity;
					currFFM->multiplicity = r;
					currFFM->update (n,&goal);
					currFFM->multiplicity = tmp;
				}
			} else {
				int lbm;
				if (currFFM->dep != NULL){
					
					int k = currFFM->update (n,&goal)+1;
					int d = (lead_it->first->fieldP->size()-1) / k;
					
					lbm = (n-lead_it->first->multiplicity) / d;
					currFFM = currFFM->dep;
					do{
						lbm -= currFFM->multiplicity * (currFFM->fieldP->size()-1); 
						currFFM = currFFM->dep;
					} while (currFFM!=NULL);
					lbm /= k;
					goal -= (lbm-1)*(lead_it->first->fieldP->size()-1);
				}else {
					lbm = (n-lead_it->first->multiplicity) / (lead_it->first->fieldP->size()-1);
					goal -=  (lbm-1)*(lead_it->first->fieldP->size()-1); 
				}
				lead_it->first->multiplicity = lbm;
			}
		}
		
		// Recursive search 
		typename FactPoly::iterator firstUnknowFactor = itf;
		if ( factnum <= __MAXITER ){
			std::vector<FactorMult<FieldPoly,IntPoly> > unknownFact (factnum);
			for (size_t  i = 0; i < factnum; ++i, itf++){
				unknownFact[i] = *itf->second;
			}
			std::list<vector<FactorMult<FieldPoly,IntPoly> > > sols;
			trials (sols, goal,unknownFact, 0);
			typename list<vector<FactorMult<FieldPoly,IntPoly> > >::iterator uf_it = sols.begin();

			if (sols.size()>1){
				// Evaluation of P in random gamma
				typename Field::Element d, gamma, mgamma, d2;
				typename Field::RandIter g(F);
				do
					g.random(gamma);
				while (F.isZero(gamma));
				

				//Building the matrix A + gamma.Id mod p
				F.neg( mgamma, gamma );
				ScalarMatrix<Field> gammaId( F, n, gamma ); 
				Sum<Blackbox,ScalarMatrix<Field> > Agamma(A, gammaId);

				// Compute det (A+gamma.Id)
				det (d, Agamma, M);
				if (A.rowdim()%2)
					F.negin(d);
				
				// Compute Prod(Pi(-gamma)^mi)
				typename Field::Element tmp, e;
				F.init (d2,1);
				typename FactPoly::iterator it_f=factCharPoly.begin();
				PolyDom PD (F);
				for (size_t i = 0; i < factCharPoly.size()-factnum; ++i, it_f++){
					PD.eval (tmp, *it_f->second->fieldP, mgamma);
					for (size_t j=0; j < it_f->second->multiplicity; ++j)
						F.mulin (d2, tmp);
				}
				while ( uf_it != sols.end() ){
					F.init (e,1);
					for (size_t i = 0; i < uf_it->size(); ++i){
						PD.eval( tmp, *(*uf_it)[i].fieldP, mgamma );
						for (size_t  j=0; j < (*uf_it)[i].multiplicity; ++j)
							F.mulin( e, tmp );
					}
					F.mulin( e, d2);
					if (F.areEqual(e,d))
						break;
					uf_it++;
				}
				if (uf_it == sols.end())
					std::cerr<<"FAIL:: No solutions found in recursive seach"<<endl;
			} // At this point, uf_it points on the good solution
			// update with the good multiplicities
			typename FactPoly::iterator it_f = firstUnknowFactor;
			typename std::vector<FactorMult<FieldPoly,IntPoly> >::iterator it_fm = (*uf_it).begin();
			for (; it_f != factCharPoly.end(); it_f++, it_fm++)
				it_f->second->multiplicity = it_fm->multiplicity;
		}
	}

}

#endif // __BBCHARPOLY_H