source: git/libpolys/coeffs/coeffs.h @ 48a994

fieker-DuValspielwiese
Last change on this file since 48a994 was 48a994, checked in by Frank Seelisch <seelisch@…>, 13 years ago
factory-related includes
  • Property mode set to 100644
File size: 17.5 KB
Line 
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
15#include <coeffs/si_gmp.h>
16
17#ifdef HAVE_FACTORY
18class CanonicalForm;
19#endif
20
21enum n_coeffType
22{
23  n_unknown=0,
24  n_Zp,
25  n_Q,
26  n_R,
27  n_GF,
28  n_long_R,
29  n_Ext,  // used for all extensions (of Zp, of Q AND OF EXTENSIONS THEREOF)
30  n_long_C,
31  // only used if HAVE_RINGS is defined:
32  n_Z,
33  n_Zn,
34  n_Zpn, // does no longer exist?
35  n_Z2m,
36  n_CF
37};
38
39struct snumber;
40typedef struct snumber *   number;
41
42struct snumber;
43typedef struct snumber *   number;
44
45/* standard types */
46#ifdef HAVE_RINGS
47typedef unsigned long NATNUMBER;
48typedef mpz_ptr int_number;
49#endif
50
51struct ip_sring;
52typedef struct ip_sring *         ring;
53
54struct n_Procs_s;
55typedef struct  n_Procs_s  n_Procs_s;
56typedef struct  n_Procs_s  *coeffs;
57
58typedef number (*numberfunc)(number a, number b, const coeffs r);
59
60/// maps "a", which lives in src, into dst
61typedef number (*nMapFunc)(number a, const coeffs src, const coeffs dst);
62
63struct n_Procs_s
64{
65   coeffs next;
66   unsigned int ringtype;  /* =0 => coefficient field,
67                             !=0 => coeffs from one of the rings */
68
69   // general properties:
70   /// TRUE, if nNew/nDelete/nCopy are dummies
71   BOOLEAN has_simple_Alloc;
72   /// TRUE, if std should make polynomials monic (if nInvers is cheap)
73   /// if false, then a gcd routine is used for a content computation
74   BOOLEAN has_simple_Inverse;
75
76   // tests for numbers.cc:
77   BOOLEAN (*nCoeffIsEqual)(const coeffs r, n_coeffType n, void * parameter);
78
79   /// output of coeff description via Print
80   void (*cfCoeffWrite)(const coeffs r);
81
82   // the union stuff
83
84   // Zp:
85   int npPrimeM;
86   int npPminus1M;
87   #ifdef HAVE_DIV_MOD
88   unsigned short *npInvTable;
89   #endif
90   #if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD)
91   unsigned short *npExpTable;
92   unsigned short *npLogTable;
93   #endif
94
95   // ?
96   // initialisation:
97   //void (*cfInitChar)(coeffs r, int parameter); // do one-time initialisations
98   void (*cfKillChar)(coeffs r); //  undo all initialisations
99                                // or NULL
100   void (*cfSetChar)(const coeffs r); // initialisations after each ring change
101                                // or NULL
102   // general stuff
103   numberfunc cfMult, cfSub ,cfAdd ,cfDiv, cfIntDiv, cfIntMod, cfExactDiv;
104   /// init with an integer
105   number  (*cfInit)(int i,const coeffs r);
106   number  (*cfPar)(int i, const coeffs r);
107   int     (*cfParDeg)(number n, const coeffs r);
108   /// how complicated, (0) => 0, or positive
109   int     (*cfSize)(number n, const coeffs r);
110   /// convertion, 0 if impossible
111   int     (*cfInt)(number &n, const coeffs r);
112
113#ifdef HAVE_RINGS
114   int     (*cfDivComp)(number a,number b,const coeffs r);
115   BOOLEAN (*cfIsUnit)(number a,const coeffs r);
116   number  (*cfGetUnit)(number a,const coeffs r);
117   number  (*cfExtGcd)(number a, number b, number *s, number *t,const coeffs r);
118#endif
119
120   /// changes argument  inline: a:= -a
121   number  (*cfNeg)(number a, const coeffs r);
122   /// return 1/a
123   number  (*cfInvers)(number a, const coeffs r);
124   /// return a copy of a
125   number  (*cfCopy)(number a, const coeffs r);
126   number  (*cfRePart)(number a, const coeffs r);
127   number  (*cfImPart)(number a, const coeffs r);
128   void    (*cfWrite)(number &a, const coeffs r);
129   const char *  (*cfRead)(const char * s, number * a, const coeffs r);
130   void    (*cfNormalize)(number &a, const coeffs r);
131   BOOLEAN (*cfGreater)(number a,number b, const coeffs r),
132#ifdef HAVE_RINGS
133           (*cfDivBy)(number a, number b, const coeffs r),
134#endif
135            /// tests
136           (*cfEqual)(number a,number b, const coeffs r),
137           (*cfIsZero)(number a, const coeffs r),
138           (*cfIsOne)(number a, const coeffs r),
139           (*cfIsMOne)(number a, const coeffs r),
140           (*cfGreaterZero)(number a, const coeffs r);
141
142   void    (*cfPower)(number a, int i, number * result, const coeffs r);
143   number  (*cfGetDenom)(number &n, const coeffs r);
144   number  (*cfGetNumerator)(number &n, const coeffs r);
145   number  (*cfGcd)(number a, number b, const coeffs r);
146   number  (*cfLcm)(number a, number b, const coeffs r);
147   void    (*cfDelete)(number * a, const coeffs r);
148   nMapFunc (*cfSetMap)(const coeffs src, const coeffs dst);
149
150   /// For extensions (writes into global string buffer)
151   char *  (*cfName)(number n, const coeffs r);
152
153   /// Inplace: a *= b
154   void    (*cfInpMult)(number &a, number b, const coeffs r);
155   /// maps the bigint i (from dummy) into the coeffs dst
156   number  (*cfInit_bigint)(number i, const coeffs dummy, const coeffs dst);
157
158#ifdef HAVE_FACTORY
159   number (*convFactoryNSingN)( const CanonicalForm n, const coeffs r);
160   CanonicalForm (*convSingNFactoryN)( number n, BOOLEAN setChar, const coeffs r );
161#endif
162
163
164#ifdef LDEBUG
165   /// Test: is "a" a correct number?
166   BOOLEAN (*cfDBTest)(number a, const char *f, const int l, const coeffs r);
167#endif
168
169   number nNULL; /* the 0 as constant */
170   int     char_flag;
171   int     ref;
172   n_coeffType type;
173//-------------------------------------------
174
175  /// For Zp_a, Q_a we need polynomials (due to polys)
176  ring          algring; //< implementation of extensions needs polynomials...
177  /// for Q_a/Zp_a, rInit
178  ///number     minpoly;  //< no longer needed: replaced by
179                          //  algring->minideal->[0]
180
181
182//-------------------------------------------
183  char* complex_parameter; //< the name of sqrt(-1), i.e. 'i' or 'j' etc...?
184
185#ifdef HAVE_RINGS
186  /* The following members are for representing the ring Z/n,
187     where n is not a prime. We distinguish four cases:
188     1.) n has at least two distinct prime factors. Then
189         modBase stores n, modExponent stores 1, modNumber
190         stores n, and mod2mMask is not used;
191     2.) n = p^k for some odd prime p and k > 1. Then
192         modBase stores p, modExponent stores k, modNumber
193         stores n, and mod2mMask is not used;
194     3.) n = 2^k for some k > 1; moreover, 2^k - 1 fits in
195         an unsigned long. Then modBase stores 2, modExponent
196         stores k, modNumber is not used, and mod2mMask stores
197         2^k - 1, i.e., the bit mask '111..1' of length k.
198     4.) n = 2^k for some k > 1; but 2^k - 1 does not fit in
199         an unsigned long. Then modBase stores 2, modExponent
200         stores k, modNumber stores n, and mod2mMask is not
201         used;
202     Cases 1.), 2.), and 4.) are covered by the implementation
203     in the files rmodulon.h and rmodulon.cc, whereas case 3.)
204     is implemented in the files rmodulo2m.h and rmodulo2m.cc. */
205  int_number    modBase;
206  unsigned long modExponent;
207  int_number    modNumber;
208  unsigned long mod2mMask;
209#endif
210  int        ch;  /* characteristic, rInit */
211
212  short      float_len; /* additional char-flags, rInit */
213  short      float_len2; /* additional char-flags, rInit */
214
215  BOOLEAN   ShortOut; /// ffields need this.
216
217// ---------------------------------------------------
218  // for n_GF
219
220  int m_nfCharQ;  ///< the number of elemts: q
221  int m_nfM1;       ///< representation of -1
222  int m_nfCharP;  ///< the characteristic: p
223  int m_nfCharQ1; ///< q-1
224  unsigned short *m_nfPlus1Table;
225  int *m_nfMinPoly;
226  char * m_nfParameter;
227};
228//
229// test properties and type
230/// Returns the type of coeffs domain
231static inline n_coeffType getCoeffType(const coeffs r)
232{
233  assume(r != NULL);
234  return r->type;
235}
236
237static inline int nInternalChar(const coeffs r)
238{
239  assume(r != NULL);
240  return r->ch;
241}
242
243/// one-time initialisations for new coeffs
244/// in case of an error return NULL
245coeffs nInitChar(n_coeffType t, void * parameter);
246
247/// undo all initialisations
248void nKillChar(coeffs r);
249
250/// initialisations after each ring change
251static inline void nSetChar(const coeffs r)
252{
253  assume(r!=NULL); // r==NULL is an error
254  if (r->cfSetChar!=NULL) r->cfSetChar(r);
255}
256
257void           nNew(number * a);
258#define n_New(n, r)           nNew(n)
259
260
261// the access methods (part 2):
262
263/// return a copy of a
264static inline number n_Copy(number n,    const coeffs r)
265{   assume(r != NULL); assume(r->cfCopy!=NULL); return r->cfCopy(n, r); }
266
267static inline void   n_Delete(number* p, const coeffs r)
268{   assume(r != NULL); assume(r->cfDelete!= NULL); r->cfDelete(p, r); }
269
270static inline BOOLEAN n_Equal(number a, number b, const coeffs r)
271{ assume(r != NULL); assume(r->cfEqual!=NULL); return r->cfEqual(a, b, r); }
272
273static inline BOOLEAN n_IsZero(number n, const coeffs r)
274{ assume(r != NULL); assume(r->cfIsZero!=NULL); return r->cfIsZero(n,r); }
275
276static inline BOOLEAN n_IsOne(number n,  const coeffs r)
277{ assume(r != NULL); assume(r->cfIsOne!=NULL); return r->cfIsOne(n,r); }
278
279static inline BOOLEAN n_IsMOne(number n, const coeffs r)
280{ assume(r != NULL); assume(r->cfIsMOne!=NULL); return r->cfIsMOne(n,r); }
281
282static inline BOOLEAN n_GreaterZero(number n, const coeffs r)
283{ assume(r != NULL); assume(r->cfGreaterZero!=NULL); return r->cfGreaterZero(n,r); }
284static inline BOOLEAN n_Greater(number a, number b, const coeffs r)
285{ assume(r != NULL); assume(r->cfGreater!=NULL); return r->cfGreater(a,b,r); }
286
287#ifdef HAVE_RINGS
288static inline BOOLEAN n_IsUnit(number n, const coeffs r)
289{ assume(r != NULL); assume(r->cfIsUnit!=NULL); return r->cfIsUnit(n,r); }
290
291static inline number n_GetUnit(number n, const coeffs r)
292{ assume(r != NULL); assume(r->cfGetUnit!=NULL); return r->cfGetUnit(n,r); }
293
294static inline BOOLEAN n_DivBy(number a, number b, const coeffs r)
295{ assume(r != NULL); assume(r->cfDivBy!=NULL); return r->cfDivBy(a,b,r); }
296#endif
297
298/// init with an integer
299static inline number n_Init(int i,       const coeffs r)
300{ assume(r != NULL); assume(r->cfInit!=NULL); return r->cfInit(i,r); }
301
302/// conversion to int; 0 if not possible
303static inline int n_Int(number n,        const coeffs r)
304{ assume(r != NULL); assume(r->cfInt!=NULL); return r->cfInt(n,r); }
305
306/// changes argument  inline: a:= -a
307static inline number n_Neg(number n,     const coeffs r)
308{ assume(r != NULL); assume(r->cfNeg!=NULL); return r->cfNeg(n,r); }
309
310/// return 1/a
311static inline number n_Invers(number a,  const coeffs r)
312{ assume(r != NULL); assume(r->cfInvers!=NULL); return r->cfInvers(a,r); }
313
314/// use for pivot strategies, (0) => 0, otherwise positive
315static inline int    n_Size(number n,    const coeffs r)
316{ assume(r != NULL); assume(r->cfSize!=NULL); return r->cfSize(n,r); }
317
318/// normalize the number. i.e. go to some canonnical representation (inplace)
319static inline void   n_Normalize(number& n, const coeffs r)
320{ assume(r != NULL); assume(r->cfNormalize!=NULL); r->cfNormalize(n,r); }
321
322/// Normalize and Write to the output buffer of reporter
323static inline void   n_Write(number& n,  const coeffs r)
324{ assume(r != NULL); assume(r->cfWrite!=NULL); r->cfWrite(n,r); }
325
326/// Normalize and get denomerator
327static inline number n_GetDenom(number& n, const coeffs r)
328{ assume(r != NULL); assume(r->cfGetDenom!=NULL); return r->cfGetDenom(n, r); }
329
330/// Normalize and get numerator
331static inline number n_GetNumerator(number& n, const coeffs r)
332{ assume(r != NULL); assume(r->cfGetNumerator!=NULL); return r->cfGetNumerator(n, r); }
333
334static inline void   n_Power(number a, int b, number *res, const coeffs r)
335{ assume(r != NULL); assume(r->cfPower!=NULL); r->cfPower(a,b,res,r); }
336
337static inline number n_Mult(number a, number b, const coeffs r)
338{ assume(r != NULL); assume(r->cfMult!=NULL); return r->cfMult(a, b, r); }
339
340/// Inplace multiplication: a := a * b
341static inline void n_InpMult(number &a, number b, const coeffs r)
342{ assume(r != NULL); assume(r->cfInpMult!=NULL); r->cfInpMult(a,b,r); }
343
344static inline number n_Sub(number a, number b, const coeffs r)
345{ assume(r != NULL); assume(r->cfSub!=NULL); return r->cfSub(a, b, r); }
346
347static inline number n_Add(number a, number b, const coeffs r)
348{ assume(r != NULL); assume(r->cfAdd!=NULL); return r->cfAdd(a, b, r); }
349
350static inline number n_Div(number a, number b, const coeffs r)
351{ assume(r != NULL); assume(r->cfDiv!=NULL); return r->cfDiv(a,b,r); }
352
353static inline number n_IntDiv(number a, number b, const coeffs r)
354{ assume(r != NULL); assume(r->cfIntDiv!=NULL); return r->cfIntDiv(a,b,r); }
355
356static inline number n_ExactDiv(number a, number b, const coeffs r)
357{ assume(r != NULL); assume(r->cfExactDiv!=NULL); return r->cfExactDiv(a,b,r); }
358
359static inline number n_Gcd(number a, number b, const coeffs r)
360{ assume(r != NULL); assume(r->cfGcd!=NULL); return r->cfGcd(a,b,r); }
361
362static inline number n_Lcm(number a, number b, const coeffs r)
363{ assume(r != NULL); assume(r->cfLcm!=NULL); return r->cfLcm(a,b,r); }
364
365static inline nMapFunc n_SetMap(const coeffs src, const coeffs dst)
366{ assume(src != NULL && dst != NULL); assume(dst->cfSetMap!=NULL); return dst->cfSetMap(src,dst); }
367
368static inline number n_Par(int n, const coeffs r)
369{ assume(r != NULL); assume(r->cfPar!=NULL); return r->cfPar(n,r); }
370
371static inline int n_ParDeg(number n, const coeffs r)
372{ assume(r != NULL); assume(r->cfParDeg!=NULL); return r->cfParDeg(n,r); }
373
374/// Tests whether n is a correct number: only used if LDEBUG is defined
375static inline BOOLEAN n_DBTest(number n, const char *filename, const int linenumber, const coeffs r)
376{
377  assume(r != NULL); 
378#ifdef LDEBUG
379  assume(r->cfDBTest != NULL); 
380  return r->cfDBTest(n, filename, linenumber, r);
381#else
382  return TRUE;
383#endif
384}
385
386/// output the coeff description
387static inline void   n_CoeffWrite(const coeffs r)
388{ assume(r != NULL); assume(r->cfCoeffWrite != NULL); r->cfCoeffWrite(r); }
389
390// Tests:
391static inline BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
392{ assume(r != NULL); return (r->ringtype == 1); }
393
394static inline BOOLEAN nCoeff_is_Ring_ModN(const coeffs r)
395{ assume(r != NULL); return (r->ringtype == 2); }
396
397static inline BOOLEAN nCoeff_is_Ring_PtoM(const coeffs r)
398{ assume(r != NULL); return (r->ringtype == 3); }
399
400static inline BOOLEAN nCoeff_is_Ring_Z(const coeffs r)
401{ assume(r != NULL); return (r->ringtype == 4); }
402
403static inline BOOLEAN nCoeff_is_Ring(const coeffs r)
404{ assume(r != NULL); return (r->ringtype != 0); }
405
406/// returns TRUE, if r is not a field and r has no zero divisors (i.e is a domain)
407static inline BOOLEAN nCoeff_is_Domain(const coeffs r)
408{
409  assume(r != NULL); 
410#ifdef HAVE_RINGS
411  return (r->ringtype == 4 || r->ringtype == 0);
412#else
413  return TRUE;
414#endif
415}
416
417/// returns TRUE, if r is not a field and r has non-trivial units
418static inline BOOLEAN nCoeff_has_Units(const coeffs r)
419{ assume(r != NULL); return ((r->ringtype == 1) || (r->ringtype == 2) || (r->ringtype == 3)); }
420
421static inline BOOLEAN nCoeff_is_Zp(const coeffs r)
422{ assume(r != NULL); return getCoeffType(r)==n_Zp; }
423
424static inline BOOLEAN nCoeff_is_Zp(const coeffs r, int p)
425{ assume(r != NULL); return (getCoeffType(r)  && (r->ch == ABS(p))); }
426
427static inline BOOLEAN nCoeff_is_Q(const coeffs r)
428{ assume(r != NULL); return getCoeffType(r)==n_Q; }
429
430static inline BOOLEAN nCoeff_is_numeric(const coeffs r) /* R, long R, long C */
431{ assume(r != NULL);  return (getCoeffType(r)==n_R) || (getCoeffType(r)==n_long_R) || (getCoeffType(r)==n_long_C); }
432// (r->ringtype == 0) && (r->ch ==  -1); ??
433
434
435static inline BOOLEAN nCoeff_is_R(const coeffs r)
436{ assume(r != NULL); return getCoeffType(r)==n_R; }
437
438static inline BOOLEAN nCoeff_is_GF(const coeffs r)
439{ assume(r != NULL); return getCoeffType(r)==n_GF; }
440
441static inline BOOLEAN nCoeff_is_GF(const coeffs r, int q)
442{ assume(r != NULL); return (getCoeffType(r)==n_GF) && (r->ch == q); }
443
444static inline BOOLEAN nCoeff_is_Zp_a(const coeffs r)
445{
446  assume(r != NULL);
447  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch < -1);
448}
449
450static inline BOOLEAN nCoeff_is_Zp_a(const coeffs r, int p)
451{
452  assume(r != NULL);
453  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch < -1 )
454                            && (-(r->ch) == ABS(p));
455}
456
457static inline BOOLEAN nCoeff_is_Q_a(const coeffs r)
458{
459  assume(r != NULL);
460  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch == 0);
461}
462
463static inline BOOLEAN nCoeff_is_long_R(const coeffs r)
464{ assume(r != NULL); return getCoeffType(r)==n_long_R; }
465
466static inline BOOLEAN nCoeff_is_long_C(const coeffs r)
467{ assume(r != NULL); return getCoeffType(r)==n_long_C; }
468
469static inline BOOLEAN nCoeff_is_CF(const coeffs r)
470{ assume(r != NULL); return getCoeffType(r)==n_CF; }
471
472/// TRUE, if the computation of the inverse is fast (i.e. prefer leading coeff. 1 over content)
473static inline BOOLEAN nCoeff_has_simple_inverse(const coeffs r)
474{ assume(r != NULL); return r->has_simple_Inverse; }
475/* Z/2^n, Z/p, GF(p,n), R, long_R, long_C*/
476// /* { return (r->ch>1) || (r->ch== -1); } *//* Z/p, GF(p,n), R, long_R, long_C*/
477// #ifdef HAVE_RINGS
478// { return (r->ringtype > 0) || (r->ch>1) || ((r->ch== -1) && (r->float_len < 10)); } /* Z/2^n, Z/p, GF(p,n), R, long_R, long_C*/
479// #else
480// { return (r->ch>1) || ((r->ch== -1) && (r->float_len < 10)); } /* Z/p, GF(p,n), R, long_R, long_C*/
481// #endif
482
483
484
485/// TRUE if n_Delete/n_New are empty operations
486static inline BOOLEAN nCoeff_has_simple_Alloc(const coeffs r)
487{ assume(r != NULL); return r->has_simple_Alloc; }
488/* Z/p, GF(p,n), R, Ring_2toM: nCopy, nNew, nDelete are dummies*/
489// return (rField_is_Zp(r)
490//         || rField_is_GF(r)
491// #ifdef HAVE_RINGS
492//             || rField_is_Ring_2toM(r)
493// #endif
494//             || rField_is_R(r)); }
495
496
497static inline BOOLEAN nCoeff_is_Extension(const coeffs r)
498{ assume(r != NULL); return (nCoeff_is_Q_a(r)) || (nCoeff_is_Zp_a(r)); } /* Z/p(a) and Q(a)*/
499
500/// BOOLEAN n_Test(number a, const coeffs r)
501#define n_Test(a,r)  n_DBTest(a, __FILE__, __LINE__, r)
502
503// Missing wrappers for:
504// cfIntMod, cfRePart, cfImPart, cfRead, cfName, cfInit_bigint
505// HAVE_RINGS: cfDivComp, cfExtGcd... cfDivBy
506
507
508// Deprecated:
509static inline int n_GetChar(const coeffs r)
510{ assume(r != NULL); return nInternalChar(r); }
511
512#endif
513
Note: See TracBrowser for help on using the repository browser.