source: git/libpolys/coeffs/coeffs.h @ 529fa4

spielwiese
Last change on this file since 529fa4 was 529fa4, checked in by Hans Schoenemann <hannes@…>, 13 years ago
add: nChineseRemainder/idChineseRemainder
  • 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#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, BOOLEAN setChar, 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); }
283static inline BOOLEAN n_Greater(number a, number b, const coeffs r)
284{ assume(r != NULL); assume(r->cfGreater!=NULL); return r->cfGreater(a,b,r); }
285
286#ifdef HAVE_RINGS
287static inline BOOLEAN n_IsUnit(number n, const coeffs r)
288{ assume(r != NULL); assume(r->cfIsUnit!=NULL); return r->cfIsUnit(n,r); }
289
290static inline number n_GetUnit(number n, const coeffs r)
291{ assume(r != NULL); assume(r->cfGetUnit!=NULL); return r->cfGetUnit(n,r); }
292
293static inline BOOLEAN n_DivBy(number a, number b, const coeffs r)
294{ assume(r != NULL); assume(r->cfDivBy!=NULL); return r->cfDivBy(a,b,r); }
295#endif
296
297/// init with an integer
298static inline number n_Init(int i,       const coeffs r)
299{ assume(r != NULL); assume(r->cfInit!=NULL); return r->cfInit(i,r); }
300
301/// conversion to int; 0 if not possible
302static inline int n_Int(number n,        const coeffs r)
303{ assume(r != NULL); assume(r->cfInt!=NULL); return r->cfInt(n,r); }
304
305/// changes argument  inline: a:= -a
306static inline number n_Neg(number n,     const coeffs r)
307{ assume(r != NULL); assume(r->cfNeg!=NULL); return r->cfNeg(n,r); }
308
309/// return 1/a
310static inline number n_Invers(number a,  const coeffs r)
311{ assume(r != NULL); assume(r->cfInvers!=NULL); return r->cfInvers(a,r); }
312
313/// use for pivot strategies, (0) => 0, otherwise positive
314static inline int    n_Size(number n,    const coeffs r)
315{ assume(r != NULL); assume(r->cfSize!=NULL); return r->cfSize(n,r); }
316
317/// normalize the number. i.e. go to some canonnical representation (inplace)
318static inline void   n_Normalize(number& n, const coeffs r)
319{ assume(r != NULL); assume(r->cfNormalize!=NULL); r->cfNormalize(n,r); }
320
321/// Normalize and Write to the output buffer of reporter
322static inline void   n_Write(number& n,  const coeffs r)
323{ assume(r != NULL); assume(r->cfWrite!=NULL); r->cfWrite(n,r); }
324
325/// Normalize and get denomerator
326static inline number n_GetDenom(number& n, const coeffs r)
327{ assume(r != NULL); assume(r->cfGetDenom!=NULL); return r->cfGetDenom(n, r); }
328
329/// Normalize and get numerator
330static inline number n_GetNumerator(number& n, const coeffs r)
331{ assume(r != NULL); assume(r->cfGetNumerator!=NULL); return r->cfGetNumerator(n, r); }
332
333static inline void   n_Power(number a, int b, number *res, const coeffs r)
334{ assume(r != NULL); assume(r->cfPower!=NULL); r->cfPower(a,b,res,r); }
335
336static inline number n_Mult(number a, number b, const coeffs r)
337{ assume(r != NULL); assume(r->cfMult!=NULL); return r->cfMult(a, b, r); }
338
339/// Inplace multiplication: a := a * b
340static inline void n_InpMult(number &a, number b, const coeffs r)
341{ assume(r != NULL); assume(r->cfInpMult!=NULL); r->cfInpMult(a,b,r); }
342
343static inline number n_Sub(number a, number b, const coeffs r)
344{ assume(r != NULL); assume(r->cfSub!=NULL); return r->cfSub(a, b, r); }
345
346static inline number n_Add(number a, number b, const coeffs r)
347{ assume(r != NULL); assume(r->cfAdd!=NULL); return r->cfAdd(a, b, r); }
348
349static inline number n_Div(number a, number b, const coeffs r)
350{ assume(r != NULL); assume(r->cfDiv!=NULL); return r->cfDiv(a,b,r); }
351
352static inline number n_IntDiv(number a, number b, const coeffs r)
353{ assume(r != NULL); assume(r->cfIntDiv!=NULL); return r->cfIntDiv(a,b,r); }
354
355static inline number n_ExactDiv(number a, number b, const coeffs r)
356{ assume(r != NULL); assume(r->cfExactDiv!=NULL); return r->cfExactDiv(a,b,r); }
357
358static inline number n_Gcd(number a, number b, const coeffs r)
359{ assume(r != NULL); assume(r->cfGcd!=NULL); return r->cfGcd(a,b,r); }
360
361static inline number n_Lcm(number a, number b, const coeffs r)
362{ assume(r != NULL); assume(r->cfLcm!=NULL); return r->cfLcm(a,b,r); }
363
364static inline nMapFunc n_SetMap(const coeffs src, const coeffs dst)
365{ assume(src != NULL && dst != NULL); assume(dst->cfSetMap!=NULL); return dst->cfSetMap(src,dst); }
366
367static inline number n_Par(int n, const coeffs r)
368{ assume(r != NULL); assume(r->cfPar!=NULL); return r->cfPar(n,r); }
369
370static inline int n_ParDeg(number n, const coeffs r)
371{ assume(r != NULL); assume(r->cfParDeg!=NULL); return r->cfParDeg(n,r); }
372
373/// Tests whether n is a correct number: only used if LDEBUG is defined
374static inline BOOLEAN n_DBTest(number n, const char *filename, const int linenumber, const coeffs r)
375{
376  assume(r != NULL); 
377#ifdef LDEBUG
378  assume(r->cfDBTest != NULL); 
379  return r->cfDBTest(n, filename, linenumber, r);
380#else
381  return TRUE;
382#endif
383}
384
385/// output the coeff description
386static inline void   n_CoeffWrite(const coeffs r)
387{ assume(r != NULL); assume(r->cfCoeffWrite != NULL); r->cfCoeffWrite(r); }
388
389// Tests:
390static inline BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
391{ assume(r != NULL); return (r->ringtype == 1); }
392
393static inline BOOLEAN nCoeff_is_Ring_ModN(const coeffs r)
394{ assume(r != NULL); return (r->ringtype == 2); }
395
396static inline BOOLEAN nCoeff_is_Ring_PtoM(const coeffs r)
397{ assume(r != NULL); return (r->ringtype == 3); }
398
399static inline BOOLEAN nCoeff_is_Ring_Z(const coeffs r)
400{ assume(r != NULL); return (r->ringtype == 4); }
401
402static inline BOOLEAN nCoeff_is_Ring(const coeffs r)
403{ assume(r != NULL); return (r->ringtype != 0); }
404
405/// returns TRUE, if r is not a field and r has no zero divisors (i.e is a domain)
406static inline BOOLEAN nCoeff_is_Domain(const coeffs r)
407{
408  assume(r != NULL); 
409#ifdef HAVE_RINGS
410  return (r->ringtype == 4 || r->ringtype == 0);
411#else
412  return TRUE;
413#endif
414}
415
416/// returns TRUE, if r is not a field and r has non-trivial units
417static inline BOOLEAN nCoeff_has_Units(const coeffs r)
418{ assume(r != NULL); return ((r->ringtype == 1) || (r->ringtype == 2) || (r->ringtype == 3)); }
419
420static inline BOOLEAN nCoeff_is_Zp(const coeffs r)
421{ assume(r != NULL); return getCoeffType(r)==n_Zp; }
422
423static inline BOOLEAN nCoeff_is_Zp(const coeffs r, int p)
424{ assume(r != NULL); return (getCoeffType(r)  && (r->ch == ABS(p))); }
425
426static inline BOOLEAN nCoeff_is_Q(const coeffs r)
427{ assume(r != NULL); return getCoeffType(r)==n_Q; }
428
429static inline BOOLEAN nCoeff_is_numeric(const coeffs r) /* R, long R, long C */
430{ assume(r != NULL);  return (getCoeffType(r)==n_R) || (getCoeffType(r)==n_long_R) || (getCoeffType(r)==n_long_C); }
431// (r->ringtype == 0) && (r->ch ==  -1); ??
432
433
434static inline BOOLEAN nCoeff_is_R(const coeffs r)
435{ assume(r != NULL); return getCoeffType(r)==n_R; }
436
437static inline BOOLEAN nCoeff_is_GF(const coeffs r)
438{ assume(r != NULL); return getCoeffType(r)==n_GF; }
439
440static inline BOOLEAN nCoeff_is_GF(const coeffs r, int q)
441{ assume(r != NULL); return (getCoeffType(r)==n_GF) && (r->ch == q); }
442
443static inline BOOLEAN nCoeff_is_Zp_a(const coeffs r)
444{
445  assume(r != NULL);
446  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch < -1);
447}
448
449static inline BOOLEAN nCoeff_is_Zp_a(const coeffs r, int p)
450{
451  assume(r != NULL);
452  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch < -1 )
453                            && (-(r->ch) == ABS(p));
454}
455
456static inline BOOLEAN nCoeff_is_Q_a(const coeffs r)
457{
458  assume(r != NULL);
459  return (r->ringtype == 0) && (getCoeffType(r)==n_Ext) && (r->ch == 0);
460}
461
462static inline BOOLEAN nCoeff_is_long_R(const coeffs r)
463{ assume(r != NULL); return getCoeffType(r)==n_long_R; }
464
465static inline BOOLEAN nCoeff_is_long_C(const coeffs r)
466{ assume(r != NULL); return getCoeffType(r)==n_long_C; }
467
468static inline BOOLEAN nCoeff_is_CF(const coeffs r)
469{ assume(r != NULL); return getCoeffType(r)==n_CF; }
470
471/// TRUE, if the computation of the inverse is fast (i.e. prefer leading coeff. 1 over content)
472static inline BOOLEAN nCoeff_has_simple_inverse(const coeffs r)
473{ assume(r != NULL); return r->has_simple_Inverse; }
474/* Z/2^n, Z/p, GF(p,n), R, long_R, long_C*/
475// /* { return (r->ch>1) || (r->ch== -1); } *//* Z/p, GF(p,n), R, long_R, long_C*/
476// #ifdef HAVE_RINGS
477// { 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*/
478// #else
479// { return (r->ch>1) || ((r->ch== -1) && (r->float_len < 10)); } /* Z/p, GF(p,n), R, long_R, long_C*/
480// #endif
481
482
483
484/// TRUE if n_Delete/n_New are empty operations
485static inline BOOLEAN nCoeff_has_simple_Alloc(const coeffs r)
486{ assume(r != NULL); return r->has_simple_Alloc; }
487/* Z/p, GF(p,n), R, Ring_2toM: nCopy, nNew, nDelete are dummies*/
488// return (rField_is_Zp(r)
489//         || rField_is_GF(r)
490// #ifdef HAVE_RINGS
491//             || rField_is_Ring_2toM(r)
492// #endif
493//             || rField_is_R(r)); }
494
495
496static inline BOOLEAN nCoeff_is_Extension(const coeffs r)
497{ assume(r != NULL); return (nCoeff_is_Q_a(r)) || (nCoeff_is_Zp_a(r)); } /* Z/p(a) and Q(a)*/
498
499/// BOOLEAN n_Test(number a, const coeffs r)
500#define n_Test(a,r)  n_DBTest(a, __FILE__, __LINE__, r)
501
502// Missing wrappers for:
503// cfIntMod, cfRePart, cfImPart, cfRead, cfName, cfInit_bigint
504// HAVE_RINGS: cfDivComp, cfExtGcd... cfDivBy
505
506
507// Deprecated:
508static inline int n_GetChar(const coeffs r)
509{ assume(r != NULL); return nInternalChar(r); }
510
511#endif
512
Note: See TracBrowser for help on using the repository browser.