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 | |
---|
20 | struct snumber; |
---|
21 | typedef 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 | |
---|
53 | struct 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 | |
---|
89 | LINLINE BOOLEAN nlEqual(number a, number b, const coeffs r); |
---|
90 | LINLINE number nlInit(long i, const coeffs r); |
---|
91 | number nlRInit (long i); |
---|
92 | LINLINE BOOLEAN nlIsOne(number a, const coeffs r); |
---|
93 | LINLINE BOOLEAN nlIsZero(number za, const coeffs r); |
---|
94 | LINLINE number nlCopy(number a, const coeffs r); |
---|
95 | LINLINE number nl_Copy(number a, const coeffs r); |
---|
96 | LINLINE void nlDelete(number *a, const coeffs r); |
---|
97 | LINLINE number nlNeg(number za, const coeffs r); |
---|
98 | LINLINE number nlAdd(number la, number li, const coeffs r); |
---|
99 | LINLINE number nlSub(number la, number li, const coeffs r); |
---|
100 | LINLINE number nlMult(number a, number b, const coeffs r); |
---|
101 | |
---|
102 | BOOLEAN nlInitChar(coeffs r, void*); |
---|
103 | |
---|
104 | number nlInit2 (int i, int j, const coeffs r); |
---|
105 | number 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 | |
---|
110 | number nlGcd(number a, number b, const coeffs r); |
---|
111 | number nlExtGcd(number a, number b, number *s, number *t, const coeffs); |
---|
112 | number nlLcm(number a, number b, const coeffs r); /*special routine !*/ |
---|
113 | BOOLEAN nlGreater(number a, number b, const coeffs r); |
---|
114 | BOOLEAN nlIsMOne(number a, const coeffs r); |
---|
115 | int nlInt(number &n, const coeffs r); |
---|
116 | number nlBigInt(number &n); |
---|
117 | |
---|
118 | #ifdef HAVE_RINGS |
---|
119 | number nlMapGMP(number from, const coeffs src, const coeffs dst); |
---|
120 | void nlGMP(number &i, number n, const coeffs r); |
---|
121 | #endif |
---|
122 | |
---|
123 | BOOLEAN nlGreaterZero(number za, const coeffs r); |
---|
124 | number nlInvers(number a, const coeffs r); |
---|
125 | void nlNormalize(number &x, const coeffs r); |
---|
126 | number nlDiv(number a, number b, const coeffs r); |
---|
127 | number nlExactDiv(number a, number b, const coeffs r); |
---|
128 | number nlIntDiv(number a, number b, const coeffs r); |
---|
129 | number nlIntMod(number a, number b, const coeffs r); |
---|
130 | void nlPower(number x, int exp, number *lu, const coeffs r); |
---|
131 | const char * nlRead (const char *s, number *a, const coeffs r); |
---|
132 | void nlWrite(number &a, const coeffs r); |
---|
133 | |
---|
134 | /// Map q \in QQ \to Zp |
---|
135 | number nlModP(number q, const coeffs Q, const coeffs Zp); |
---|
136 | |
---|
137 | int nlSize(number n, const coeffs r); |
---|
138 | |
---|
139 | static 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 | |
---|
163 | number nlGetDenom(number &n, const coeffs r); |
---|
164 | number nlGetNumerator(number &n, const coeffs r); |
---|
165 | void nlCoeffWrite(const coeffs r, BOOLEAN details); |
---|
166 | number nlChineseRemainder(number *x, number *q,int rl, const coeffs C); |
---|
167 | number nlFarey(number nN, number nP, const coeffs CF); |
---|
168 | |
---|
169 | #ifdef LDEBUG |
---|
170 | BOOLEAN nlDBTest(number a, const char *f, const int l); |
---|
171 | #endif |
---|
172 | extern number nlOne; |
---|
173 | |
---|
174 | nMapFunc nlSetMap(const coeffs src, const coeffs dst); |
---|
175 | |
---|
176 | extern 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 |
---|
183 | void nlInpGcd(number &a, number b, const coeffs r); |
---|
184 | void nlInpIntDiv(number &a, number b, const coeffs r); |
---|
185 | |
---|
186 | LINLINE void nlInpAdd(number &a, number b, const coeffs r); |
---|
187 | LINLINE void nlInpMult(number &a, number b, const coeffs r); |
---|
188 | |
---|
189 | #ifdef LDEBUG |
---|
190 | #define nlTest(a, r) nlDBTest(a,__FILE__,__LINE__, r) |
---|
191 | BOOLEAN 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 | |
---|