This file is indexed.

/usr/include/linbox/field/block-ring.h is in liblinbox-dev 1.3.2-1.1+b1.

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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* linbox/fields/blas-ring.h
 * Copyright (C) 2007 LinBox Team
 *
 * Written by JP May, with tweaks by D. Saunders, Z. 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_blockring_H
#define __LINBOX_blockring_H
#include <iostream>
#include "linbox/matrix/blas-matrix.h"
#include "linbox/field/field-interface.h"
#include "linbox/algorithms/blas-domain.h"
#include <fflas-ffpack/fflas/fflas.h>

namespace LinBox
{


	/** Elements are wrapped BlasMatrix objects.

	  Operations expect conformal sizes for inputs and outputs.

	  You can expect good performance due to BLAS usage,
	  especially when the Field is Modular<double> or Modular<float>.
	  */

	template < class _Field >
	class BlockRing : public FieldInterface {
	public:
		_Field _field;
		BlasMatrixDomain<_Field> _blasMatrixDomain;
		size_t _b;

		typedef BlasMatrix<_Field> Matrix;
		typedef typename _Field::Element Scalar;



		/// default constructable wrapper for BlasMatrix
		class Element {

		public:

			typedef _Field Field;
			typedef Scalar Entry;

			Element() :
				matrix(0)
			{}

			~Element() {
				release();
			}

			// copy constructor
			Element(const Element& e) :
				matrix(0)
			{
				if (e.matrix != 0) {
					matrix = new Matrix(*(e.matrix));
					// memory leak of previous value?
				}
			}

			// overload assignment
			Element& operator= (const Element& e) {
				if (matrix == e.matrix) {
					return *this;
				}
				else if (e.matrix == 0) {
					release();
					return *this;
				}
				else {
					//set(new Matrix(*(e.matrix))); // does this really copy?
					clone(e);
					return *this;
				}
			}

			void clone(const Element& A) {
				// make this a deep copy of A
				// BlasMatrix copy constructor is shallow!

				release();

				if(A.matrix == 0) return;

				size_t rows = A.matrix->rowdim();
				size_t cols = A.matrix->coldim();

				set(new Matrix(A.matrix->field(),rows, cols));

				Scalar* a=A.matrix->getPointer();
				Scalar* b=  matrix->getPointer();

				for(size_t i=0; i < rows*cols; ++i) {
					*b=*a;
					++a; ++b;
				}


			}

			// cleanly deletes the current stored value
			// before assigning to the new value
			void set(Matrix* thematrix) {
				this -> release();
				matrix = thematrix;
			}

			Matrix* matrix;

		private:

			void release() {
				if (matrix != 0 )
					delete matrix;
				matrix = 0;
			}

		}; // class Element

		Element one,zero,mOne;

		class RandIter {
			typedef typename _Field::RandIter FieldRandIter;

			FieldRandIter r;
			size_t dim;

		public:
			RandIter(const BlockRing<_Field>& BR,
				 const integer size=0,
				 const integer seed=0) :
				r(BR._field, size, seed), dim(BR._b) {}

			Element& random(Element& e) const
			{
				// e must be init'd
				for(size_t i=0; i < e. matrix -> rowdim(); ++i)
					for(size_t j=0; j < e. matrix -> coldim(); ++j)
						r.random(e.matrix->refEntry(i,j));
				return e;
			}

		}; //class RandIter


		BlockRing(const _Field& F, size_t d=1) :
			_field(F), _blasMatrixDomain(F), _b(d)
		{
			one.set(new Matrix(_field,d,d));
			zero.set(new Matrix(_field,d,d));
			mOne.set(new Matrix(_field,d,d)) ;
			_blasMatrixDomain.setIdentity(*(one.matrix));
			_blasMatrixDomain.setZero(*(zero.matrix));
			for (size_t i = 0 ;i < d ;++i)
				mOne.matrix->setEntry(i,i,_field.mOne);
		}

		Element& init(Element& B) const
		{
			// B is garbage from memory
			B.set(new Matrix(_field,_b,_b));
			return B;
		}

