source: git/libpolys/coeffs/coeffs.h @ 4c6e420

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