File: test-common.h

package info (click to toggle)
linbox 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,940 kB
  • sloc: cpp: 108,392; lisp: 5,469; makefile: 1,345; sh: 1,244; csh: 131; python: 74; perl: 2
file content (395 lines) | stat: -rw-r--r-- 9,486 bytes parent folder | download | duplicates (4)
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
/* linbox/tests/test-common.C
 * Copyright (C) 2001, 2002 Bradford Hovinen
 *
 * Written by Bradford Hovinen <hovinen@cis.udel.edu>
 *
 * ------------------------------------
 * Modified by Dmitriy Morozov <linbox@foxcub.org>. May 27, 2002.
 *
 * Added parametrization to the VectorCategory tags to make them fit the
 * Rootbeer meeting design of VectorCategories being parametrized by
 * VectorTraits.
 *
 * ------------------------------------
 *
 *
 * ========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_test_common_H
#define __LINBOX_test_common_H

#include <iostream>
#include <fstream>
// #include <vector>

#include "linbox/linbox-config.h"
#include "linbox/field/archetype.h"
#include "linbox/integer.h"
#include "linbox/vector/vector-domain.h"
#include "linbox/vector/blas-vector.h"

using namespace std;
#include "linbox/util/commentator.h"
#include "linbox/util/args-parser.h"


template <class Field, class Vector>
void printVectorSpecialized(
			    Field &F,
			    ostream &output,
			    const Vector &v,
			    LinBox::VectorCategories::DenseVectorTag tag
			   )
{
	unsigned int i;

	output << '(';
	for (i = 0; i < v.size (); i++) {
		F.write (output, v[(size_t)i]);
		if (i < v.size () - 1)
			output << ", ";
	}
	output << ')' << endl;
}

template <class Field, class Vector>
void printVectorSpecialized(
			    Field &F,
			    ostream &output,
			    const Vector &v,
			    LinBox::VectorCategories::SparseSequenceVectorTag tag
			   )
{
	typename Vector::const_iterator i;
	unsigned int j;

	output << '(';
	for (i = v.begin (), j = 0; i != v.end (); j++) {
		while (j < (*i).first) {
			output << "0, ";
			j++;
		}

		F.write (output, (*i).second);

		if (++i != v.end ())
			output << ", ";
	}
	output << ')' << endl;
}

template <class Field, class Vector>
void printVectorSpecialized(
			    Field &F,
			    ostream &output,
			    const Vector &v,
			    LinBox::VectorCategories::SparseAssociativeVectorTag tag
			   )
{
	typename Vector::const_iterator i;
	unsigned int j;

	output << '(';
	for (i = v.begin (), j = 0; i != v.end (); j++) {
		while (j < (*i).first) {
			output << "0, ";
			j++;
		}

		F.write (output, (*i).second);

		if (++i != v.end ())
			output << ", ";
	}
	output << ')' << endl;
}

template <class Field, class Vector>
void printVector (Field &F, ostream &output, const Vector &v)
{
	printVectorSpecialized(F, output, v, typename LinBox::VectorTraits<Vector>::VectorCategory());
}

template <class Field, class Vector>
bool areVectorsEqual (Field &F, const Vector &v, const Vector &w)
{
	return areVectorsEqualSpecialized(F, v, w, LinBox::VectorTraits<Vector>::VectorCategory());
}

template <class Field, class Vector>
bool areVectorsEqualSpecialized(
				Field &F,
				const Vector &v,
				const Vector &w,
				LinBox::VectorCategories::DenseVectorTag tag
			       )
{
	if (v.size() != w.size()) return false;

	for (size_t i = 0; i < v.size(); i++)
		if (!F.areEqual (w[(size_t)i], v[(size_t)i]))
			return false;

	return true;
}

template <class Field, class Vector>
bool areVectorsEqualSpecialized(
				Field &F,
				const Vector &v,
				const Vector &w,
				LinBox::VectorCategories::SparseSequenceVectorTag tag
			       )
{
	if (v.size() != w.size()) return false;

	typename Vector::const_iterator v_iter, w_iter;
	w_iter = w.begin();

	for ( v_iter = v.begin(); v_iter != v.end(); ++v_iter, ++w_iter)
		if ( (w_iter->first != v_iter->first)
		     || (!F.areEqual (w_iter->second, v_iter->second)) )
			return false;

	return true;
}

template <class Field, class Vector>
bool areVectorsEqualSpecialized(
				Field &F,
				const Vector &v,
				const Vector &w,
				LinBox::VectorCategories::SparseAssociativeVectorTag tag
			       )
{
	if (v.size() != w.size()) return false;

	typename Vector::const_iterator v_iter, w_iter;
	w_iter = w.begin();

	for ( v_iter = v.begin(); v_iter != v.end(); ++v_iter, ++w_iter)
		if ( (w_iter->first != v_iter->first)
		     || (!F.areEqual (w_iter->second, v_iter->second)) )
			return false;

	return true;
}

template <class Field, class Vector>
bool allZero (Field &F, const Vector &v)
{
       	return allZeroSpecialized(F, v, LinBox::VectorTraits<Vector>::VectorCategory());
}

template <class Field, class Vector>
bool allZeroSpecialized(
			Field &F,
			const Vector &v,
			LinBox::VectorCategories::DenseVectorTag tag
		       )
{
	for (size_t i = 0; i < v.size(); i++)
		if (!F.isZero (v[(size_t)i]))
			return false;

	return true;
}

template <class Field, class Vector>
bool allZeroSpecialized(
			Field &F,
			const Vector &v,
			LinBox::VectorCategories::SparseSequenceVectorTag tag
		       )
{
	if (0 != v.size())
		return false;
	else
		return true;
}

template <class Field, class Polynomial>
void printPolynomial (Field &F, ostream &output, const Polynomial &v)
{
	int i;
	size_t val;

	for (val = 0; val < v.size () && F.isZero (v[val]); val++) ;

	if (v.size () == 0 || val == v.size ())
		output << "0";

	for (i = (int)v.size () - 1; i >= 0; i--) {
		if (F.isZero (v[(size_t)i]))
			continue;

		if (!F.isOne (v[(size_t)i]) || i == 0)
			F.write (output, v[(size_t)i]);

		if (i > 0)
			output << " x^" << i;

		if (i > (int) val)
			output << " + ";
	}

	output << endl;
}

template <class Field, class Blackbox, class Polynomial, class Vector>
LinBox::BlasVector <Field> &
applyPoly (const Field                             &F,
	   Vector                                  &w,
	   const Blackbox			   &A,
	   const Polynomial                        &phi,
	   const Vector                            &v)
{
	LinBox::VectorDomain <Field> VD (F);
	Vector z(F);
	int i;

	LinBox::VectorWrapper::ensureDim (z, A.rowdim ());

	VD.mul (w, v, phi[phi.size () - 1]);

	for (i = (int)phi.size () - 2; i >= 0; i--) {
		A.apply (z, w);
		VD.axpy (w, phi[(size_t)i], v, z);
	}

	return w;
}

/* Evaluate polynomial at a whole vector of points */

