1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* $Id: numbers.cc,v 1.12 1999-05-29 14:31:30 obachman Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT: interface to coefficient aritmetics |
---|
8 | */ |
---|
9 | |
---|
10 | #include <string.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include "mod2.h" |
---|
13 | #include "tok.h" |
---|
14 | #include "febase.h" |
---|
15 | #include "kstd1.h" |
---|
16 | #include "numbers.h" |
---|
17 | #include "longrat.h" |
---|
18 | #include "longalg.h" |
---|
19 | #include "modulop.h" |
---|
20 | #include "gnumpfl.h" |
---|
21 | #include "gnumpc.h" |
---|
22 | #include "ring.h" |
---|
23 | #ifndef FAST_AND_DIRTY |
---|
24 | #undef npMultM |
---|
25 | #undef npSubM |
---|
26 | #undef npNegM |
---|
27 | #undef npEqualM |
---|
28 | #endif |
---|
29 | #include "ffields.h" |
---|
30 | #include "shortfl.h" |
---|
31 | |
---|
32 | //static int characteristic = 0; |
---|
33 | extern int IsPrime(int p); |
---|
34 | |
---|
35 | void (*nNew)(number *a); |
---|
36 | number (*nInit)(int i); |
---|
37 | number (*nPar)(int i); |
---|
38 | int (*nParDeg)(number n); |
---|
39 | int (*nSize)(number n); |
---|
40 | int (*nInt)(number &n); |
---|
41 | numberfunc nMult, nSub, nAdd, nDiv, nIntDiv, nIntMod, nExactDiv; |
---|
42 | number (*nNeg)(number a); |
---|
43 | number (*nInvers)(number a); |
---|
44 | void (*nNormalize)(number &a); |
---|
45 | number (*nCopy)(number a); |
---|
46 | BOOLEAN (*nGreater)(number a,number b); |
---|
47 | BOOLEAN (*nEqual)(number a,number b); |
---|
48 | BOOLEAN (*nIsZero)(number a); |
---|
49 | BOOLEAN (*nIsOne)(number a); |
---|
50 | BOOLEAN (*nIsMOne)(number a); |
---|
51 | BOOLEAN (*nGreaterZero)(number a); |
---|
52 | void (*nWrite)(number &a); |
---|
53 | char * (*nRead)(char *s,number *a); |
---|
54 | void (*nPower)(number a, int i, number * result); |
---|
55 | numberfunc nGcd,nLcm; |
---|
56 | BOOLEAN (*nSetMap)(int c,char **par, int nop, number minpol); |
---|
57 | number (*nMap)(number from); |
---|
58 | char * (*nName)(number n); |
---|
59 | #ifdef LDEBUG |
---|
60 | BOOLEAN (*nDBTest)(number a, char *f, int l); |
---|
61 | void (*nDBDelete)(number *a, char *f, int l); |
---|
62 | #else |
---|
63 | void (*nDelete)(number *a); |
---|
64 | #endif |
---|
65 | |
---|
66 | /*0 implementation*/ |
---|
67 | number nNULL; /* the 0 as constant */ |
---|
68 | int nChar; |
---|
69 | |
---|
70 | void nDummy1(number* d) { *d=NULL; } |
---|
71 | |
---|
72 | #ifdef LDEBUG |
---|
73 | void nDBDummy1(number* d,char *f, int l) { *d=NULL; } |
---|
74 | #endif |
---|
75 | |
---|
76 | void nDummy2(number& d) { } |
---|
77 | |
---|
78 | char * ndName(number n) { return NULL; } |
---|
79 | |
---|
80 | number ndPar(int i) { return nInit(0); } |
---|
81 | |
---|
82 | int ndParDeg(number n) { return 0; } |
---|
83 | |
---|
84 | number ndGcd(number a, number b) { return nInit(1); } |
---|
85 | |
---|
86 | number ndIntMod(number a, number b) { return nInit(0); } |
---|
87 | |
---|
88 | int nGetChar() { return nChar; } |
---|
89 | |
---|
90 | int ndSize(number a) {return (int)nIsZero(a)==FALSE; } |
---|
91 | |
---|
92 | /*2 |
---|
93 | * init operations for characteristic c (complete==TRUE) |
---|
94 | * init nDelete for characteristic c (complete==FALSE) |
---|
95 | */ |
---|
96 | void nSetChar(ring r, BOOLEAN complete) |
---|
97 | { |
---|
98 | int c=rInternalChar(r); |
---|
99 | |
---|
100 | if (nNULL!=NULL) |
---|
101 | { |
---|
102 | nDelete(&nNULL);nNULL=NULL; |
---|
103 | } |
---|
104 | if (complete) |
---|
105 | { |
---|
106 | nChar=c; |
---|
107 | nPar = ndPar; |
---|
108 | nParDeg= ndParDeg; |
---|
109 | nSize = ndSize; |
---|
110 | } |
---|
111 | //Print("n:c=%d compl=%d param=%d\n",c,complete,r->parameter); |
---|
112 | //if ((c == 1) || (c< (-1))) |
---|
113 | if (rField_is_Extension(r)) |
---|
114 | { |
---|
115 | naSetChar(c,complete,r->parameter,rPar(r)); |
---|
116 | #ifdef LDEBUG |
---|
117 | nDBDelete= naDBDelete; |
---|
118 | #else |
---|
119 | nDelete= naDelete; |
---|
120 | #endif |
---|
121 | if (complete) |
---|
122 | { |
---|
123 | test |= Sy_bit(OPT_INTSTRATEGY); /*intStrategy*/ |
---|
124 | test &= ~Sy_bit(OPT_REDTAIL); /*noredTail*/ |
---|
125 | nNew = naNew; |
---|
126 | nNormalize=naNormalize; |
---|
127 | nInit = naInit; |
---|
128 | nPar = naPar; |
---|
129 | nParDeg= naParDeg; |
---|
130 | nInt = naInt; |
---|
131 | nAdd = naAdd; |
---|
132 | nSub = naSub; |
---|
133 | nMult = naMult; |
---|
134 | nDiv = naDiv; |
---|
135 | nExactDiv= naDiv; |
---|
136 | nIntDiv= naIntDiv; |
---|
137 | nIntMod= ndIntMod; /* dummy !! */ |
---|
138 | nNeg = naNeg; |
---|
139 | nInvers= naInvers; |
---|
140 | nCopy = naCopy; |
---|
141 | nGreater = naGreater; |
---|
142 | nEqual = naEqual; |
---|
143 | nIsZero = naIsZero; |
---|
144 | nIsOne = naIsOne; |
---|
145 | nIsMOne = naIsMOne; |
---|
146 | nGreaterZero = naGreaterZero; |
---|
147 | nWrite = naWrite; |
---|
148 | nRead = naRead; |
---|
149 | nPower = naPower; |
---|
150 | nGcd = naGcd; |
---|
151 | nLcm = naLcm; |
---|
152 | nSetMap = naSetMap; |
---|
153 | nName= naName; |
---|
154 | nSize = naSize; |
---|
155 | #ifdef LDEBUG |
---|
156 | nDBTest=naDBTest; |
---|
157 | #endif |
---|
158 | } |
---|
159 | } |
---|
160 | else |
---|
161 | if (rField_is_Q(r)) |
---|
162 | { |
---|
163 | #ifdef LDEBUG |
---|
164 | nDBDelete= nlDBDelete; |
---|
165 | #else |
---|
166 | nDelete= nlDelete; |
---|
167 | #endif |
---|
168 | if (complete) |
---|
169 | { |
---|
170 | test |= Sy_bit(OPT_INTSTRATEGY); /*26*/ |
---|
171 | nNew = nlNew; |
---|
172 | nNormalize=nlNormalize; |
---|
173 | nInit = nlInit; |
---|
174 | nInt = nlInt; |
---|
175 | nAdd = nlAdd; |
---|
176 | nSub = nlSub; |
---|
177 | nMult = nlMult; |
---|
178 | nDiv = nlDiv; |
---|
179 | nExactDiv= nlExactDiv; |
---|
180 | nIntDiv= nlIntDiv; |
---|
181 | nIntMod= nlIntMod; |
---|
182 | nNeg = nlNeg; |
---|
183 | nInvers= nlInvers; |
---|
184 | nCopy = nlCopy; |
---|
185 | nGreater = nlGreater; |
---|
186 | nEqual = nlEqual; |
---|
187 | nIsZero = nlIsZero; |
---|
188 | nIsOne = nlIsOne; |
---|
189 | nIsMOne = nlIsMOne; |
---|
190 | nGreaterZero = nlGreaterZero; |
---|
191 | nWrite = nlWrite; |
---|
192 | nRead = nlRead; |
---|
193 | nPower = nlPower; |
---|
194 | nGcd = nlGcd; |
---|
195 | nLcm = nlLcm; |
---|
196 | nSetMap = nlSetMap; |
---|
197 | nName= ndName; |
---|
198 | nSize = nlSize; |
---|
199 | #ifdef LDEBUG |
---|
200 | nDBTest=nlDBTest; |
---|
201 | #endif |
---|
202 | } |
---|
203 | } |
---|
204 | else if (rField_is_Zp(r)) |
---|
205 | /*----------------------char. p----------------*/ |
---|
206 | { |
---|
207 | #ifdef LDEBUG |
---|
208 | nDBDelete= nDBDummy1; |
---|
209 | #else |
---|
210 | nDelete= nDummy1; |
---|
211 | #endif |
---|
212 | if (complete) |
---|
213 | { |
---|
214 | npSetChar(c); |
---|
215 | test &= ~Sy_bit(OPT_INTSTRATEGY); /*26*/ |
---|
216 | nNew = nDummy1; |
---|
217 | nNormalize=nDummy2; |
---|
218 | nInit = npInit; |
---|
219 | nInt = npInt; |
---|
220 | nAdd = npAdd; |
---|
221 | nSub = npSub; |
---|
222 | nMult = npMult; |
---|
223 | nDiv = npDiv; |
---|
224 | nExactDiv= npDiv; |
---|
225 | nIntDiv= npDiv; |
---|
226 | nIntMod= ndIntMod; /* dummy !! */ |
---|
227 | nNeg = npNeg; |
---|
228 | nInvers= npInvers; |
---|
229 | nCopy = npCopy; |
---|
230 | nGreater = npGreater; |
---|
231 | nEqual = npEqual; |
---|
232 | nIsZero = npIsZero; |
---|
233 | nIsOne = npIsOne; |
---|
234 | nIsMOne = npIsMOne; |
---|
235 | nGreaterZero = npGreaterZero; |
---|
236 | nWrite = npWrite; |
---|
237 | nRead = npRead; |
---|
238 | nPower = npPower; |
---|
239 | nGcd = ndGcd; |
---|
240 | nLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
241 | nSetMap = npSetMap; |
---|
242 | nName= ndName; |
---|
243 | /*nSize = ndSize;*/ |
---|
244 | #ifdef LDEBUG |
---|
245 | nDBTest=npDBTest; |
---|
246 | #endif |
---|
247 | } |
---|
248 | } |
---|
249 | else |
---|
250 | /* -------------- GF(p^m) -----------------------*/ |
---|
251 | if (rField_is_GF(r)) |
---|
252 | { |
---|
253 | #ifdef LDEBUG |
---|
254 | nDBDelete= nDBDummy1; |
---|
255 | #else |
---|
256 | nDelete= nDummy1; |
---|
257 | #endif |
---|
258 | if (complete) |
---|
259 | { |
---|
260 | test &= ~Sy_bit(OPT_INTSTRATEGY); /*26*/ |
---|
261 | nfSetChar(c,r->parameter); |
---|
262 | nNew = nDummy1; |
---|
263 | nNormalize=nDummy2; |
---|
264 | nInit = nfInit; |
---|
265 | nPar = nfPar; |
---|
266 | nParDeg= nfParDeg; |
---|
267 | nInt = nfInt; |
---|
268 | nAdd = nfAdd; |
---|
269 | nSub = nfSub; |
---|
270 | nMult = nfMult; |
---|
271 | nDiv = nfDiv; |
---|
272 | nExactDiv= nfDiv; |
---|
273 | nIntDiv= nfDiv; |
---|
274 | nIntMod= ndIntMod; /* dummy !! */ |
---|
275 | nNeg = nfNeg; |
---|
276 | nInvers= nfInvers; |
---|
277 | nCopy = nfCopy; |
---|
278 | nGreater = nfGreater; |
---|
279 | nEqual = nfEqual; |
---|
280 | nIsZero = nfIsZero; |
---|
281 | nIsOne = nfIsOne; |
---|
282 | nIsMOne = nfIsMOne; |
---|
283 | nGreaterZero = nfGreaterZero; |
---|
284 | nWrite = nfWrite; |
---|
285 | nRead = nfRead; |
---|
286 | nPower = nfPower; |
---|
287 | nGcd = ndGcd; |
---|
288 | nLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
289 | nSetMap = nfSetMap; |
---|
290 | nName= nfName; |
---|
291 | /*nSize = ndSize;*/ |
---|
292 | #ifdef LDEBUG |
---|
293 | nDBTest=nfDBTest; |
---|
294 | #endif |
---|
295 | } |
---|
296 | } |
---|
297 | else |
---|
298 | /* -------------- R -----------------------*/ |
---|
299 | //if (c==(-1)) |
---|
300 | if (rField_is_R(r)) |
---|
301 | { |
---|
302 | #ifdef LDEBUG |
---|
303 | nDBDelete= nDBDummy1; |
---|
304 | #else |
---|
305 | nDelete= nDummy1; |
---|
306 | #endif |
---|
307 | if (complete) |
---|
308 | { |
---|
309 | nNew=nDummy1; |
---|
310 | nNormalize=nDummy2; |
---|
311 | nInit = nrInit; |
---|
312 | nInt = nrInt; |
---|
313 | nAdd = nrAdd; |
---|
314 | nSub = nrSub; |
---|
315 | nMult = nrMult; |
---|
316 | nDiv = nrDiv; |
---|
317 | nExactDiv= nrDiv; |
---|
318 | nIntDiv= nrDiv; |
---|
319 | nIntMod= ndIntMod; /* dummy !! */ |
---|
320 | nNeg = nrNeg; |
---|
321 | nInvers= nrInvers; |
---|
322 | nCopy = nrCopy; |
---|
323 | nGreater = nrGreater; |
---|
324 | nEqual = nrEqual; |
---|
325 | nIsZero = nrIsZero; |
---|
326 | nIsOne = nrIsOne; |
---|
327 | nIsMOne = nrIsMOne; |
---|
328 | nGreaterZero = nrGreaterZero; |
---|
329 | nWrite = nrWrite; |
---|
330 | nRead = nrRead; |
---|
331 | nPower = nrPower; |
---|
332 | nGcd = ndGcd; |
---|
333 | nLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
334 | nSetMap=nrSetMap; |
---|
335 | nName=ndName; |
---|
336 | /*nSize = ndSize;*/ |
---|
337 | #ifdef LDEBUG |
---|
338 | nDBTest=nrDBTest; |
---|
339 | #endif |
---|
340 | } |
---|
341 | } |
---|
342 | else |
---|
343 | /* -------------- long R -----------------------*/ |
---|
344 | if (rField_is_long_R(r)) |
---|
345 | { |
---|
346 | setGMPFloatPrecBytes(r->ch_flags); |
---|
347 | #ifdef LDEBUG |
---|
348 | nDBDelete= ngfDBDelete; |
---|
349 | #else |
---|
350 | nDelete= ngfDelete; |
---|
351 | #endif |
---|
352 | if (complete) |
---|
353 | { |
---|
354 | nNew=ngfNew; |
---|
355 | nNormalize=nDummy2; |
---|
356 | nInit = ngfInit; |
---|
357 | nInt = ngfInt; |
---|
358 | nAdd = ngfAdd; |
---|
359 | nSub = ngfSub; |
---|
360 | nMult = ngfMult; |
---|
361 | nDiv = ngfDiv; |
---|
362 | nExactDiv= ngfDiv; |
---|
363 | nIntDiv= ngfDiv; |
---|
364 | nIntMod= ndIntMod; /* dummy !! */ |
---|
365 | nNeg = ngfNeg; |
---|
366 | nInvers= ngfInvers; |
---|
367 | nCopy = ngfCopy; |
---|
368 | nGreater = ngfGreater; |
---|
369 | nEqual = ngfEqual; |
---|
370 | nIsZero = ngfIsZero; |
---|
371 | nIsOne = ngfIsOne; |
---|
372 | nIsMOne = ngfIsMOne; |
---|
373 | nGreaterZero = ngfGreaterZero; |
---|
374 | nWrite = ngfWrite; |
---|
375 | nRead = ngfRead; |
---|
376 | nPower = ngfPower; |
---|
377 | nGcd = ndGcd; |
---|
378 | nLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
379 | nSetMap=ngfSetMap; |
---|
380 | nName=ndName; |
---|
381 | /*nSize = ndSize;*/ |
---|
382 | #ifdef LDEBUG |
---|
383 | nDBTest=ngfDBTest; |
---|
384 | #endif |
---|
385 | } |
---|
386 | } |
---|
387 | else |
---|
388 | /* -------------- long C -----------------------*/ |
---|
389 | else if (rField_is_long_C(r)) |
---|
390 | { |
---|
391 | setGMPFloatPrecBytes(r->ch_flags); |
---|
392 | #ifdef LDEBUG |
---|
393 | nDBDelete= ngcDBDelete; |
---|
394 | #else |
---|
395 | nDelete= ngcDelete; |
---|
396 | #endif |
---|
397 | if (complete) |
---|
398 | { |
---|
399 | nNew=ngcNew; |
---|
400 | nNormalize=nDummy2; |
---|
401 | nInit = ngcInit; |
---|
402 | nInt = ngcInt; |
---|
403 | nAdd = ngcAdd; |
---|
404 | nSub = ngcSub; |
---|
405 | nMult = ngcMult; |
---|
406 | nDiv = ngcDiv; |
---|
407 | nExactDiv= ngcDiv; |
---|
408 | nIntDiv= ngcDiv; |
---|
409 | nIntMod= ndIntMod; /* dummy !! */ |
---|
410 | nNeg = ngcNeg; |
---|
411 | nInvers= ngcInvers; |
---|
412 | nCopy = ngcCopy; |
---|
413 | nGreater = ngcGreater; |
---|
414 | nEqual = ngcEqual; |
---|
415 | nIsZero = ngcIsZero; |
---|
416 | nIsOne = ngcIsOne; |
---|
417 | nIsMOne = ngcIsMOne; |
---|
418 | nGreaterZero = ngcGreaterZero; |
---|
419 | nWrite = ngcWrite; |
---|
420 | nRead = ngcRead; |
---|
421 | nPower = ngcPower; |
---|
422 | nGcd = ndGcd; |
---|
423 | nLcm = ndGcd; /* tricky, isn't it ?*/ |
---|
424 | nSetMap=ngcSetMap; |
---|
425 | nName=ndName; |
---|
426 | nPar=ngcPar; |
---|
427 | /*nSize = ndSize;*/ |
---|
428 | #ifdef LDEBUG |
---|
429 | nDBTest=ngcDBTest; |
---|
430 | #endif |
---|
431 | } |
---|
432 | } |
---|
433 | #ifdef TEST |
---|
434 | else |
---|
435 | { |
---|
436 | WerrorS("unknown field"); |
---|
437 | } |
---|
438 | #endif |
---|
439 | if (complete&&(!errorreported)) nNULL=nInit(0); |
---|
440 | } |
---|
441 | |
---|