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