		template <typename ints>
		Element& init(Element& B, ints n, size_t r = 0, size_t c = 0) const
		// n supposed to be integer, r num rows, c num cols
		{
			// default block dim is default dim of ring, but others are allowed.
			if (r == 0) r = _b;
			if (c == 0) c = _b;

			B.set(new Matrix(_field,r,c));

			size_t k = ( (r < c) ? r : c );

			typename _Field::Element N; _field.init(N, n);

			for (size_t i = 0; i < k; ++i) (B.matrix)->setEntry(i, i, N);

			return B;

		}


		template <typename ints>
		ints& convert(ints& x) const
		{
			return _field.convert(x);
		}


		template <typename ints>
		ints& convert(ints& x, const Element &A) const
		{
			return _field.convert(x, *(A.matrix->getPointer()));
		}

		Element& assign(Element &A, const Element &B) const
		{
			return A = B;
		}


		integer& cardinality(integer &c) const
		{
			// c = p^(b^2)

			_field.cardinality(c);

			if(c > 1) // _field is a finite field
			{
				integer tmp, n;
				n = _b*_b;
				c = expt(tmp, c, n);
			} // else c  = -1

			return c;
		}

		integer& characteristic(integer &c) const
		{
			return _field.characteristic(c);
		}

		unsigned long cardinality() const
		{
			return _field. cardinality() ;
		}

		unsigned long characteristic() const
		{
			return _field. characteristic() ;
		}



		size_t dim() const
		{
			return _b;
		}


		//Operations
		//
		// All operations will work for matrices of dim != _b
		// but assume that the dimensions of all the given matrices
		// are compatible with the dimensions of the A operand.

		//Operations from the Matrix Domain:

		Element& mul(Element& C, const Element& A, const Element& B) const
		{
			_blasMatrixDomain.mul(*(C.matrix), *(A.matrix), *(B.matrix));
			return C;
		}


		//non-commutative: use mulin_left: A = A*B
		Element& mulin(Element& A, const Element& B) const
		{
			_blasMatrixDomain.mulin_left(*(A.matrix), *(B.matrix));
			return A;
		}

		// D = A*X+Y
		Element& axpy(Element& D, const Element& A, const Element& X, const Element& Y) const
		{
			_blasMatrixDomain.axpy(*(D.matrix), *(A.matrix), *(X.matrix), *(Y.matrix));
			return D;
		}

		// R = A*X+R
		Element& axpyin(Element& R, const Element& A, const Element& X) const
		{
			_blasMatrixDomain.axpyin(*(R.matrix), *(A.matrix), *(X.matrix));
			return R;
		}


		// These operations will not work for all elements
		// and no checks are provided!

		// B = A^{-1}
		Element& inv(Element& B, const Element& A) const
		{

			int nullflag = 0;

			_blasMatrixDomain.inv(*(B.matrix), *(A.matrix), nullflag);

			if (nullflag)
				throw PreconditionFailed(__func__,__FILE__,__LINE__,"InvMatrix: inverse undefined");

			return B;
		}

		// A=A^{-1} not really inplace!
		Element& invin(Element& A) const
		{

			int nullflag = 0;

			//_blasMatrixDomain.invin(A, A, nullflag);

			Element B;  init(B, A.matrix->rowdim(), A.matrix->coldim());
			_blasMatrixDomain.inv(*(B.matrix), *(A.matrix), nullflag);

			if (nullflag)
				throw PreconditionFailed(__func__,__FILE__,__LINE__,"InvMatrix: inverse undefined");

			A=B;

			return A;
		}


		// C = A*B^{-1}
		Element& div(Element& C, const Element& A, const Element& B) const
		{

			_blasMatrixDomain.right_solve(*(C.matrix),*(B.matrix),*(A.matrix));
			return C;
		}


		//A = A*B^{-1};
		Element& divin( Element& A, const Element& B) const
		{
			_blasMatrixDomain.right_solve(*(B.matrix),*(A.matrix));
			return A;
		}




		// Unwrapped operations using simple loops:

