1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT: interface to coefficient aritmetics |
---|
8 | */ |
---|
9 | |
---|
10 | #include <string.h> |
---|
11 | #include <stdlib.h> |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | #include <misc/auxiliary.h> |
---|
15 | |
---|
16 | #ifdef HAVE_FACTORY |
---|
17 | #include <factory/factory.h> |
---|
18 | #endif |
---|
19 | |
---|
20 | |
---|
21 | #include "coeffs.h" |
---|
22 | #include <coeffs/numbers.h> |
---|
23 | |
---|
24 | #include <reporter/reporter.h> |
---|
25 | #include <omalloc/omalloc.h> |
---|
26 | #include <coeffs/numbers.h> |
---|
27 | #include <coeffs/longrat.h> |
---|
28 | #include <coeffs/modulop.h> |
---|
29 | #include <coeffs/gnumpfl.h> |
---|
30 | #include <coeffs/gnumpc.h> |
---|
31 | #include <coeffs/ffields.h> |
---|
32 | #include <coeffs/shortfl.h> |
---|
33 | |
---|
34 | #ifdef HAVE_RINGS |
---|
35 | #include <coeffs/rmodulo2m.h> |
---|
36 | #include <coeffs/rmodulon.h> |
---|
37 | #include <coeffs/rintegers.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #ifdef HAVE_POLYEXTENSIONS |
---|
41 | #include <polys/ext_fields/algext.h> |
---|
42 | #include <polys/ext_fields/transext.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | //static int characteristic = 0; |
---|
48 | extern int IsPrime(int p); |
---|
49 | |
---|
50 | /*0 implementation*/ |
---|
51 | number nNULL; /* the 0 as constant */ |
---|
52 | |
---|
53 | n_Procs_s *cf_root=NULL; |
---|
54 | |
---|
55 | void nNew(number* d) { *d=NULL; } |
---|
56 | void ndDelete(number* d, const coeffs) { *d=NULL; } |
---|
57 | void ndInpMult(number &a, number b, const coeffs r) |
---|
58 | { |
---|
59 | number n=n_Mult(a,b,r); |
---|
60 | n_Delete(&a,r); |
---|
61 | a=n; |
---|
62 | } |
---|
63 | void ndInpAdd(number &a, number b, const coeffs r) |
---|
64 | { |
---|
65 | number n=n_Add(a,b,r); |
---|
66 | n_Delete(&a,r); |
---|
67 | a=n; |
---|
68 | } |
---|
69 | |
---|
70 | #ifdef LDEBUG |
---|
71 | void nDBDummy1(number* d,char *, int) { *d=NULL; } |
---|
72 | BOOLEAN ndDBTest(number, const char *, const int, const coeffs) |
---|
73 | { |
---|
74 | return TRUE; |
---|
75 | } |
---|
76 | #endif |
---|
77 | |
---|
78 | |
---|
79 | BOOLEAN n_IsZeroDivisor( number a, const coeffs r) |
---|
80 | { |
---|
81 | int c = n_GetChar(r); |
---|
82 | BOOLEAN ret = n_IsZero(a, r); |
---|
83 | if( (c != 0) && !ret ) |
---|
84 | { |
---|
85 | number ch = n_Init( c, r ); |
---|
86 | number g = n_Gcd( ch, a, r ); |
---|
87 | ret = !n_IsOne (g, r); |
---|
88 | n_Delete(&ch, r); |
---|
89 | n_Delete(&g, r); |
---|
90 | } |
---|
91 | return ret; |
---|
92 | } |
---|
93 | |
---|
94 | void ndNormalize(number&, const coeffs) { } |
---|
95 | |
---|
96 | char * ndName(number, const coeffs) { return NULL; } |
---|
97 | |
---|
98 | number ndReturn0(number, const coeffs r) { return n_Init(0,r); } |
---|
99 | |
---|
100 | number ndGcd(number, number, const coeffs r) { return n_Init(1,r); } |
---|
101 | |
---|
102 | number ndIntMod(number, number, const coeffs r) { return n_Init(0,r); } |
---|
103 | |
---|
104 | number ndGetDenom(number &, const coeffs r) { return n_Init(1,r); } |
---|
105 | number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); } |
---|
106 | |
---|
107 | int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; } |
---|
108 | |
---|
109 | number ndCopy(number a, const coeffs) { return a; } |
---|
110 | number ndCopyMap(number a, const coeffs aRing, const coeffs r) |
---|
111 | { |
---|
112 | assume( getCoeffType(r) == getCoeffType(aRing) ); |
---|
113 | if ( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) ) |
---|
114 | return a; |
---|
115 | else |
---|
116 | return n_Copy(a, r); |
---|
117 | } |
---|
118 | void ndKillChar(coeffs) {} |
---|
119 | void ndSetChar(const coeffs) {} |
---|
120 | |
---|
121 | number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); } |
---|
122 | |
---|
123 | number ndChineseRemainder(number *,number *,int,const coeffs r){ return n_Init(0,r); } |
---|
124 | #ifdef HAVE_RINGS |
---|
125 | BOOLEAN ndDivBy(number, number, const coeffs) { return TRUE; } // assume a,b !=0 |
---|
126 | int ndDivComp(number, number, const coeffs) { return 2; } |
---|
127 | BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); } |
---|
128 | number ndExtGcd (number, number, number *, number *, const coeffs r) { return n_Init(1,r); } |
---|
129 | #endif |
---|
130 | |
---|
131 | #ifdef HAVE_FACTORY |
---|
132 | CanonicalForm ndConvSingNFactoryN( number, BOOLEAN /*setChar*/, const coeffs) |
---|
133 | { |
---|
134 | CanonicalForm term(0); |
---|
135 | Werror("no conversion to factory"); |
---|
136 | return term; |
---|
137 | } |
---|
138 | |
---|
139 | number ndConvFactoryNSingN( const CanonicalForm, const coeffs) |
---|
140 | { |
---|
141 | Werror("no conversion from factory"); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | #endif |
---|
145 | |
---|
146 | number ndInit_bigint(number, const coeffs, const coeffs) |
---|
147 | { |
---|
148 | Werror("no conversion from bigint to this field"); |
---|
149 | return NULL; |
---|
150 | } |
---|
151 | |
---|
152 | /**< [in, out] a bigint number >= 0 */ |
---|
153 | /**< [out] the GMP equivalent */ |
---|
154 | /// Converts a non-negative bigint number into a GMP number. |
---|
155 | void ndMPZ(mpz_t result, number &n, const coeffs r) |
---|
156 | { |
---|
157 | mpz_init_set_si( result, n_Int(n, r) ); |
---|
158 | } |
---|
159 | |
---|
160 | number ndInitMPZ(mpz_t m, const coeffs r) |
---|
161 | { |
---|
162 | return n_Init( mpz_get_si(m), r); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | BOOLEAN ndCoeffIsEqual(const coeffs r, n_coeffType n, void *) |
---|
167 | { |
---|
168 | /* test, if r is an instance of nInitCoeffs(n,parameter) */ |
---|
169 | /* if paramater is not needed */ |
---|
170 | return (n==r->type); |
---|
171 | } |
---|
172 | |
---|
173 | static n_coeffType nLastCoeffs=n_CF; |
---|
174 | cfInitCharProc nInitCharTableDefault[]= |
---|
175 | { NULL, /*n_unknown */ |
---|
176 | npInitChar, /* n_Zp */ |
---|
177 | nlInitChar, /* n_Q */ |
---|
178 | nrInitChar, /* n_R */ |
---|
179 | nfInitChar, /* n_GF */ |
---|
180 | ngfInitChar, /* n_long_R */ |
---|
181 | #ifdef HAVE_POLYEXTENSIONS |
---|
182 | naInitChar, /* n_algExt */ |
---|
183 | ntInitChar, /* n_transExt */ |
---|
184 | #else |
---|
185 | NULL, /* n_algExt */ |
---|
186 | NULL, /* n_transExt */ |
---|
187 | #endif |
---|
188 | ngcInitChar, /* n_long_C */ |
---|
189 | #ifdef HAVE_RINGS |
---|
190 | nrzInitChar, /* n_Z */ |
---|
191 | nrnInitChar, /* n_Zn */ |
---|
192 | NULL, /* n_Zpn */ |
---|
193 | nr2mInitChar, /* n_Z2m */ |
---|
194 | #else |
---|
195 | NULL, /* n_Z */ |
---|
196 | NULL, /* n_Zn */ |
---|
197 | NULL, /* n_Zpn */ |
---|
198 | NULL, /* n_Z2m */ |
---|
199 | #endif |
---|
200 | NULL /* n_CF */ |
---|
201 | }; |
---|
202 | |
---|
203 | static cfInitCharProc *nInitCharTable=nInitCharTableDefault; |
---|
204 | /*2 |
---|
205 | * init operations for coeffs r |
---|
206 | */ |
---|
207 | coeffs nInitChar(n_coeffType t, void * parameter) |
---|
208 | { |
---|
209 | n_Procs_s *n=cf_root; |
---|
210 | |
---|
211 | while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter))) |
---|
212 | n=n->next; |
---|
213 | |
---|
214 | if (n==NULL) |
---|
215 | { |
---|
216 | n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s)); |
---|
217 | n->next=cf_root; |
---|
218 | n->ref=1; |
---|
219 | n->type=t; |
---|
220 | |
---|
221 | // default entries (different from NULL) for some routines: |
---|
222 | n->cfSize = ndSize; |
---|
223 | n->cfGetDenom= ndGetDenom; |
---|
224 | n->cfGetNumerator= ndGetNumerator; |
---|
225 | n->cfName = ndName; |
---|
226 | n->cfImPart=ndReturn0; |
---|
227 | n->cfDelete= ndDelete; |
---|
228 | n->cfInpMult=ndInpMult; |
---|
229 | n->cfCopy = ndCopy; |
---|
230 | n->cfIntMod=ndIntMod; /* dummy !! */ |
---|
231 | n->cfNormalize=ndNormalize; |
---|
232 | n->cfGcd = ndGcd; |
---|
233 | n->cfLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
234 | n->cfInit_bigint = ndInit_bigint; |
---|
235 | n->cfInitMPZ = ndInitMPZ; |
---|
236 | n->cfMPZ = ndMPZ; |
---|
237 | |
---|
238 | //n->cfKillChar = ndKillChar; /* dummy */ |
---|
239 | n->cfSetChar = ndSetChar; /* dummy */ |
---|
240 | // temp. removed to catch all the coeffs which miss to implement this! |
---|
241 | |
---|
242 | n->cfChineseRemainder = ndChineseRemainder; |
---|
243 | n->cfFarey = ndGcd; |
---|
244 | |
---|
245 | #ifdef HAVE_RINGS |
---|
246 | n->cfDivComp = ndDivComp; |
---|
247 | n->cfDivBy = ndDivBy; |
---|
248 | n->cfIsUnit = ndIsUnit; |
---|
249 | n->cfExtGcd = ndExtGcd; |
---|
250 | //n->cfGetUnit = (nMapFunc)NULL; |
---|
251 | #endif |
---|
252 | |
---|
253 | #ifdef fACTORY |
---|
254 | n->convSingNFactoryN=ndConvSingNFactoryN; |
---|
255 | n->convFactoryNSingN=ndConvFactoryNSingN; |
---|
256 | #endif |
---|
257 | |
---|
258 | BOOLEAN nOK=TRUE; |
---|
259 | // init |
---|
260 | if ((t<=nLastCoeffs) && (nInitCharTable[t]!=NULL)) |
---|
261 | nOK = (nInitCharTable[t])(n,parameter); |
---|
262 | else |
---|
263 | Werror("Sorry: the coeff type [%d] was not registered: it is missing in nInitCharTable", (int)t); |
---|
264 | if (nOK) |
---|
265 | { |
---|
266 | omFreeSize(n,sizeof(*n)); |
---|
267 | return NULL; |
---|
268 | } |
---|
269 | cf_root=n; |
---|
270 | // post init settings: |
---|
271 | if (n->cfRePart==NULL) n->cfRePart=n->cfCopy; |
---|
272 | if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv; |
---|
273 | |
---|
274 | #ifdef HAVE_RINGS |
---|
275 | if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy; |
---|
276 | #endif |
---|
277 | |
---|
278 | #ifndef NDEBUG |
---|
279 | assume(n->nCoeffIsEqual!=NULL); |
---|
280 | if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t); |
---|
281 | assume(n->cfSetChar!=NULL); |
---|
282 | assume(n->cfMult!=NULL); |
---|
283 | assume(n->cfSub!=NULL); |
---|
284 | assume(n->cfAdd!=NULL); |
---|
285 | assume(n->cfDiv!=NULL); |
---|
286 | assume(n->cfIntDiv!=NULL); |
---|
287 | assume(n->cfIntMod!=NULL); |
---|
288 | assume(n->cfExactDiv!=NULL); |
---|
289 | assume(n->cfInit!=NULL); |
---|
290 | assume(n->cfInitMPZ!=NULL); |
---|
291 | assume(n->cfSize!=NULL); |
---|
292 | assume(n->cfInt!=NULL); |
---|
293 | assume(n->cfMPZ!=NULL); |
---|
294 | //assume(n->n->cfDivComp!=NULL); |
---|
295 | //assume(n->cfIsUnit!=NULL); |
---|
296 | //assume(n->cfGetUnit!=NULL); |
---|
297 | //assume(n->cfExtGcd!=NULL); |
---|
298 | assume(n->cfNeg!=NULL); |
---|
299 | assume(n->cfCopy!=NULL); |
---|
300 | assume(n->cfRePart!=NULL); |
---|
301 | assume(n->cfImPart!=NULL); |
---|
302 | assume(n->cfWrite!=NULL); |
---|
303 | assume(n->cfRead!=NULL); |
---|
304 | assume(n->cfNormalize!=NULL); |
---|
305 | assume(n->cfGreater!=NULL); |
---|
306 | //assume(n->cfDivBy!=NULL); |
---|
307 | assume(n->cfEqual!=NULL); |
---|
308 | assume(n->cfIsZero!=NULL); |
---|
309 | assume(n->cfIsOne!=NULL); |
---|
310 | assume(n->cfIsMOne!=NULL); |
---|
311 | assume(n->cfGreaterZero!=NULL); |
---|
312 | assume(n->cfPower!=NULL); |
---|
313 | assume(n->cfGetDenom!=NULL); |
---|
314 | assume(n->cfGetNumerator!=NULL); |
---|
315 | assume(n->cfGcd!=NULL); |
---|
316 | assume(n->cfLcm!=NULL); |
---|
317 | assume(n->cfDelete!=NULL); |
---|
318 | assume(n->cfSetMap!=NULL); |
---|
319 | assume(n->cfName!=NULL); |
---|
320 | assume(n->cfInpMult!=NULL); |
---|
321 | // assume(n->cfInit_bigint!=NULL); |
---|
322 | assume(n->cfCoeffWrite != NULL); |
---|
323 | #ifdef LDEBUG |
---|
324 | assume(n->cfDBTest!=NULL); |
---|
325 | #endif |
---|
326 | assume(n->type==t); |
---|
327 | #endif |
---|
328 | } |
---|
329 | else |
---|
330 | { |
---|
331 | n->ref++; |
---|
332 | } |
---|
333 | return n; |
---|
334 | } |
---|
335 | |
---|
336 | void nKillChar(coeffs r) |
---|
337 | { |
---|
338 | if (r!=NULL) |
---|
339 | { |
---|
340 | r->ref--; |
---|
341 | if (r->ref<=0) |
---|
342 | { |
---|
343 | n_Procs_s tmp; |
---|
344 | n_Procs_s* n=&tmp; |
---|
345 | tmp.next=cf_root; |
---|
346 | while((n->next!=NULL) && (n->next!=r)) n=n->next; |
---|
347 | if (n->next==r) |
---|
348 | { |
---|
349 | n->next=n->next->next; |
---|
350 | if (cf_root==r) cf_root=n->next; |
---|
351 | r->cfDelete(&(r->nNULL),r); |
---|
352 | if (r->cfKillChar!=NULL) r->cfKillChar(r); |
---|
353 | omFreeSize((void *)r, sizeof(n_Procs_s)); |
---|
354 | r=NULL; |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | WarnS("cf_root list destroyed"); |
---|
359 | } |
---|
360 | r->cf=NULL; |
---|
361 | } |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | n_coeffType nRegister(n_coeffType n, cfInitCharProc p) |
---|
367 | { |
---|
368 | if (n==n_unknown) |
---|
369 | { |
---|
370 | nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1); |
---|
371 | if (nInitCharTable==nInitCharTableDefault) |
---|
372 | { |
---|
373 | nInitCharTable=(cfInitCharProc*)omAlloc0( |
---|
374 | nLastCoeffs*sizeof(cfInitCharProc)); |
---|
375 | memcpy(nInitCharTable,nInitCharTableDefault, |
---|
376 | (nLastCoeffs-1)*sizeof(cfInitCharProc)); |
---|
377 | } |
---|
378 | else |
---|
379 | { |
---|
380 | nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable, |
---|
381 | (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc), |
---|
382 | ((int)nLastCoeffs)*sizeof(cfInitCharProc)); |
---|
383 | } |
---|
384 | |
---|
385 | nInitCharTable[nLastCoeffs]=p; |
---|
386 | return nLastCoeffs; |
---|
387 | } |
---|
388 | else |
---|
389 | { |
---|
390 | if (nInitCharTable[n]!=NULL) Print("coeff %d already initialized\n",n); |
---|
391 | nInitCharTable[n]=p; |
---|
392 | return n; |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|