My Project
coeffs_test.h
Go to the documentation of this file.
1#include "misc/auxiliary.h"
2
3#include "reporter/reporter.h"
5
6#include "coeffs/coeffs.h"
7#include "coeffs/numbers.h"
8
9// the following headers are private...
10#include "coeffs/longrat.h"
11#include "coeffs/gnumpfl.h"
12#include "coeffs/gnumpc.h"
13#include "coeffs/shortfl.h"
14#include "coeffs/ffields.h"
15#include "coeffs/modulop.h"
16#include "coeffs/rmodulon.h"
17#include "coeffs/rmodulo2m.h"
18#include "coeffs/rintegers.h"
19
20
21#include "common.h"
22using namespace std;
23
24
25void TestSum(const coeffs r, const unsigned long N)
26{
27 clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: ");
28 clog << endl;
29
30 assume( N > 0 ); // just for now...
31
32 const unsigned long ssss = (N * (N+1)) / 2;
33
34 number sum1 = n_Init(ssss, r);
35 clog<< "N*(N+1)/2 (int: " << ssss << "): "; PrintSized(sum1, r);
36
37 number s, ss, i, res;
38
39 s = n_Init(N , r);
40 i = n_Init(N+1, r);
41 n_InpMult(s, i, r);
42 n_Delete(&i, r);
43
44 clog<< "N*(N+1): ("<< N*(N+1) << ")"; PrintSized(s, r);
45
46 i = n_Init(2, r);
47 clog<< "2: "; PrintSized(i, r);
48
49 if( !n_IsZero( i, r) )
50 {
51#ifdef HAVE_RINGS
52 TS_ASSERT( n_DivBy(s, i, r) );
53#endif
54
55 res = n_Div(s, i, r);
56
57 clog<< "N*(N+1)/2: "; PrintSized(res, r);
58
59
60 number d = n_Sub(res, sum1, r);
62 n_Delete(&d, r);
63
64 if( n_GetChar(r) == 0 )
65 {
66 TS_ASSERT( n_Equal(sum1, res, r) );
67 TS_ASSERT( n_Equal(res, sum1, r) );
68 }
69 } else
71
72
73 n_Delete(&s, r); n_Delete(&i, r);
74
75 n_Delete(&sum1, r); n_Delete(&res, r);
76
77
78 s = n_Init(0 , r);
79 ss = n_Init(0 , r);
80 for( int k = N; k >= 0; k-- )
81 {
82 i = n_Init(k, r);
83 n_InpAdd(s, i, r); // s += i
84
85 i = n_InpNeg(i, r);
86 n_InpAdd(ss, i, r); // ss -= i
87
88 n_Delete(&i, r);
89 }
90 clog<< "ss: "; PrintSized(ss, r);
91
92 ss = n_InpNeg(ss, r); // ss = -ss
93
94 clog<< "real sum : "; PrintSized(s, r);
95 clog<< "real sum(--): "; PrintSized(ss, r);
96
97 TS_ASSERT( n_Equal(s, ss, r) );
98 TS_ASSERT( n_Equal(ss, s, r) );
99
100 n_Delete(&s, r);
101 n_Delete(&ss, r);
102
103 clog << ( " >>> TEST DONE!" );
104 clog << endl;
105
106}
107
108
109void TestArith(const coeffs r)
110{
111 clog << ("TEST: Simple Arithmetics: ");
112 clog << endl;
113
114 number two = n_Init(2, r);
115
116 number t = n_Init(1, r);
117 n_InpAdd(t, t, r);
118 TS_ASSERT( n_Equal(two, t, r) );
119 n_Delete(&t, r);
120
121 if( getCoeffType(r) == n_Q )
122 {
123 number t = n_Init(1, r);
124 n_InpAdd(t, t, r);
125 TS_ASSERT( n_Equal(two, t, r) );
126 n_Delete(&t, r);
127 }
128
129
130
131
132 const int N = 66666;
133
134 number a = n_Init(N, r);
135
136 clog<< "a: "; PrintSized(a, r);
137
138
139 clog<< "two: "; PrintSized(two, r);
140
141 number aa0 = n_Init(N*2, r);
142
143 number aa = n_Add(a, a, r);
144
145 clog<< "aa = a + a: "; PrintSized(aa, r);
146
147 number aa2 = n_Mult(a, two, r);
148
149 clog<< "aa2 = a * 2: "; PrintSized(aa2, r);
150
151 number aa1 = n_Mult(two, a, r);
152
153 clog<< "aa1 = 2 * a: "; PrintSized(aa1, r);
154
155 n_Delete(&a, r);
156 n_Delete(&two, r);
157
158
159 a = n_Sub( aa, aa1, r );
160
161 clog<< "a = aa - aa1: "; PrintSized(a, r);
162
163 TS_ASSERT( n_IsZero(a, r) );
164
165 n_Delete(&a, r);
166
167 a = n_Sub( aa, aa2, r );
168
169 clog<< "a = aa - aa2: "; PrintSized(a, r);
170
171 TS_ASSERT( n_IsZero(a, r) );
172
173 n_Delete(&a, r);
174
175
176 a = n_Sub( aa1, aa2, r );
177
178 clog<< "a = aa1 - aa2: "; PrintSized(a, r);
179
180 TS_ASSERT( n_IsZero(a, r) );
181
182 n_Delete(&a, r);
183
184
185
186 TS_ASSERT( n_Equal(aa, aa1, r) );
187 TS_ASSERT( n_Equal(aa, aa2, r) );
188 TS_ASSERT( n_Equal(aa1, aa2, r) );
189
190 TS_ASSERT( n_Equal(aa0, aa, r) );
191 TS_ASSERT( n_Equal(aa0, aa1, r) );
192 TS_ASSERT( n_Equal(aa0, aa2, r) );
193
194 n_Delete(&aa, r);
195 n_Delete(&aa1, r);
196 n_Delete(&aa2, r);
197
198 n_Delete(&aa0, r);
199
200 clog << ( " >>> TEST DONE!" );
201 clog << endl;
202
203}
204
205
206
207
208
209BOOLEAN Test(const n_coeffType type, void* p = NULLp)
210{
211
212 clog << endl;
213 clog << ( "----------------------- Testing coeffs: [" + _2S(type) + ", " + _2S(p) + "]: -----------------------");
214 clog << endl;
215
216 const coeffs r = nInitChar( type, p );
217
218 if( r == NULLp )
219 {
220 clog << ( "Test: could not get this coeff. domain" );
221 return FALSE;
222 };
223
224 TS_ASSERT_DIFFERS( r->cfCoeffWrite, NULLp );
225
226 if( r->cfCoeffWrite != NULL )
227 {
228 clog << "Coeff-domain: " << endl;
229 n_CoeffWrite(r); PrintLn();
230 }
231
232 if (n_NumberOfParameters(r) > 0)
233 {
234 number z = n_Param(1, r); // also any integer instead of 0//?
235 PrintS("Parameter: "); PrintSized(z, r);
236 n_Delete(&z, r);
237 }
238
239
240 clog << "Char: " << n_GetChar(r) << endl;
241
242
244 nSetChar( r );
245 TS_ASSERT_EQUALS( getCoeffType(r), type );
246
247 TS_ASSERT_DIFFERS( r->cfInit, NULLp );
248 TS_ASSERT_DIFFERS( r->cfWriteLong, NULLp );
249 TS_ASSERT_DIFFERS( r->cfAdd, NULLp );
250 TS_ASSERT_DIFFERS( r->cfDelete, NULLp );
251
252 switch( type )
253 {
254 case n_Q:
255 {
256 //TS_ASSERT_EQUALS( r->cfInit, nlInit );
257 //TS_ASSERT_EQUALS( r->cfAdd, nlAdd );
258 //TS_ASSERT_EQUALS( r->cfDelete, nlDelete );
259
260 TS_ASSERT( nCoeff_is_Q( r ));
262
263 TS_ASSERT( !nCoeff_has_Units( r )); // ?
266
268 TS_ASSERT( !nCoeff_is_Zn( r ));
270 TS_ASSERT( !nCoeff_is_Z( r ));
272 TS_ASSERT( !nCoeff_is_Zp( r ));
274 TS_ASSERT( !nCoeff_is_R( r ));
275 TS_ASSERT( !nCoeff_is_GF( r ));
278 TS_ASSERT( !nCoeff_is_CF( r ));
280
281 break;
282 }
283 case n_long_R:
284 {
285 //TS_ASSERT_EQUALS( r->cfInit, ngfInit );
286 //TS_ASSERT_EQUALS( r->cfAdd, ngfAdd );
287 //TS_ASSERT_EQUALS( r->cfDelete, ngfDelete );
288 break;
289 }
290 case n_long_C:
291 {
292// TS_ASSERT_EQUALS( r->cfInit, ngcInit );
293// TS_ASSERT_EQUALS( r->cfAdd, ngcAdd );
294// TS_ASSERT_EQUALS( r->cfDelete, ngcDelete );
295 break;
296 }
297 case n_R:
298 {
299 //TS_ASSERT_EQUALS( r->cfInit, nrInit );
300 //TS_ASSERT_EQUALS( r->cfAdd, nrAdd );
301 // TS_ASSERT_EQUALS( r->cfDelete, nrDelete ); // No?
302 break;
303 }
304 case n_GF:
305 {
306// TS_ASSERT_EQUALS( r->cfInit, nfInit );
307// TS_ASSERT_EQUALS( r->cfAdd, nfAdd );
308 //TS_ASSERT_EQUALS( r->cfDelete, nfDelete );
309 break;
310 }
311#ifdef HAVE_RINGS
312 case n_Z2m:
313 {
314 //TS_ASSERT_EQUALS( r->cfInit, nr2mInit );
315 //TS_ASSERT_EQUALS( r->cfAdd, nr2mAdd );
316 //TS_ASSERT_EQUALS( r->cfDelete, ndDelete );
317 break;
318 }
319 case n_Zn:
320 {
321 //TS_ASSERT_EQUALS( r->cfInit, nrnInit );
322 //TS_ASSERT_EQUALS( r->cfAdd, nrnAdd );
323 //TS_ASSERT_EQUALS( r->cfDelete, nrnDelete );
324 break;
325 }
326#endif
327 default:
328 {
329 // ...
330 }
331 }
332
333 TestArith( r );
334 TestSum( r, 10 );
335 TestSum( r, 100 );
336 TestSum( r, 101 );
337 TestSum( r, 1001 );
338 TestSum( r, 9000 );
339
340 nKillChar( r );
341
342 return TRUE;
343}
344
345
346
347// We can rely on this file being included exactly once
348// and declare this global variable in the header file.
349//
351
352
354{
355 public:
356// void test_dummy() { float fnum = 2.00001f; TS_ASSERT_DELTA (fnum, 2.0f, 0.0001f); }
357
359 {
360#ifdef HAVE_RINGS
361 n_coeffType type = n_Z2m;
362 TS_ASSERT( Test(type, (void*) 4) );
363#endif
364 }
365
367 {
368 n_coeffType type = n_Zp;
369 TS_ASSERT( Test(type, (void*) 101) );
370 }
371
373 {
374#ifdef HAVE_RINGS
375 n_coeffType type = n_Z2m;
376 TS_ASSERT( Test(type, (void*) 8) );
377#endif
378 }
379
380 void simple(const n_coeffType _type)
381 {
382 n_coeffType type = _type;
383 TS_ASSERT( type == _type ); // ?
384 TS_ASSERT( Test(type) );
385 }
386
387 void test_Q()
388 {
389 simple(n_Q);
390 }
391
392 void test_R()
393 {
394 simple(n_R);
395 }
396
397
398 void test_Z()
399 {
400#ifdef HAVE_RINGS
401 simple(n_Z); // No need in GMP?
402#endif
403 }
404
405
407 {
408 n_coeffType type = n_GF;
409
410 GFInfo param;
411
412 param.GFChar= 5;
413 param.GFDegree= 12;
414 param.GFPar_name= (const char*)"q";
415
416 TS_ASSERT( !Test(type, (void*) &param) );
417
418 // it should not be used by numbers... right?
419 // TODO: what is our policy wrt param-pointer-ownership?
420 }
421
422 void test_GF()
423 {
424 // TODO: what if it was already registered?
425 // Q: no way to deRegister a type?
426 n_coeffType type = n_GF;
427
428 GFInfo param;
429
430 param.GFChar= 5;
431 param.GFDegree= 2;
432 param.GFPar_name= (const char*)"Q";
433
434 TS_ASSERT( Test(type, (void*) &param) );
435
436 // it should not be used by numbers... right?
437 // TODO: what is our policy wrt param-pointer-ownership?
438 }
439
440
441 void test_Zn3()
442 {
443#ifdef HAVE_RINGS
444 n_coeffType type = n_Zn;
445
446 ZnmInfo Znmparam;
447 Znmparam.base= (mpz_ptr) omAlloc (sizeof (mpz_t));
448 mpz_init_set_ui (Znmparam.base, 3);
449 Znmparam.exp= 1;
450
451 TS_ASSERT( Test(type, (void*) &Znmparam) );
452#endif
453 }
454
456 {
457#ifdef HAVE_RINGS
458 n_coeffType type = n_Z2m;
459
460 TS_ASSERT( Test(type, (void*) 2) );
461#endif
462 }
463
464 void test_LR()
465 {
467 }
468
469 void test_LC()
470 {
472 }
473
475 {
476 const coeffs cf = nInitChar(n_Q, NULLp);
477
478 if (cf == NULLp)
479 clog << ( "Test: could not get this coeff. domain" );
480
481 TS_ASSERT_DIFFERS(cf->cfCoeffWrite, NULLp);
482
483 if (cf->cfCoeffWrite != NULL )
484 {
485 clog << "Coeff-domain: " << endl;
487 }
488
489 number q1 = n_Init(21, cf);
490 number q2 = n_Init(2, cf);
491 number q3 = n_Div(q1, q2, cf);
492 number q4 = n_Init(30, cf);
493 number q5 = n_Mult(q3, q4, cf);
494 TS_ASSERT(n_Test(q5, cf));
495 Print("21/2 * 30 = %ld\n", n_Int(q5, cf));
496 TS_ASSERT(n_Test(q5, cf));
497 n_Delete(&q1, cf);
498 n_Delete(&q2, cf);
499 n_Delete(&q3, cf);
500 n_Delete(&q4, cf);
501 n_Delete(&q5, cf);
502 }
503};
504
#define TS_ASSERT_EQUALS(x, y)
Definition: TestSuite.h:255
#define TS_ASSERT_DIFFERS(x, y)
Definition: TestSuite.h:287
#define TS_ASSERT(e)
Definition: TestSuite.h:239
All the auxiliary stuff.
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
#define NULLp
Definition: auxiliary.h:108
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:56
int i
Definition: cfEzgcd.cc:132
int k
Definition: cfEzgcd.cc:99
int p
Definition: cfModGcd.cc:4078
CanonicalForm cf
Definition: cfModGcd.cc:4083
void test_GF_toobig()
Definition: coeffs_test.h:406
void simple(const n_coeffType _type)
Definition: coeffs_test.h:380
void test_Q_special()
Definition: coeffs_test.h:474
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of 'a' and 'b', i.e., a*b
Definition: coeffs.h:633
static FORCE_INLINE number n_Param(const int iParameter, const coeffs r)
return the (iParameter^th) parameter as a NEW number NOTE: parameter numbering: 1....
Definition: coeffs.h:780
static FORCE_INLINE long n_Int(number &n, const coeffs r)
conversion of n to an int; 0 if not possible in Z/pZ: the representing int lying in (-p/2 ....
Definition: coeffs.h:544
static FORCE_INLINE number n_Add(number a, number b, const coeffs r)
return the sum of 'a' and 'b', i.e., a+b
Definition: coeffs.h:647
static FORCE_INLINE BOOLEAN nCoeff_has_Units(const coeffs r)
returns TRUE, if r is not a field and r has non-trivial units
Definition: coeffs.h:794
static FORCE_INLINE void n_CoeffWrite(const coeffs r, BOOLEAN details=TRUE)
output the coeff description
Definition: coeffs.h:716
static FORCE_INLINE BOOLEAN nCoeff_is_GF(const coeffs r)
Definition: coeffs.h:836
static FORCE_INLINE BOOLEAN nCoeff_is_Z(const coeffs r)
Definition: coeffs.h:813
static FORCE_INLINE BOOLEAN nCoeff_is_Extension(const coeffs r)
Definition: coeffs.h:843
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition: coeffs.h:888
int GFDegree
Definition: coeffs.h:95
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:709
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_PtoM(const coeffs r)
Definition: coeffs.h:724
n_coeffType
Definition: coeffs.h:27
@ n_R
single prescision (6,6) real numbers
Definition: coeffs.h:31
@ n_GF
\GF{p^n < 2^16}
Definition: coeffs.h:32
@ n_Q
rational (GMP) numbers
Definition: coeffs.h:30
@ n_Zn
only used if HAVE_RINGS is defined
Definition: coeffs.h:44
@ n_long_R
real floating point (GMP) numbers
Definition: coeffs.h:33
@ n_Z2m
only used if HAVE_RINGS is defined
Definition: coeffs.h:46
@ n_Zp
\F{p < 2^31}
Definition: coeffs.h:29
@ n_Z
only used if HAVE_RINGS is defined
Definition: coeffs.h:43
@ n_long_C
complex floating point (GMP) numbers
Definition: coeffs.h:41
static FORCE_INLINE BOOLEAN nCoeff_is_CF(const coeffs r)
Definition: coeffs.h:894
static FORCE_INLINE BOOLEAN nCoeff_is_numeric(const coeffs r)
Definition: coeffs.h:829
static FORCE_INLINE BOOLEAN nCoeff_is_Domain(const coeffs r)
returns TRUE, if r is a field or r has no zero divisors (i.e is a domain)
Definition: coeffs.h:736
static FORCE_INLINE number n_InpNeg(number n, const coeffs r)
in-place negation of n MUST BE USED: n = n_InpNeg(n) (no copy is returned)
Definition: coeffs.h:554
static FORCE_INLINE BOOLEAN nCoeff_has_simple_inverse(const coeffs r)
TRUE, if the computation of the inverse is fast, i.e. prefer leading coeff. 1 over content.
Definition: coeffs.h:899
static FORCE_INLINE number n_Div(number a, number b, const coeffs r)
return the quotient of 'a' and 'b', i.e., a/b; raises an error if 'b' is not invertible in r exceptio...
Definition: coeffs.h:612
static FORCE_INLINE BOOLEAN nCoeff_is_Q(const coeffs r)
Definition: coeffs.h:803
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL
Definition: numbers.cc:413
static FORCE_INLINE void nSetChar(const coeffs r)
initialisations after each ring change
Definition: coeffs.h:437
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:461
static FORCE_INLINE BOOLEAN nCoeff_has_simple_Alloc(const coeffs r)
TRUE if n_Delete is empty operation.
Definition: coeffs.h:903
static FORCE_INLINE number n_Sub(number a, number b, const coeffs r)
return the difference of 'a' and 'b', i.e., a-b
Definition: coeffs.h:652
static FORCE_INLINE BOOLEAN nCoeff_is_Ring(const coeffs r)
Definition: coeffs.h:727
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition: coeffs.h:422
static FORCE_INLINE int n_GetChar(const coeffs r)
Return the characteristic of the coeff. domain.
Definition: coeffs.h:441
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:452
static FORCE_INLINE BOOLEAN nCoeff_is_Zn(const coeffs r)
Definition: coeffs.h:823
static FORCE_INLINE int n_NumberOfParameters(const coeffs r)
Returns the number of parameters.
Definition: coeffs.h:771
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
Definition: coeffs.h:797
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:535
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
Definition: coeffs.h:721
static FORCE_INLINE BOOLEAN n_DivBy(number a, number b, const coeffs r)
test whether 'a' is divisible 'b'; for r encoding a field: TRUE iff 'b' does not represent zero in Z:...
Definition: coeffs.h:750
static FORCE_INLINE void n_InpMult(number &a, number b, const coeffs r)
multiplication of 'a' and 'b'; replacement of 'a' by the product a*b
Definition: coeffs.h:638
static FORCE_INLINE BOOLEAN n_Equal(number a, number b, const coeffs r)
TRUE iff 'a' and 'b' represent the same number; they may have different representations.
Definition: coeffs.h:457
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition: coeffs.h:833
const char * GFPar_name
Definition: coeffs.h:96
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:891
int GFChar
Definition: coeffs.h:94
void nKillChar(coeffs r)
undo all initialisations
Definition: numbers.cc:568
static FORCE_INLINE void n_InpAdd(number &a, number b, const coeffs r)
addition of 'a' and 'b'; replacement of 'a' by the sum a+b
Definition: coeffs.h:643
Creation data needed for finite fields.
Definition: coeffs.h:93
void TestArith(const coeffs r)
Definition: coeffs_test.h:109
void TestSum(const coeffs r, const unsigned long N)
Definition: coeffs_test.h:25
static GlobalPrintingFixture globalPrintingFixture
Definition: coeffs_test.h:350
BOOLEAN Test(const n_coeffType type, void *p=NULLp)
Definition: coeffs_test.h:209
#define Print
Definition: emacs.cc:80
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
#define assume(x)
Definition: mod2.h:389
The main handler for Singular numbers which are suitable for Singular polynomials.
BOOLEAN n_IsZeroDivisor(number a, const coeffs r)
Test whether a is a zero divisor in r i.e. not coprime with char. of r very inefficient implementatio...
Definition: numbers.cc:173
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define NULL
Definition: omList.c:12
void PrintS(const char *s)
Definition: reporter.cc:284
void PrintLn()
Definition: reporter.cc:310
mpz_ptr base
Definition: rmodulon.h:18
unsigned long exp
Definition: rmodulon.h:18