This file is indexed.

/usr/include/linbox/algorithms/smith-form-binary.h is in liblinbox-dev 1.3.2-1.1build2.

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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* Copyright (C)  LinBox
 * Written by Zhendong Wan
 *
 *
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
  * LinBox 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 __LINBOX_smith_form_binary_H
#define __LINBOX_smith_form_binary_H

/*! @file algorithms/smith-form-binary.h
 *  Implementation of EGV and EGV+ algorithm
 */


#include "linbox/util/debug.h"
#include "linbox/algorithms/default.h"
#include "linbox/util/commentator.h"

namespace LinBox
{

	/** \brief Compute Smith form.
	 *
	 * This is an implementation of EGV and EGV+ algorithms
	 * See EGV (FOCS '00) and SW (ISSAC '04) papers.
	 */
	template<class _Ring, class _oneInvariantFactor, class _Rank>
	class SmithFormBinary {

	public:

		typedef _Ring Ring;

		typedef _oneInvariantFactor oneInvariantFactor;

		typedef _Rank Rank;

		typedef typename Ring::Element Integer;

	protected:

		oneInvariantFactor oif;
		Rank rank;
		Ring r;

	public:

		/** \brief constructor
		*/
		SmithFormBinary(const oneInvariantFactor& _oif =oneInvariantFactor(),
				const Rank& _rank =Rank(),
				const Ring& _r = Ring(),
				int _oifthreshold =DEFAULTOIFTHRESHOLD,
				int _lifthreshold =DEFAULTLIFTHRESHOLD) :
			oif(_oif),rank(_rank),r(_r)
		{

			oif.setThreshold(_oifthreshold);
			oif.getLastInvariantFactor().setThreshold( _lifthreshold);
		}

		void setOIFThreshold (int _oifthreshold =DEFAULTOIFTHRESHOLD)
		{
			oif.setThreshold(_oifthreshold);
		}

		void setLIFThreshold (int _lifthreshold =DEFAULTLIFTHRESHOLD)
		{
			oif.getLastInvariantFactor().setThreshold(_lifthreshold);
		}

		int getOIFThreshold() const
		{
			return oif.getThreshold();
		}

		int getLIFThreshold() const
		{
			return oif.getLastInvariantFactor().getlIFThreshold();
		}


		/** \brief compute the Smith Form of an integer matrix,
		 *  ignoring these factors of primes in PrimeL
		 */
		template<class IMatrix, class Vector, class VectorP>
		Vector&  smithForm(Vector& sf, const IMatrix& A, const VectorP& PrimeL) const
		{

			// check if there are enough spaces in sf to store all invariant factors of A
			linbox_check(sf.size() >= (A.rowdim() <= A.coldim() ? A.rowdim() : A.coldim()));

			std::ostream& report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);

			typename Vector::iterator p;

			Integer zero;

			long Ar = rank.rank(A);


			report << "Rank = " << Ar <<'\n';

			r.init (zero,0);

			// set k-th invariant factor to zero for all k > Ar
			for (p = sf.begin() + Ar; p!= sf.end(); ++p)
				r.assign(*p,zero);


			// A is a zero matrix
			if (Ar == 0) {

				report << "Smith Form:[ ";

				for (p = sf.begin(); p != sf.end(); ++ p) {

					r. write (report, *p);

					report << ' ';
				}

				report <<"]\n";


				return sf;
			}


			// compute first invariant factor of A
			firstInvariantFactor(sf[0], A, PrimeL);

			report << "First Invariant Factor = ";

			r. write (report, sf[0]);

			report << '\n' << std::flush;

			// if rank(A) == 1
			if (Ar == 1) {

				report << "Smith Form:[ ";

				for (p = sf.begin(); p != sf.end(); ++ p) {

					r. write (report, *p);

					report << ' ';
				}

				report <<"]\n" << std::flush;

				return sf;
			}


			oif.oneInvariantFactor(sf[Ar - 1], A, (int)Ar, PrimeL);

