source: git/libpolys/tests/polys_test.h @ b7d7eb

spielwiese
Last change on this file since b7d7eb was b7d7eb, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
ADD: deleting polynomials... fails
  • Property mode set to 100644
File size: 6.5 KB
Line 
1#include "config.h"
2
3#include "common.h"
4using namespace std;
5
6// the following headers are private...
7#include <coeffs/longrat.h>
8#include <coeffs/gnumpfl.h>
9#include <coeffs/gnumpc.h>
10#include <coeffs/shortfl.h>
11#include <coeffs/ffields.h>
12#include <coeffs/modulop.h>
13#include <coeffs/rmodulon.h>
14#include <coeffs/rmodulo2m.h>
15#include <coeffs/rintegers.h>
16
17
18#include <polys/monomials/ring.h>
19#include <polys/monomials/p_polys.h>
20
21
22
23
24class MyGlobalPrintingFixture : public GlobalPrintingFixture
25{
26  public:
27    virtual bool setUpWorld()
28    {
29   
30      GlobalPrintingFixture::setUpWorld();
31     
32
33      TS_ASSERT_EQUALS( nRegister( n_Zp, npInitChar), n_Zp );
34      TS_ASSERT_EQUALS( nRegister( n_GF, nfInitChar), n_GF );
35      TS_ASSERT_EQUALS( nRegister( n_R, nrInitChar), n_R );
36      TS_ASSERT_EQUALS( nRegister( n_Q, nlInitChar), n_Q );
37      TS_ASSERT_EQUALS( nRegister( n_R, nrInitChar), n_R );
38     
39#ifdef HAVE_RINGS
40      TS_ASSERT_EQUALS( nRegister( n_Z, nrzInitChar), n_Z ); // these are UNusable at the moment!
41#endif
42     
43      return true;
44    }
45};
46
47
48//
49// We can rely on this file being included exactly once
50// and declare this global variable in the header file.
51//
52static MyGlobalPrintingFixture globalPrintingFixture;
53
54
55namespace
56{
57  void PrintRing(const ring r)
58  {
59    rWrite(r); PrintLn();
60  #ifdef  RDEBUG
61    rDebugPrint(r); PrintLn();
62  #endif
63  }
64
65  static inline std::string _2S(poly a, const ring r)
66  {
67    p_Test(a,r);
68   
69    StringSetS("");
70    p_Write(a, r);
71
72    const char* s = StringAppendS("");
73
74    std::stringstream ss;  ss << s;
75
76    StringSetS(""); 
77    return ss.str();
78  }
79
80  static inline void PrintSized(/*const*/ poly a, const ring r, BOOLEAN eoln = TRUE)
81  {
82    std::clog << _2S(a, r) << ", of size: " << p_Size(a, r);
83
84    if( eoln ) 
85      std::clog << std::endl; 
86  }
87
88template <typename T>
89static inline poly p_Init(T v, const ring r)
90{ assume(r != NULL); return p_ISet(v, r); }
91
92static inline void Delete(poly &p, const ring r)
93{
94  if( p != NULL )
95    p_Delete(&p, r);
96
97  p = NULL;
98}
99
100void TestSum(const ring r, const unsigned long N)
101{
102  clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: ");
103  clog << endl;
104
105  assume( N > 0 ); // just for now...
106
107  const unsigned long ssss = (N * (N+1)) / 2;
108 
109  poly sum1 = p_Init(ssss, r);
110  clog<< "poly(N*(N+1)/2) (int: " << ssss << "): "; PrintSized(sum1, r);
111
112  poly s, ss, i, res;
113
114  s = p_Init(, r);
115  i = p_Init(N+1, r);
116
117  i = p_Mult_q(s, i, r); s = NULL;
118
119  clog<< "poly(N)*poly(N+1): (int: "<< N*(N+1) << "): "; PrintSized(i, r); 
120
121  number t = n_Init(2, r->cf);
122  clog<< "number(2): "; PrintSized(i, r); 
123
124  if( !n_IsZero( t, r->cf) )
125  {
126    TS_ASSERT( n_DivBy(p_GetCoeff(i, r), t, r->cf) );
127    res = p_Div_nn(i, t, r); i = NULL;
128 
129    clog<< "(poly(N)*poly(N+1))/number(2): "; PrintSized(res, r);
130    poly d = p_Sub(res, sum1, r);
131   
132    if( d != NULL )
133      TS_ASSERT( n_IsZeroDivisor(p_GetCoeff(d, r), r->cf) );
134   
135    Delete(d, r);
136   
137    if( n_GetChar(r->cf) == 0 )
138    {
139      TS_ASSERT( p_EqualPolys(sum1, res, r) );
140      TS_ASSERT( p_EqualPolys(res, sum1, r) );
141    }
142  } else
143    TS_ASSERT_EQUALS( n_GetChar(r->cf), 2);
144 
145  n_Delete(&t, r->cf);
146
147
148  s = p_Init(0  , r);
149  ss = p_Init(0 , r);
150  for( int k = N; k >= 0; k-- )
151  {
152    i = p_Init(k, r);
153    s = p_Add_q(s, i, r); // s += i
154
155    i = p_Neg( p_Init(k, r), r );
156    ss = p_Add_q(ss, i, r); // ss -= i
157  }
158 
159  clog<< "ss(-sum): "; PrintSized(ss, r); 
160
161  ss = p_Neg(ss, r); // ss = -ss
162 
163  clog<< "real sum    : "; PrintSized(s, r);
164  clog<< "real sum(--): "; PrintSized(ss, r); 
165
166  TS_ASSERT( p_EqualPolys(s, ss, r) );
167  TS_ASSERT( p_EqualPolys(ss, s, r) );
168
169  TODO(somebody, fix the delete method!);
170
171  Delete(sum1, r); 
172  Delete(res, r);
173
174  Delete(s, r);   
175  Delete(ss, r);   
176
177  clog << ( " >>> TEST DONE!" );
178  clog << endl;
179
180}
181   
182void Test(const ring r)
183{
184  TestSum( r, 10 );
185  TestSum( r, 100 );
186  TestSum( r, 101 );
187  TestSum( r, 1001 );
188  TestSum( r, 9000 );
189}
190
191}
192
193class PolysTestSuite : public CxxTest::TestSuite
194{
195public:
196  void test_Z13_t()
197  {
198    clog << "Creating  Z/13[t]: " << endl;
199
200    char* n[] = {"t"};
201    ring r = rDefault( 13, 1, n);     
202    TS_ASSERT_DIFFERS( r, NULLp );
203
204    PrintRing(r);
205
206    TS_ASSERT( rField_is_Domain(r) );
207    TS_ASSERT( !rField_is_Q(r) );
208
209    TS_ASSERT( rField_is_Zp(r) );
210    TS_ASSERT( !rField_is_Zp(r, 11) );
211    TS_ASSERT( rField_is_Zp(r, 13) );
212
213    TS_ASSERT_EQUALS( rVar(r), 1);
214
215    Test(r);
216     
217    rDelete(r);
218  }
219
220  void test_QQ_t()
221  {
222    clog << "Creating  Q[s]: " << endl;
223
224    char* n[] = {"s"};
225    ring r = rDefault( 0, 1, n);     
226    TS_ASSERT_DIFFERS( r, NULLp );
227
228    PrintRing(r);
229
230    TS_ASSERT( rField_is_Domain(r) );
231    TS_ASSERT( rField_is_Q(r) );
232   
233    TS_ASSERT( !rField_is_Zp(r) );
234    TS_ASSERT( !rField_is_Zp(r, 11) );
235
236    TS_ASSERT_EQUALS( rVar(r), 1);
237
238    Test(r);
239
240    rDelete(r);
241  }
242 
243  void test_Z11_x_y_z()
244  {
245     clog << "Creating  Z/11[x, y, z]: " << endl;
246     
247     char* n[] = {"x", "y", "z"};
248     ring r = rDefault( 11, 3, n);     
249     TS_ASSERT_DIFFERS( r, NULLp );
250
251     PrintRing(r);
252     
253     TS_ASSERT( rField_is_Domain(r) );
254     TS_ASSERT( !rField_is_Q(r) );
255
256     TS_ASSERT( rField_is_Zp(r) );
257     TS_ASSERT( rField_is_Zp(r, 11) );
258     TS_ASSERT( !rField_is_Zp(r, 13) );
259
260     TS_ASSERT_EQUALS( rVar(r), 3);
261
262     Test(r);
263     
264     rDelete(r);
265  }
266   void test_QQ_x_y_z()
267   {
268     clog << "Creating  QQ[x, y, z, u]: " << endl;
269
270     char* n[] = {"x", "y", "z", "u"};
271     ring r = rDefault( 0, 4, n);     
272     TS_ASSERT_DIFFERS( r, NULLp );
273
274     PrintRing(r);
275
276     TS_ASSERT( rField_is_Domain(r) );
277     TS_ASSERT( rField_is_Q(r) );
278
279     TS_ASSERT( !rField_is_Zp(r) );
280     TS_ASSERT( !rField_is_Zp(r, 11) );
281
282     TS_ASSERT_EQUALS( rVar(r), 4);
283
284     Test(r);
285     
286     rDelete(r);
287   }
288
289
290   void test_Z13_t_GF()
291   {
292     clog << "Creating  GF[t]: " << endl;
293
294     char* n[] = {"t"};
295
296     GFInfo param;
297
298     param.GFChar= 5;
299     param.GFDegree= 2;
300     param.GFPar_name= (const char*)"Q";
301
302     const coeffs cf = nInitChar( n_GF, &param );
303
304     TS_ASSERT_DIFFERS( cf, NULLp );
305
306     ring r = rDefault( cf, 1, n);  // now cf belongs to r!
307     TS_ASSERT_DIFFERS( r, NULLp );
308
309     PrintRing(r);
310
311     TS_ASSERT( rField_is_Domain(r) );
312     TS_ASSERT( !rField_is_Q(r) );
313
314     TS_ASSERT( !rField_is_Zp(r) );
315     TS_ASSERT( !rField_is_Zp(r, 11) );
316     TS_ASSERT( !rField_is_Zp(r, 13) );
317     TS_ASSERT( rField_is_GF(r) );
318
319     TS_ASSERT( rField_is_GF(r, 5) );
320     TS_ASSERT( !rField_is_GF(r, 25) );
321
322     TS_ASSERT_EQUALS( rVar(r), 1);
323
324     Test(r);
325
326     rDelete(r); // kills 'cf' as well!
327   }
328
329};
330
Note: See TracBrowser for help on using the repository browser.