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