source: git/libpolys/coeffs/longrat.h @ f52880

spielwiese
Last change on this file since f52880 was f52880, checked in by Janko Boehm <boehm@…>, 10 years ago
Added missing include for debug version
  • Property mode set to 100644
File size: 6.1 KB
Line 
1#ifndef LONGRAT_H
2#define LONGRAT_H
3/****************************************
4*  Computer Algebra System SINGULAR     *
5****************************************/
6/*
7* ABSTRACT: computation with long rational numbers
8*/
9#include <misc/auxiliary.h>
10#include <omalloc/omalloc.h>
11#include <reporter/reporter.h>
12
13#include <coeffs/si_gmp.h>
14#include <coeffs/coeffs.h>
15
16#ifndef NDEBUG
17#include <polys/monomials/ring.h>
18#endif
19
20struct snumber;
21typedef struct snumber  *number;
22
23/*-----------------------------------------------------------------*/
24/**
25**  'SR_INT' is the type of those integers small enough to fit into  29  bits.
26**  Therefor the value range of this small integers is: $-2^{28}...2^{28}-1$.
27**
28**  Small integers are represented by an immediate integer handle, containing
29**  the value instead of pointing  to  it,  which  has  the  following  form:
30**
31**      +-------+-------+-------+-------+- - - -+-------+-------+-------+
32**      | guard | sign  | bit   | bit   |       | bit   | tag   | tag   |
33**      | bit   | bit   | 27    | 26    |       | 0     | 0     | 1     |
34**      +-------+-------+-------+-------+- - - -+-------+-------+-------+
35**
36**  Immediate integers handles carry the tag 'SR_INT', i.e. the last bit is 1.
37**  This distuingishes immediate integers from other handles which  point  to
38**  structures aligned on 4 byte boundaries and therefor have last bit  zero.
39**  (The second bit is reserved as tag to allow extensions of  this  scheme.)
40**  Using immediates as pointers and dereferencing them gives address errors.
41**
42**  To aid overflow check the most significant two bits must always be equal,
43**  that is to say that the sign bit of immediate integers has a  guard  bit.
44**
45**  The macros 'INT_TO_SR' and 'SR_TO_INT' should be used to convert  between
46**  a small integer value and its representation as immediate integer handle.
47**
48**  Large integers and rationals are represented by z and n
49**  where n may be undefined (if s==3)
50**  NULL represents only deleted values
51*/
52
53struct snumber
54{
55  mpz_t z; //< Zaehler
56  mpz_t n; //< Nenner
57#if defined(LDEBUG)
58  int debug;
59#endif
60
61  /**
62   * parameter s in number:
63   * 0 (or FALSE): not normalised rational
64   * 1 (or TRUE):  normalised rational
65   * 3          :  integer with n==NULL
66   **/
67  BOOLEAN s; //< integer parameter
68};
69
70#define SR_HDL(A) ((long)(A))
71
72#define SR_INT    1L
73#define INT_TO_SR(INT)  ((number) (((long)INT << 2) + SR_INT))
74#define SR_TO_INT(SR)   (((long)SR) >> 2)
75
76#define MP_SMALL 1
77
78
79
80// allow inlining only from p_Numbers.h and if ! LDEBUG
81
82#if defined(DO_LINLINE) && defined(P_NUMBERS_H) && !defined(LDEBUG)
83#define LINLINE static inline
84#else
85#define LINLINE
86#undef DO_LINLINE
87#endif // DO_LINLINE
88
89LINLINE BOOLEAN  nlEqual(number a, number b, const coeffs r);
90LINLINE number   nlInit(long i, const coeffs r);
91number nlRInit (long i);
92LINLINE BOOLEAN  nlIsOne(number a, const coeffs r);
93LINLINE BOOLEAN  nlIsZero(number za, const coeffs r);
94LINLINE number   nlCopy(number a, const coeffs r);
95LINLINE number   nl_Copy(number a, const coeffs r);
96LINLINE void     nlDelete(number *a, const coeffs r);
97LINLINE number   nlNeg(number za, const coeffs r);
98LINLINE number   nlAdd(number la, number li, const coeffs r);
99LINLINE number   nlSub(number la, number li, const coeffs r);
100LINLINE number   nlMult(number a, number b, const coeffs r);
101
102BOOLEAN nlInitChar(coeffs r, void*);
103
104number   nlInit2 (int i, int j, const coeffs r);
105number   nlInit2gmp (mpz_t i, mpz_t j);
106
107// number nlInitMPZ(mpz_t m, const coeffs r);
108// void nlMPZ(mpz_t m, number &n, const coeffs r);
109
110number   nlGcd(number a, number b, const coeffs r);
111number nlExtGcd(number a, number b, number *s, number *t, const coeffs);
112number   nlLcm(number a, number b, const coeffs r);   /*special routine !*/
113BOOLEAN  nlGreater(number a, number b, const coeffs r);
114BOOLEAN  nlIsMOne(number a, const coeffs r);
115int      nlInt(number &n, const coeffs r);
116number   nlBigInt(number &n);
117
118#ifdef HAVE_RINGS
119number nlMapGMP(number from, const coeffs src, const coeffs dst);
120void     nlGMP(number &i, number n, const coeffs r);
121#endif
122
123BOOLEAN  nlGreaterZero(number za, const coeffs r);
124number   nlInvers(number a, const coeffs r);
125void     nlNormalize(number &x, const coeffs r);
126number   nlDiv(number a, number b, const coeffs r);
127number   nlExactDiv(number a, number b, const coeffs r);
128number   nlIntDiv(number a, number b, const coeffs r);
129number   nlIntMod(number a, number b, const coeffs r);
130void     nlPower(number x, int exp, number *lu, const coeffs r);
131const char *   nlRead (const char *s, number *a, const coeffs r);
132void     nlWrite(number &a, const coeffs r);
133
134/// Map q \in QQ \to Zp
135number nlModP(number q, const coeffs Q, const coeffs Zp);
136
137int      nlSize(number n, const coeffs r);
138
139static inline int nlQlogSize (number n, const coeffs r)
140{
141  assume( nCoeff_is_Q (r) );
142
143  long nl=nlSize(n,r);
144  if (nl==0L) return 0;
145  if (nl==1L)
146  {
147    long i = SR_TO_INT (n);
148    unsigned long v;
149    v = (i >= 0) ? i : -i;
150    int r = 0;
151
152    while(v >>= 1)
153    {
154      r++;
155    }
156    return r + 1;
157  }
158  //assume denominator is 0
159  number nn=(number) n;
160  return mpz_sizeinbase (nn->z, 2);
161}
162
163number   nlGetDenom(number &n, const coeffs r);
164number   nlGetNumerator(number &n, const coeffs r);
165void     nlCoeffWrite(const coeffs r, BOOLEAN details);
166number   nlChineseRemainder(number *x, number *q,int rl, const coeffs C);
167number   nlFarey(number nN, number nP, const coeffs CF);
168
169#ifdef LDEBUG
170BOOLEAN  nlDBTest(number a, const char *f, const int l);
171#endif
172extern number nlOne;
173
174nMapFunc nlSetMap(const coeffs src, const coeffs dst);
175
176extern omBin rnumber_bin;
177
178#define FREE_RNUMBER(x) omFreeBin((void *)x, rnumber_bin)
179#define ALLOC_RNUMBER() (number)omAllocBin(rnumber_bin)
180#define ALLOC0_RNUMBER() (number)omAlloc0Bin(rnumber_bin)
181
182// in-place operations
183void nlInpGcd(number &a, number b, const coeffs r);
184void nlInpIntDiv(number &a, number b, const coeffs r);
185
186LINLINE void nlInpAdd(number &a, number b, const coeffs r);
187LINLINE void nlInpMult(number &a, number b, const coeffs r);
188
189#ifdef LDEBUG
190#define nlTest(a, r) nlDBTest(a,__FILE__,__LINE__, r)
191BOOLEAN nlDBTest(number a, char *f,int l, const coeffs r);
192#else
193#define nlTest(a, r) do {} while (0)
194#endif
195
196#endif
197
198
Note: See TracBrowser for help on using the repository browser.