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