source: git/libpolys/coeffs/coeffs.h @ fba6f18

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