1 | #ifndef COEFFS_H |
---|
2 | #define COEFFS_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id$ */ |
---|
7 | /* |
---|
8 | * ABSTRACT |
---|
9 | */ |
---|
10 | |
---|
11 | #include <misc/auxiliary.h> |
---|
12 | /* for assume: */ |
---|
13 | #include <reporter/reporter.h> |
---|
14 | #include <coeffs/si_gmp.h> |
---|
15 | |
---|
16 | enum n_coeffType |
---|
17 | { |
---|
18 | n_unknown=0, |
---|
19 | n_Zp, |
---|
20 | n_Q, |
---|
21 | n_R, |
---|
22 | n_GF, |
---|
23 | n_long_R, |
---|
24 | n_Zp_a, |
---|
25 | n_Q_a, |
---|
26 | n_long_C, |
---|
27 | // only used if HAVE_RINGS is defined |
---|
28 | n_Z, |
---|
29 | n_Zn, |
---|
30 | n_Zpn, // does no longer exist? |
---|
31 | n_Z2m, |
---|
32 | n_CF |
---|
33 | }; |
---|
34 | |
---|
35 | struct snumber; |
---|
36 | typedef struct snumber * number; |
---|
37 | |
---|
38 | /* standard types */ |
---|
39 | #ifdef HAVE_RINGS |
---|
40 | typedef unsigned long NATNUMBER; |
---|
41 | typedef mpz_ptr int_number; |
---|
42 | #endif |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | struct n_Procs_s; |
---|
49 | typedef struct n_Procs_s n_Procs_s; |
---|
50 | typedef struct n_Procs_s *coeffs; |
---|
51 | |
---|
52 | typedef number (*numberfunc)(number a, number b, const coeffs r); |
---|
53 | |
---|
54 | /// maps "a", which lives in src, into dst |
---|
55 | typedef number (*nMapFunc)(number a, const coeffs src, const coeffs dst); |
---|
56 | |
---|
57 | struct n_Procs_s |
---|
58 | { |
---|
59 | coeffs next; |
---|
60 | unsigned int ringtype; /* 0 => coefficient field, 1 => coeffs from Z/2^m */ |
---|
61 | |
---|
62 | // general properties: |
---|
63 | /// TRUE, if nNew/nDelete/nCopy are dummies |
---|
64 | BOOLEAN has_simple_Alloc; |
---|
65 | /// TRUE, if std should make polynomials monic (if nInvers is cheap) |
---|
66 | /// if false, then a gcd routine is required for a content computation |
---|
67 | BOOLEAN has_simple_Inverse; |
---|
68 | |
---|
69 | // tests for numbers.cc: |
---|
70 | BOOLEAN (*nCoeffIsEqual)(const coeffs r, n_coeffType n, void * parameter); |
---|
71 | |
---|
72 | // the union stuff |
---|
73 | |
---|
74 | // Zp: |
---|
75 | int npPrimeM; |
---|
76 | int npPminus1M; |
---|
77 | #ifdef HAVE_DIV_MOD |
---|
78 | unsigned short *npInvTable; |
---|
79 | #endif |
---|
80 | #if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD) |
---|
81 | unsigned short *npExpTable; |
---|
82 | unsigned short *npLogTable; |
---|
83 | #endif |
---|
84 | // Zp_a, Q_a |
---|
85 | // ? |
---|
86 | // initialisation: |
---|
87 | //void (*cfInitChar)(coeffs r, int parameter); // do one-time initialisations |
---|
88 | void (*cfKillChar)(coeffs r); // undo all initialisations |
---|
89 | // or NULL |
---|
90 | void (*cfSetChar)(const coeffs r); // initialisations after each ring change |
---|
91 | // or NULL |
---|
92 | // general stuff |
---|
93 | numberfunc cfMult, cfSub ,cfAdd ,cfDiv, cfIntDiv, cfIntMod, cfExactDiv; |
---|
94 | /// init with an integer |
---|
95 | number (*cfInit)(int i,const coeffs r); |
---|
96 | number (*cfPar)(int i, const coeffs r); |
---|
97 | int (*cfParDeg)(number n, const coeffs r); |
---|
98 | /// how complicated, (0) => 0, or positive |
---|
99 | int (*cfSize)(number n, const coeffs r); |
---|
100 | /// convertion, 0 if impossible |
---|
101 | int (*cfInt)(number &n, const coeffs r); |
---|
102 | |
---|
103 | #ifdef HAVE_RINGS |
---|
104 | int (*cfDivComp)(number a,number b,const coeffs r); |
---|
105 | BOOLEAN (*cfIsUnit)(number a,const coeffs r); |
---|
106 | number (*cfGetUnit)(number a,const coeffs r); |
---|
107 | number (*cfExtGcd)(number a, number b, number *s, number *t,const coeffs r); |
---|
108 | #endif |
---|
109 | |
---|
110 | /// changes argument inline: a:= -a |
---|
111 | number (*cfNeg)(number a, const coeffs r); |
---|
112 | /// return 1/a |
---|
113 | number (*cfInvers)(number a, const coeffs r); |
---|
114 | /// return a copy of a |
---|
115 | number (*cfCopy)(number a, const coeffs r); |
---|
116 | number (*cfRePart)(number a, const coeffs r); |
---|
117 | number (*cfImPart)(number a, const coeffs r); |
---|
118 | void (*cfWrite)(number &a, const coeffs r); |
---|
119 | const char * (*cfRead)(const char * s, number * a, const coeffs r); |
---|
120 | void (*cfNormalize)(number &a, const coeffs r); |
---|
121 | BOOLEAN (*cfGreater)(number a,number b, const coeffs r), |
---|
122 | #ifdef HAVE_RINGS |
---|
123 | (*cfDivBy)(number a, number b, const coeffs r), |
---|
124 | #endif |
---|
125 | /// tests |
---|
126 | (*cfEqual)(number a,number b, const coeffs r), |
---|
127 | (*cfIsZero)(number a, const coeffs r), |
---|
128 | (*cfIsOne)(number a, const coeffs r), |
---|
129 | (*cfIsMOne)(number a, const coeffs r), |
---|
130 | (*cfGreaterZero)(number a, const coeffs r); |
---|
131 | |
---|
132 | void (*cfPower)(number a, int i, number * result, const coeffs r); |
---|
133 | number (*cfGetDenom)(number &n, const coeffs r); |
---|
134 | number (*cfGetNumerator)(number &n, const coeffs r); |
---|
135 | number (*cfGcd)(number a, number b, const coeffs r); |
---|
136 | number (*cfLcm)(number a, number b, const coeffs r); |
---|
137 | void (*cfDelete)(number * a, const coeffs r); |
---|
138 | nMapFunc (*cfSetMap)(const coeffs src, const coeffs dst); |
---|
139 | |
---|
140 | /// For extensions (writes into global string buffer) |
---|
141 | char * (*cfName)(number n, const coeffs r); |
---|
142 | |
---|
143 | /// Inline: a := b |
---|
144 | void (*cfInpMult)(number &a, number b, const coeffs r); |
---|
145 | /// maps the bigint i (from dummy) into the coeffs dst |
---|
146 | number (*cfInit_bigint)(number i, const coeffs dummy, const coeffs dst); |
---|
147 | |
---|
148 | #ifdef LDEBUG |
---|
149 | /// Test: is "a" a correct number? |
---|
150 | BOOLEAN (*cfDBTest)(number a, const char *f, const int l, const coeffs r); |
---|
151 | #endif |
---|
152 | |
---|
153 | number nNULL; /* the 0 as constant */ |
---|
154 | int char_flag; |
---|
155 | int ref; |
---|
156 | n_coeffType type; |
---|
157 | ////----------------------------------------- |
---|
158 | char** parameter; /* names of parameters, rInit */ |
---|
159 | number minpoly; /* for Q_a/Zp_a, rInit */ |
---|
160 | |
---|
161 | #ifdef HAVE_RINGS |
---|
162 | /* The following members are for representing the ring Z/n, |
---|
163 | where n is not a prime. We distinguish three cases: |
---|
164 | 1.) n has at least two distinct prime factors. Then |
---|
165 | modBase stores n, modExponent stores 1, modNumber |
---|
166 | stores n, and mod2mMask is not used; |
---|
167 | 2.) n = p^k for some odd prime p and k > 1. Then |
---|
168 | modBase stores p, modExponent stores k, modNumber |
---|
169 | stores n, and mod2mMask is not used; |
---|
170 | 3.) n = 2^k for some k > 1; moreover, 2^k - 1 fits in |
---|
171 | an unsigned long. Then modBase stores 2, modExponent |
---|
172 | stores k, modNumber is not used, and mod2mMask stores |
---|
173 | 2^k - 1, i.e., the bit mask '111..1' of length k. |
---|
174 | 4.) n = 2^k for some k > 1; but 2^k - 1 does not fit in |
---|
175 | an unsigned long. Then modBase stores 2, modExponent |
---|
176 | stores k, modNumber stores n, and mod2mMask is not |
---|
177 | used; |
---|
178 | Cases 1.), 2.), and 4.) are covered by the implementation |
---|
179 | in the files rmodulon.h and rmodulon.cc, whereas case 3.) |
---|
180 | is implemented in the files rmodulo2m.h and rmodulo2m.cc. */ |
---|
181 | int_number modBase; |
---|
182 | unsigned long modExponent; |
---|
183 | int_number modNumber; |
---|
184 | unsigned long mod2mMask; |
---|
185 | #endif |
---|
186 | int ch; /* characteristic, rInit */ |
---|
187 | |
---|
188 | short float_len; /* additional char-flags, rInit */ |
---|
189 | short float_len2; /* additional char-flags, rInit */ |
---|
190 | |
---|
191 | BOOLEAN ShortOut; /// ffields need this. |
---|
192 | |
---|
193 | // --------------------------------------------------- |
---|
194 | // for n_GF |
---|
195 | |
---|
196 | int m_nfCharQ; ///< the number of elemts: q |
---|
197 | int m_nfM1; ///< representation of -1 |
---|
198 | int m_nfCharP; ///< the characteristic: p |
---|
199 | int m_nfCharQ1; ///< q-1 |
---|
200 | unsigned short *m_nfPlus1Table; |
---|
201 | int *m_nfMinPoly; |
---|
202 | char * m_nfParameter; |
---|
203 | }; |
---|
204 | // |
---|
205 | // test properties and type |
---|
206 | /// Returns the type of coeffs domain |
---|
207 | static inline n_coeffType getCoeffType(const coeffs r) |
---|
208 | { |
---|
209 | return r->type; |
---|
210 | } |
---|
211 | |
---|
212 | /// returns true for coeffs being a domain |
---|
213 | static inline bool nField_is_Domain(const coeffs r) |
---|
214 | { |
---|
215 | #ifdef HAVE_RINGS |
---|
216 | return (r->ringtype == 4 || r->ringtype == 0); |
---|
217 | #else |
---|
218 | return true; |
---|
219 | #endif |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | static inline int nInternalChar(const coeffs r) |
---|
224 | { |
---|
225 | return r->ch; |
---|
226 | } |
---|
227 | |
---|
228 | /// one-time initialisations for new coeffs |
---|
229 | /// in case of an error return NULL |
---|
230 | coeffs nInitChar(n_coeffType t, void * parameter); |
---|
231 | |
---|
232 | /// undo all initialisations |
---|
233 | void nKillChar(coeffs r); |
---|
234 | |
---|
235 | /// initialisations after each ring change |
---|
236 | static inline void nSetChar(const coeffs r) |
---|
237 | { |
---|
238 | assume(r!=NULL); // r==NULL is an error |
---|
239 | if (r->cfSetChar!=NULL) r->cfSetChar(r); |
---|
240 | } |
---|
241 | |
---|
242 | void nNew(number * a); |
---|
243 | #define n_New(n, r) nNew(n) |
---|
244 | |
---|
245 | |
---|
246 | // the access methods (part 2): |
---|
247 | |
---|
248 | /// return a copy of a |
---|
249 | static inline number n_Copy(number n, const coeffs r) |
---|
250 | { return r->cfCopy(n, r); } |
---|
251 | |
---|
252 | static inline void n_Delete(number* p, const coeffs r) |
---|
253 | { r->cfDelete(p, r); } |
---|
254 | |
---|
255 | static inline BOOLEAN n_Equal(number a, number b, const coeffs r) |
---|
256 | { return r->cfEqual(a, b, r); } |
---|
257 | |
---|
258 | static inline BOOLEAN n_IsZero(number n, const coeffs r) |
---|
259 | { return r->cfIsZero(n,r); } |
---|
260 | |
---|
261 | static inline BOOLEAN n_IsOne(number n, const coeffs r) |
---|
262 | { return r->cfIsOne(n,r); } |
---|
263 | |
---|
264 | static inline BOOLEAN n_IsMOne(number n, const coeffs r) |
---|
265 | { return r->cfIsMOne(n,r); } |
---|
266 | |
---|
267 | static inline BOOLEAN n_GreaterZero(number n, const coeffs r) |
---|
268 | { return r->cfGreaterZero(n,r); } |
---|
269 | // cfGreater? |
---|
270 | |
---|
271 | #ifdef HAVE_RINGS |
---|
272 | static inline BOOLEAN n_IsUnit(number n, const coeffs r) |
---|
273 | { return r->cfIsUnit(n,r); } |
---|
274 | |
---|
275 | static inline number n_GetUnit(number n, const coeffs r) |
---|
276 | { return r->cfGetUnit(n,r); } |
---|
277 | |
---|
278 | static inline BOOLEAN n_DivBy(number a, number b, const coeffs r) |
---|
279 | { return r->cfDivBy(a,b,r); } |
---|
280 | #endif |
---|
281 | |
---|
282 | /// init with an integer |
---|
283 | static inline number n_Init(int i, const coeffs r) |
---|
284 | { return r->cfInit(i,r); } |
---|
285 | |
---|
286 | /// changes argument inline: a:= -a |
---|
287 | static inline number n_Neg(number n, const coeffs r) |
---|
288 | { return r->cfNeg(n,r); } |
---|
289 | |
---|
290 | /// return 1/a |
---|
291 | static inline number n_Invers(number a, const coeffs r) |
---|
292 | { return r->cfInvers(a,r); } |
---|
293 | |
---|
294 | /// use for pivot strategies, (0) => 0, otherwise positive |
---|
295 | static inline int n_Size(number n, const coeffs r) |
---|
296 | { return r->cfSize(n,r); } |
---|
297 | |
---|
298 | /// normalize the number. i.e. go to some canonnical representation (inplace) |
---|
299 | static inline void n_Normalize(number& n, const coeffs r) |
---|
300 | { return r->cfNormalize(n,r); } |
---|
301 | |
---|
302 | /// Normalize and Write to the output buffer of reporter |
---|
303 | static inline void n_Write(number& n, const coeffs r) |
---|
304 | { return r->cfWrite(n,r); } |
---|
305 | |
---|
306 | /// Normalize and get denomerator |
---|
307 | static inline number n_GetDenom(number& n, const coeffs r) |
---|
308 | { return r->cfGetDenom(n, r); } |
---|
309 | |
---|
310 | /// Normalize and get numerator |
---|
311 | static inline number n_GetNumerator(number& n, const coeffs r) |
---|
312 | { return r->cfGetNumerator(n, r); } |
---|
313 | |
---|
314 | static inline void n_Power(number a, int b, number *res, const coeffs r) |
---|
315 | { r->cfPower(a,b,res,r); } |
---|
316 | |
---|
317 | static inline number n_Mult(number a, number b, const coeffs r) |
---|
318 | { return r->cfMult(a, b, r); } |
---|
319 | |
---|
320 | /// Inplace multiplication: a := a * b |
---|
321 | static inline void n_InpMult(number &a, number b, const coeffs r) |
---|
322 | { r->cfInpMult(a,b,r); } |
---|
323 | |
---|
324 | static inline number n_Sub(number a, number b, const coeffs r) |
---|
325 | { return r->cfSub(a, b, r); } |
---|
326 | |
---|
327 | static inline number n_Add(number a, number b, const coeffs r) |
---|
328 | { return r->cfAdd(a, b, r); } |
---|
329 | |
---|
330 | static inline number n_Div(number a, number b, const coeffs r) |
---|
331 | { return r->cfDiv(a,b,r); } |
---|
332 | |
---|
333 | static inline number n_IntDiv(number a, number b, const coeffs r) |
---|
334 | { return r->cfIntDiv(a,b,r); } |
---|
335 | |
---|
336 | static inline number n_ExactDiv(number a, number b, const coeffs r) |
---|
337 | { return r->cfExactDiv(a,b,r); } |
---|
338 | |
---|
339 | static inline number n_Gcd(number a, number b, const coeffs r) |
---|
340 | { return r->cfGcd(a,b,r); } |
---|
341 | |
---|
342 | static inline number n_Lcm(number a, number b, const coeffs r) |
---|
343 | { return r->cfLcm(a,b,r); } |
---|
344 | |
---|
345 | static inline nMapFunc n_SetMap(const coeffs src, const coeffs dst) |
---|
346 | { return dst->cfSetMap(src,dst); } |
---|
347 | |
---|
348 | static inline number n_Par(int n, const coeffs r) |
---|
349 | { return r->cfPar(n,r); } |
---|
350 | |
---|
351 | static inline int n_ParDeg(number n, const coeffs r) |
---|
352 | { return r->cfParDeg(n,r); } |
---|
353 | |
---|
354 | /// Tests whether n is a correct number: only used if LDEBUG is defined |
---|
355 | static inline BOOLEAN n_DBTest(number n, const char *filename, const int linenumber, const coeffs r) |
---|
356 | { |
---|
357 | #ifdef LDEBUG |
---|
358 | return (r)->cfDBTest(n, filename, linenumber, r); |
---|
359 | #else |
---|
360 | return TRUE; |
---|
361 | #endif |
---|
362 | } |
---|
363 | |
---|
364 | /// BOOLEAN n_Test(number a, const coeffs r) |
---|
365 | #define n_Test(a,r) n_DBTest(a, __FILE__, __LINE__, r) |
---|
366 | |
---|
367 | // Missing wrappers for: |
---|
368 | // cfIntMod, cfInt, cfRePart, cfImPart, cfRead, cfName, cfInit_bigint |
---|
369 | // HAVE_RINGS: cfDivComp, cfExtGcd... cfDivBy |
---|
370 | |
---|
371 | |
---|
372 | // Deprecated: |
---|
373 | static inline int n_GetChar(const coeffs r) |
---|
374 | { return nInternalChar(r); } |
---|
375 | |
---|
376 | |
---|
377 | |
---|
378 | /* |
---|
379 | // the access methods (part 1) (see also part2 below): |
---|
380 | // |
---|
381 | // the routines w.r.t. currRing: |
---|
382 | // (should only be used in the context of currRing, i.e. in the interpreter) |
---|
383 | #define nCopy(n) n_Copy(n, currRing->cf) |
---|
384 | #define nDelete(n) n_Delete(n, currRing->cf) |
---|
385 | #define nMult(n1, n2) n_Mult(n1, n2, currRing->cf) |
---|
386 | #define nAdd(n1, n2) n_Add(n1, n2, currRing->cf) |
---|
387 | #define nIsZero(n) n_IsZero(n, currRing->cf) |
---|
388 | #define nEqual(n1, n2) n_Equal(n1, n2, currRing->cf) |
---|
389 | #define nNeg(n) n_Neg(n, currRing->cf) |
---|
390 | #define nSub(n1, n2) n_Sub(n1, n2, currRing->cf) |
---|
391 | #define nGetChar() nInternalChar(currRing->cf) |
---|
392 | #define nInit(i) n_Init(i, currRing->cf) |
---|
393 | #define nIsOne(n) n_IsOne(n, currRing->cf) |
---|
394 | #define nIsMOne(n) n_IsMOne(n, currRing->cf) |
---|
395 | #define nGreaterZero(n) n_GreaterZero(n, currRing->cf) |
---|
396 | #define nWrite(n) n_Write(n,currRing->cf) |
---|
397 | #define nNormalize(n) n_Normalize(n,currRing->cf) |
---|
398 | #define nGcd(a, b) n_Gcd(a,b,currRing->cf) |
---|
399 | #define nIntDiv(a, b) n_IntDiv(a,b,currRing->cf) |
---|
400 | #define nDiv(a, b) n_Div(a,b,currRing->cf) |
---|
401 | #define nInvers(a) n_Invers(a,currRing->cf) |
---|
402 | #define nExactDiv(a, b) n_ExactDiv(a,b,currRing->cf) |
---|
403 | #define nTest(a) n_Test(a,currRing->cf) |
---|
404 | |
---|
405 | #define nInpMult(a, b) n_InpMult(a,b,currRing->cf) |
---|
406 | #define nPower(a, b, res) n_Power(a,b,res,currRing->cf) |
---|
407 | #define nSize(n) n_Size(n,currRing->cf) |
---|
408 | #define nGetDenom(N) n_GetDenom((N),currRing->cf) |
---|
409 | #define nGetNumerator(N) n_GetNumerator((N),currRing->cf) |
---|
410 | |
---|
411 | #define nSetMap(R) n_SetMap(R,currRing->cf) |
---|
412 | */ |
---|
413 | |
---|
414 | #endif |
---|
415 | |
---|