template <class Field, class Polynomial, class Vector>
Vector &
multiEvalPoly (const Field        &F,
	       Vector             &w,
	       const Polynomial   &phi,
	       const Vector       &v)
{

	typename Field::Element tmp;
	int i;
	size_t j;

	w.resize (v.size ());

	for (j = 0; j < v.size (); j++)
		w[(size_t)j] = phi[phi.size () - 1];

	for (i = (int)phi.size () - 2; i >= 0; i--) {
		for (j = 0; j < v.size (); j++) {
			F.axpy (tmp, w[(size_t)j], v[(size_t)j], phi[(size_t)i]);
			w[(size_t)j] = tmp;
		}
	}

	return w;
}

/* Interpolate polynomial evaluated at a vector of points using Lagrange
 * interpolants */

template <class Field, class Polynomial>
Polynomial &
interpolatePoly (const Field             &F,
		 Polynomial              &f,
		 const LinBox::BlasVector<Field> &x,
		 const LinBox::BlasVector<Field> &y)
{
	typedef LinBox::BlasVector<Field> Vector;

	int n = (int)x.size ();

	// NB I leave one element in g always initialized to 0 as the ficticious
	// negative-first coefficient. This streamlines some of the code.
	static const int g_FUDGE = 1;
	Vector g(F,(size_t)(n + g_FUDGE));
	F.assign (g[0], F.zero);

	typename Field::Element gk, c1, c2;

	int i, j, k, d;

	f.resize ((size_t)n);

	for (i = 0; i < n; i++)
		F.assign(f[(size_t)i], F.zero);

	for (j = 0; j < n; j++) {
		F.assign (g[0 + g_FUDGE], F.one);

		// d is the current degree of the Lagrange interpolant. i is the
		// current index in the array of x-coordonites
		for (d = 0, i = 0; d < n - 1; d++, i++) {
			if (i == j) i++;

			// Compute coefficients of this factor.
			F.sub (c1, x[(size_t)j], x[(size_t)i]);
			F.invin (c1);
			F.mul (c2, c1, x[(size_t)i]);
			F.negin (c2);

			// Initialize the next element of the Lagrange interpolant
			F.assign (g[(size_t)(d + 1 + g_FUDGE)], F.zero);

			// Multiply this factor by the existing partial product
			for (k = d + 1 + g_FUDGE; k >= g_FUDGE; k--) {
				F.mul (gk, g[(size_t)k - 1], c1);
				F.axpyin (gk, g[(size_t)k], c2);
				g[(size_t)k] = gk;
			}
		}

		for (i = 0; i < n; i++)
			F.axpyin (f[(size_t)i], y[(size_t)j], g[(size_t)i + g_FUDGE]);
	}

	return f;
}


bool isPower        (LinBox::integer n, LinBox::integer m);

/* Give an approximation of the value of the incomplete gamma function at a, x,
 * to within the tolerance tol */

extern inline double incompleteGamma (double a, double x, double tol);

/* Give the value of the chi-squared cumulative density function for given
 * value of chi_sqr and the given degrees of freedom */

double chiSquaredCDF (double chi_sqr, double df);

#ifdef LinBoxTestOnly
#include "test-common.inl"
#endif
#endif // __LINBOX_test_common_H

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