			report << "Biggest invariant factor = ";

			r. write (report, sf[Ar - 1]);

			report << '\n' << std::flush;

			// binary search smith form
			smithFormBinarySearch (sf, A, 1, (int)Ar, PrimeL);

			report << "Smith Form:[ ";

			for (p = sf.begin(); p != sf.end(); ++ p) {

				r. write (report, *p);

				report << ' ';
			}

			report << "]\n" << std::flush;

			return sf;
		}


		/** \brief compute the Smith Form of an integer matrix
		*/
		template<class IMatrix, class Vector>
		Vector&  smithFormBinary(Vector& sf, const IMatrix& A) const
		{

			std::vector<Integer> empty_v;

			smithForm (sf, A, empty_v);

			return sf;

		}

	protected:

		/** \brief compute the 1st invariant factor, = GCD (all element in A),
		 *  missing these factors of primes in PrimeL
		 */
		template<class IMatrix, class Vector>
		Integer& firstInvariantFactor(Integer& fif, const IMatrix& A, const Vector& PrimeL) const
		{

			r.init(fif,0);

			typename IMatrix::ConstIterator A_p;

			for (A_p = A.Begin(); A_p != A.End(); ++ A_p) {

				if (!r.isZero(*A_p)) {
					r.gcd(fif, fif, *A_p);

					// if tmp == 1, break
					if (r.isOne(fif)) return fif;
				}
			}


			if (r.isZero(fif)) return fif;

			Integer p, quo, rem;

			typename Vector::const_iterator Prime_p;

			// filter out primes in PRIME from lif
			for ( Prime_p = PrimeL.begin(); Prime_p != PrimeL.end(); ++ Prime_p) {

				r.init (p,(unsigned long) *Prime_p);

				do {
					r.quoRem(quo,rem,fif,p);

					if (r.isZero( rem )) r.assign(fif,quo);
					else break;
				}
				while (true);

			}

			return fif;

		}


		/** \brief Binary search invariant factors between i and j, missing those factors in PrimeL
		 *  suppose sf[i - 1], sf [j - 1] are ith and jth invariant factor of A
		 *  i <= j
		 */

		template<class IMatrix, class Vector, class VectorP>
		Vector& smithFormBinarySearch (Vector& sf, const IMatrix& A, int i, int j, const VectorP& PrimeL) const
		{


			std::ostream& report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);

			report << "Binary Search invariant factors [" << i << ", "<< j << "]\n " << std::flush;

			typename Vector::iterator p;

			// if no invariant factor between i and j
			if (j <= i + 1) return sf;

			// if i-th invariant factor == j-th invariant factor
			if (r.areEqual(sf[i - 1], sf[j - 1])) {
				for (p = sf.begin() + i; p != sf.begin() + (j -1); ++ p)
					r.assign (*p, sf[i-1]);
				return sf;
			}

			int mid = (i + j) / 2;

			report << "Start to compute " << mid << "-th invariant factor:\n" << std::flush;


			oif.oneInvariantFactor (sf[mid - 1], A, mid, PrimeL);


			report << mid <<"-th invairant factor of A = " ;
			r.write (report, sf[mid -1]);
			report << "\n" << std::flush;


			// recursively binary search all k-invariant factors, where i <= k <= mid
			smithFormBinarySearch (sf, A, i, mid, PrimeL);

			// recurseively binary search all k-invariant factors, where mid <= k < j
			smithFormBinarySearch (sf, A, mid, j, PrimeL);