		// C = A + B
		Element& add(Element& C, const Element& A, const Element& B) const
		{
			size_t rows = A.matrix->rowdim();
			size_t cols = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();
			Scalar* c=C.matrix->getPointer();

			//FFLAS::fcopy((typename Field::Father_t)_field, rows*cols, b, 1, c, 1); // C = B


			for(size_t i=0; i < rows*cols; ++i) {
				_field.add(*c,*a,*b);
				++a; ++b; c++;
			}

			//Scalar alpha; _field.init(alpha, 1);
			//FFLAS::faxpy((typename Field::Father_t)_field, rows*cols, alpha, a, 1, c, 1);

			return C;
		}

		// A = A + B
		Element& addin(Element& A, const Element& B) const
		{
			size_t r = A.matrix->rowdim();
			size_t c = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i) {
				_field.addin(*a,*b);
				++a; ++b;
			}

			return A;

		}


		// C = A - B
		Element& sub(Element& C, const Element& A, const Element& B) const
		{
			size_t rows = A.matrix->rowdim();
			size_t cols = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();
			Scalar* c=C.matrix->getPointer();


			for(size_t i=0; i < rows*cols; ++i) {
				_field.sub(*c,*a,*b);
				++a; ++b; c++;
			}

			return C;
		}


		// A = A - B
		Element& subin(Element& A, const Element& B) const
		{
			size_t r = A.matrix->rowdim();
			size_t c = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i) {
				_field.subin(*a,*b);
				++a; ++b;
			}

			return A;
		}


		//B = -1*A
		Element& neg(Element& B, const Element& A) const
		{
			size_t r = A.matrix->rowdim();
			size_t c = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i) {
				_field.neg(*b,*a);
				++a; ++b;
			}

			return B;
		}


		//A = -1*A
		Element& negin(Element& A) const
		{
			size_t r = A.matrix->rowdim();
			size_t c = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i) {
				_field.negin(*a);
				++a;
			}

			return A;
		}

		bool areEqual(const Element& A, const Element& B) const
		{

			size_t r = A.matrix->rowdim();
			size_t c = A.matrix->coldim();

			Scalar* a=A.matrix->getPointer();
			Scalar* b=B.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i) {

				if(!_field.areEqual(*a,*b)) {
					return false;
				}

				++a; ++b;
			}
			return true;
		}


		bool isOne(const Element& X) const
		{
			size_t n = X.matrix->rowdim();

			if(n != X.matrix->coldim()) {
				return false;
			}

			Scalar* x=X.matrix->getPointer();

			for(size_t i=1; i <= n; ++i)
				for(size_t j=1; j <= n; ++j)
				{
					if(i==j) { // on the diagonal
						if(!_field.isOne(*x)) {
							return false;
						}
					}
					else {
						if(!_field.isZero(*x)) {
							return false;
						}
					}

					x++;
				}


			return true;
		}


		bool isZero(const Element& X) const
		{

			size_t r = X.matrix->rowdim();
			size_t c = X.matrix->coldim();

			Scalar* x=X.matrix->getPointer();

			for(size_t i=0; i < r*c; ++i)
			{
				if(!_field.isZero(*x)) {
					return false;
				}

				x++;
			}

			return true;
		}


		//stubs for read and write field
		std::ostream& write(std::ostream& os) const
		{
			return _field.write(os << "Dimension " << _b << " square matrices over ");
		}



		std::istream& read(std::istream& is)
		{
			return is;
		}

		// wrapped read and write element
		std::ostream& write(std::ostream& os, const Element& A) const
		{
			return (A.matrix)->write(os << std::endl);
		}


		std::istream& read(std::istream& is, const Element& A) const
		{

			return (A.matrix)->read(is, _field);
		}


	private:


		// recursive helper to compute exponentiation of integers
		static	  integer& expt (integer& res, integer& a, integer& n)
		{
			if (n == 0) {
				res=1;
			}
			else if (n == 1) {
				res=a;
			}
			else if (n[0] & 1) {
				n -= 1;
				expt(res, a, n);
				res*=a;
			}
			else {
				n /= 2;
				expt(res, a, n);
				res*=res;
			}

			return res;
		}



	}; // BlockRing

} // LinBox

#endif // __LINBOX_blockring_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: