This file is indexed.

/usr/include/linbox/algorithms/matrix-blas3/mul-toomcook.inl 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
/*  Copyright (C) 2012 the members of the LinBox group
 * Written by B. Boyer < bboyer@imag.fr >
 *
 * This file is part of the LinBox library.
 *
 * ========LICENCE========
 * 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.
 *
 * LinBox 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========
 *
 */

/*! @internal
 * @file matrix-blas3/mul-toomcook.inl
 * @ingroup algorithm
 * @ingroup blas
 * @brief Implementation of Toom-Cook.
 */

#ifndef __LINBOX_matrix_blas3_mul_toomcook_INL
#define __LINBOX_matrix_blas3_mul_toomcook_INL



#include <fflas-ffpack/fflas/fflas.h>
#include <fflas-ffpack/ffpack/ffpack.h>

namespace LinBox {
	namespace BLAS3 {

		template<class Zpz>
		BlasMatrix<Zpz>& ToomCook(BlasMatrix<Zpz>& TC, BlasMatrix<Zpz>& iTC)
		{
			linbox_check(TC.rowdim() == TC.coldim());
			size_t l = TC.rowdim();

			for (size_t i = 0 ; i < l ; ++i)
				for (size_t j = 0 ; j < l ; ++j) {
					TC.field().init(TC.refEntry(i,j), pow((Integer)i,j));
				}
			BlasMatrixDomain<Zpz> BMD(TC.field()) ;
			// TC.write(std::cout << "TC ") << std::endl;
			// BMD.invert(iTC, TC);
			int null;
			FFPACK::Invert((typename Field::Father_t)TC.field(),l,TC.getPointer(),l,iTC.getWritePointer(),l,null);
			// iTC.write(std::cout << "TC^(-1) ") << std::endl;
			// TC.write(std::cout << "TC ") << std::endl;
			return TC;
		}

		namespace Protected {


			template<class Zpz, class GFpe>
			BlasMatrix<Zpz >& mul (BlasMatrix<Zpz>      & CMatBloc,
								   const BlasMatrix<Zpz>& AMatBloc,
								   const BlasMatrix<Zpz>& BMatBloc,
								   const size_t m,
								   const size_t k,
								   const size_t n,
								   const mulMethod::ToomCook<GFpe> & T)
			{
#if (__LINBOX_FFLAS_FFPACK_VERSION < 10501)
#warning "Invert is buggy in your fflas-ffpack version. please consider upgrading to >=1.5.1."
#endif
				const Zpz & F  = CMatBloc.field();
				const GFpe& GF = T._myF ;
				// linbox_check(T._myF.characacteristic() == F.characteristic());
				// TODO si e = 1 on matmul !
				size_t e = (size_t) GF.exponent() ; // extension degree
				size_t l = 2*e - 1 ; // sure ?

				BlasMatrix<Zpz> TC    (F,l,l);
				BlasMatrix<Zpz> iTC   (F,l,l);
				BlasMatrix<Zpz> iEval (F,l,l);
				// FWD = Matrix(K, l, l, [K(i**j) for i in range(l) for j in range(l)])
				ToomCook(TC,iTC);
				// each row is a result matrix
				BlasMatrix<Zpz> TMatBloc( F, l, m*n);


				// AY = [sum(FWD[i,j]*A[j] for j in range(len(A))) for i in range(l)]
				// BY = [sum(FWD[i,j]*B[j] for j in range(len(B))) for i in range(l)]
				if (!T.memory_unlimited)	{			/* space efficient */
					BlasMatrix<Zpz> AEval( F , m, k);
					BlasMatrix<Zpz> BEval( F , k, n);


					for (size_t i = 0 ; i < l ; ++i) {
						FFLAS::fgemv((typename Field::Father_t)F, FFLAS::FflasTrans,
									 e, m*k,
									 F.one,
									 AMatBloc.getPointer(), m*k,
									 TC.getPointer()+ i*l, 1,
									 F.zero,
									 AEval.getWritePointer(), 1);

						FFLAS::fgemv((typename Field::Father_t)F, FFLAS::FflasTrans,
									 e, k*n,
									 F.one,
									 BMatBloc.getPointer(), k*n,
									 TC.getPointer()+ i*l, 1,
									 F.zero,
									 BEval.getWritePointer(), 1);

						FFLAS::fgemm((typename Field::Father_t)F,
									 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
									 // m, k , n,
									 m,n,k,
									 F.one,
									 AEval.getPointer(), AEval.coldim(), //lda
									 BEval.getPointer(), BEval.coldim(), //ldb
									 F.zero,
									 TMatBloc.getWritePointer()+i*m*n, n);
					}
				}
				else { /* time efficient (matmul) */
					BlasMatrix<Zpz> AEval( F , l, m*k);
					BlasMatrix<Zpz> BEval( F , l, k*n);


					FFLAS::fgemm((typename Field::Father_t)F,
								 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
								 // m, k , n,
								 l,m*k,e,
								 F.one,
								 // AEval.getPointer(), AEval.coldim(), //lda
								 TC.getPointer(),l,
								 AMatBloc.getPointer(), m*k,
								 F.zero,
								 AEval.getWritePointer(), m*k);
					// TMatBloc.getWritePointer()+i*m*n, n);

					FFLAS::fgemm((typename Field::Father_t)F,
								 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
								 // m, k , n,
								 l,n*k,e,
								 F.one,
								 // AEval.getPointer(), AEval.coldim(), //lda
								 TC.getPointer(),l,
								 BMatBloc.getPointer(), n*k,
								 F.zero,
								 BEval.getWritePointer(), n*k);

					for (size_t i = 0 ; i < l ; ++i) {

						FFLAS::fgemm((typename Field::Father_t)F,
									 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
									 // m, k , n,
									 m,n,k,
									 F.one,
									 AEval.getPointer()+i*m*k, k, //lda
									 BEval.getPointer()+i*n*k, n, //ldb
									 F.zero,
									 TMatBloc.getWritePointer()+i*m*n, n);
					}
				}

				std::vector<double>  minpoly_vector  =  GF.irreducible() ;
				for (size_t i = 0 ; i < minpoly_vector.size(); ++i)
					F.negin(minpoly_vector[i]);

				// BlasSubmatrix<Zpz> CompMat(CMatBloc,0,0,l,l);
				BlasMatrix<Zpz> CompMat(F,l,l);
				for (size_t i = 0 ; i < e ; ++i) { // degree == l ?
					CompMat.setEntry(i,i,F.one);
				}
				for (size_t j = 0 ; j < e-1 ; ++j) {
					CompMat.setEntry(j,e,minpoly_vector[j]);
				}
				typename Zpz::Element coeff ;
				typename Zpz::Element tmp_coeff, tmp_coeff2 ;
				F.init(tmp_coeff);
				for (size_t i = 1 ; i < e-1 ; ++i){
					for (size_t j = 1 ; j < e+1 ; ++j) {
						CompMat.setEntry(j, i + e, CompMat.getEntry((j-1),i - 1 + e) ) ;
					}
					if (!F.isZero(CompMat.getEntry(e,i+e))){
						F.init(coeff, CompMat.getEntry( e, i + e) );
						for(size_t j = 0 ; j < e-1 ; ++j){
							F.mul(tmp_coeff, coeff,minpoly_vector[j]);
							F.init(tmp_coeff2,CompMat.getEntry(j,i+e));
							F.addin(tmp_coeff2, tmp_coeff);
							CompMat.setEntry(j,i+e, tmp_coeff2 );
						}
					}
				}

				// BCK = ~FWD
				// XXX some stuff here
				FFLAS::fgemm((typename Field::Father_t)F,
							 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
							 l, l, l, F.one,
							 CompMat.getPointer(), l,
							 iTC.getPointer(), l,
							 F.zero,
							 iEval.getWritePointer(), l);


				// Y = [sum(BCK[i,j]*Y[j] for j in range(l)) for i in range(l)]
				FFLAS::fgemm((typename Field::Father_t)F,
							 FFLAS::FflasNoTrans, FFLAS::FflasNoTrans,
							 e,m*n,l,
							 F.one,
							 iEval.getPointer(), l,
							 TMatBloc.getPointer(), m*n,
							 F.zero,
							 CMatBloc.getWritePointer(), m*n);
				return CMatBloc;

			}
		}