			return sf;
		}


	public:
		/** \brief compute the Smith Form of an integer matrix,
		 *  ignoring these factors of primes in PrimeL
		 *  Using backward search descibed by B. D. Saunders.
		 */
		template<class IMatrix, class Vector, class VectorP>
		Vector&  smithFormBackward(Vector& sf, const IMatrix& A, const VectorP& PrimeL) const
		{

			// check if there are enough spaces in sf to store all invariant factors of A
			linbox_check(sf.size() >= (A.rowdim() <= A.coldim() ? A.rowdim() : A.coldim()));

			std::ostream& report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);

			typename Vector::iterator p;

			Integer zero;

			long Ar = rank.rank(A);


			report << "Rank = " << Ar <<'\n';

			r.init (zero,0);

			// set k-th invariant factor to zero for all k > Ar
			for (p = sf.begin() + Ar; p!= sf.end(); ++p)
				r.assign(*p,zero);


			// A is a zero matrix
			if (Ar == 0) {

				report << "Smith Form:[ ";

				for (p = sf.begin(); p != sf.end(); ++ p) {

					r. write (report, *p);

					report << ' ';
				}

				report <<"]\n";


				return sf;
			}


			// compute first invariant factor of A
			firstInvariantFactor(sf[0], A, PrimeL);

			report << "First Invariant Factor = ";

			r. write (report, sf[0]);

			report << '\n' << std::flush;

			// if rank(A) == 1
			if (Ar == 1) {

				report << "Smith Form:[ ";

				for (p = sf.begin(); p != sf.end(); ++ p) {

					r. write (report, *p);

					report << ' ';
				}

				report <<"]\n" << std::flush;

				return sf;
			}


			oif.oneInvariantFactor(sf[Ar - 1], A, Ar, PrimeL);

			report << "Biggest invariant factor = ";

			r. write (report, sf[Ar - 1]);

			report << '\n' << std::flush;

			// binary search smith form
			smithFormBinarySearchBackward (sf, A, 1, Ar, 1, PrimeL);

			report << "Smith Form:[ ";

			for (p = sf.begin(); p != sf.end(); ++ p) {

				r. write (report, *p);

				report << ' ';
			}

			report << "]\n" << std::flush;

			return sf;
		}


		/** \brief compute the Smith Form of an integer matrix
		 * Using backward binary search.
		 */
		template<class IMatrix, class Vector>
		Vector&  smithFormBackward(Vector& sf, const IMatrix& A) const
		{

			std::vector<Integer> empty_v;

			smithFormBackward (sf, A, empty_v);

			return sf;

		}

	protected:

		/** \brief Binary search invariant factors between i and j, missing those factors in PrimeL
		 *  suppose sf[i - 1], sf [j - 1] are ith and jth invariant factor of A
		 *  i <= j
		 */

		template<class IMatrix, class Vector, class VectorP>
		Vector& smithFormBinarySearchBackward (Vector& sf, const IMatrix& A, int i, int j, int depth, const VectorP& PrimeL) const
		{


			std::ostream& report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);

			report << "Binary Search invariant factors [" << i << ", "<< j << "]\n " << std::flush;

			typename Vector::iterator p;

			// if no invariant factor between i and j
			if (j <= i + 1) return sf;

			// if i-th invariant factor == j-th invariant factor
			if (r.areEqual(sf[i - 1], sf[j - 1])) {
				for (p = sf.begin() + i; p != sf.begin() + (j -1); ++ p)
					r.assign (*p, sf[i-1]);
				return sf;
			}

			int mid = std::max (j -depth,  (i + j) / 2);

			report << "Start to compute " << mid << "-th invariant factor:\n" << std::flush;


			oif.oneInvariantFactor (sf[mid - 1], A, mid, PrimeL);


			report << mid <<"-th invairant factor of A = " ;
			r.write (report, sf[mid -1]);
			report << "\n" << std::flush;


			// recursively binary search all k-invariant factors, where i <= k <= mid

			if (r. areEqual (sf[mid-1], sf[j-1]))
				smithFormBinarySearchBackward (sf, A, i, mid, 2 * depth, PrimeL);
			else
				smithFormBinarySearchBackward (sf, A, i, mid, depth, PrimeL);

			// recurseively binary search all k-invariant factors, where mid <= k < j
			smithFormBinarySearchBackward (sf, A, mid, j, depth, PrimeL);

			return sf;
		}
	};

}

#endif  //__LINBOX_smith_form_binary_H

// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End: