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

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