		template<class Zpz, class GF>
		std::vector<BlasMatrix<Zpz> >& mul (std::vector<BlasMatrix<Zpz> >& C,
											const std::vector<BlasMatrix<Zpz> >& A,
											const std::vector<BlasMatrix<Zpz> >& B,
											const mulMethod::ToomCook<GF> & T)
		{
			size_t m = C[0].rowdim();
			size_t k = B[0].rowdim();
			size_t n = C[0].coldim();
			const Zpz & F = C[0].field();
			BlasMatrix<Zpz> Cbloc(F,C.size(),m*n);
			BlasMatrix<Zpz> Abloc(F,C.size(),m*k);
			BlasMatrix<Zpz> Bbloc(F,C.size(),k*n);
			// convert
			for (size_t l = 0 ; l < C.size() ; ++l){
				for (size_t i = 0 ; i < m ; ++i) {
					for (size_t j = 0 ; j < k ; ++j) {
						Abloc.setEntry(l,i*k+j,A[l].getEntry(i,j));
					}
				}
			}

			for (size_t l = 0 ; l < C.size() ; ++l){
				for (size_t i = 0 ; i < k ; ++i) {
					for (size_t j = 0 ; j < n ; ++j) {
						Bbloc.setEntry(l,i*n+j,B[l].getEntry(i,j));
					}
				}
			}


			Protected::mul(Cbloc,Abloc,Bbloc,m,k,n,T);

			for (size_t l = 0 ; l < C.size() ; ++l){
				for (size_t i = 0 ; i < m ; ++i) {
					for (size_t j = 0 ; j < n ; ++j) {
						C[l].setEntry(i,j,Cbloc.getEntry(l,i*n+j));
					}
				}
			}
			// convert back
		}

		template<class Zpz>
		BlasMatrix<GivaroExtension<Zpz> >&
		mul (BlasMatrix<GivaroExtension<Zpz> >& C,
			 const BlasMatrix<GivaroExtension<Zpz> >& A,
			 const BlasMatrix<GivaroExtension<Zpz> >& B,
			 const mulMethod::ToomCook<GivaroExtension<Zpz> > & T)
		{
			size_t m = C.rowdim();
			size_t k = B.rowdim();
			size_t n = C.coldim();
			Zpz F ( A.field().characteristic() ); // BaseField ?
			size_t e = (size_t) A.field().exponent();
			BlasMatrix<Zpz> Cbloc(F,e,m*n);
			BlasMatrix<Zpz> Abloc(F,e,m*k);
			BlasMatrix<Zpz> Bbloc(F,e,k*n);


			for (size_t l = 0 ; l < e ; ++l){
				for (size_t i = 0 ; i < m ; ++i) {
					for (size_t j = 0 ; j < k ; ++j) {
						if (l< A.getEntry(i,j).size())
							Abloc.setEntry(l,i*k+j,
										   A.getEntry(i,j)[l]);
					}
				}
			}
			// for (size_t l = 0 ; l < e ; ++l){
				// std::cout << Abloc.getEntry(l,0) << ';';
			// }
			// std::cout << std::endl;

			for (size_t l = 0 ; l < e ; ++l){
				for (size_t i = 0 ; i < k ; ++i) {
					for (size_t j = 0 ; j < n ; ++j) {
						if (l< B.getEntry(i,j).size())
							Bbloc.setEntry(l,i*n+j,B.getEntry(i,j)[l]);
					}
				}
			}

			// C.field().init(C.refEntry(0,0));

			Protected::mul(Cbloc,Abloc,Bbloc,m,k,n,T);
			// convert back

			typedef typename GivaroExtension<Zpz>::Element Element ;
			for (size_t i = 0 ; i < m ; ++i) {
				for (size_t j = 0 ; j < n ; ++j) {
					Element x(e) ;
					for (size_t l = 0 ; l < e ; ++l){
						x[l] = Cbloc.getEntry(l,i*n+j);
					}
					C.field().polynomial_domain().modin(x,C.field().irreducible());
					// A.field().convert((Element&)C.refEntry(i,j),x);
					// A.field().init(x);
					C.setEntry(i,j,x);
				}
			}
			// for (size_t l = 0 ; l < e ; ++l){
				// std::cout << Cbloc.getEntry(l,0) << ';';
			// }
			// std::cout << std::endl;

		}

	}
}

#endif // __LINBOX_matrix_blas3_mul_toomcook_INL

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