[35aab3] | 1 | #ifndef LONGRAT_H |
---|
| 2 | #define LONGRAT_H |
---|
| 3 | /**************************************** |
---|
| 4 | * Computer Algebra System SINGULAR * |
---|
| 5 | ****************************************/ |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT: computation with long rational numbers |
---|
| 8 | */ |
---|
[18cb65] | 9 | #include <misc/auxiliary.h> |
---|
| 10 | #include <omalloc/omalloc.h> |
---|
[2bc4f5] | 11 | |
---|
[9eb0f9] | 12 | #include <coeffs/si_gmp.h> |
---|
| 13 | #include <coeffs/coeffs.h> |
---|
[7d90aa] | 14 | |
---|
[8181f5] | 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 | |
---|
[ffde366] | 65 | #define SR_HDL(A) ((long)(A)) |
---|
| 66 | |
---|
[7447d8] | 67 | #define SR_INT 1L |
---|
[7d90aa] | 68 | #define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT)) |
---|
[35aab3] | 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 | |
---|
[7d90aa] | 84 | LINLINE BOOLEAN nlEqual(number a, number b, const coeffs r); |
---|
[2f3764] | 85 | LINLINE number nlInit(long i, const coeffs r); |
---|
[ea11dc] | 86 | number nlRInit (long i); |
---|
[7d90aa] | 87 | LINLINE BOOLEAN nlIsOne(number a, const coeffs r); |
---|
| 88 | LINLINE BOOLEAN nlIsZero(number za, const coeffs r); |
---|
[a642c64] | 89 | LINLINE number nlCopy(number a, const coeffs r); |
---|
[7d90aa] | 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); |
---|
[35aab3] | 96 | |
---|
[1cce47] | 97 | BOOLEAN nlInitChar(coeffs r, void*); |
---|
[51d835] | 98 | |
---|
[a642c64] | 99 | number nlInit2 (int i, int j, const coeffs r); |
---|
[a1ab2a] | 100 | number nlInit2gmp (mpz_t i, mpz_t j); |
---|
[d663f2] | 101 | |
---|
| 102 | // number nlInitMPZ(mpz_t m, const coeffs r); |
---|
| 103 | // void nlMPZ(mpz_t m, number &n, const coeffs r); |
---|
| 104 | |
---|
[7d90aa] | 105 | number nlGcd(number a, number b, const coeffs r); |
---|
[3216ec] | 106 | number nlExtGcd(number a, number b, number *s, number *t, const coeffs); |
---|
[7d90aa] | 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); |
---|
[7f7573] | 111 | number nlBigInt(number &n); |
---|
[f1c465f] | 112 | |
---|
[894f5b1] | 113 | #ifdef HAVE_RINGS |
---|
[6cc7f5] | 114 | number nlMapGMP(number from, const coeffs src, const coeffs dst); |
---|
[4d1ae5] | 115 | void nlGMP(number &i, number n, const coeffs r); |
---|
[894f5b1] | 116 | #endif |
---|
[f1c465f] | 117 | |
---|
[7d90aa] | 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); |
---|
[79020f] | 128 | |
---|
| 129 | /// Map q \in QQ \to Zp |
---|
| 130 | number nlModP(number q, const coeffs Q, const coeffs Zp); |
---|
| 131 | |
---|
[7d90aa] | 132 | int nlSize(number n, const coeffs r); |
---|
| 133 | number nlGetDenom(number &n, const coeffs r); |
---|
| 134 | number nlGetNumerator(number &n, const coeffs r); |
---|
[03f7b5] | 135 | void nlCoeffWrite(const coeffs r, BOOLEAN details); |
---|
[e8c8d5] | 136 | number nlChineseRemainder(number *x, number *q,int rl, const coeffs C); |
---|
| 137 | number nlFarey(number nN, number nP, const coeffs CF); |
---|
[045efb] | 138 | |
---|
[35aab3] | 139 | #ifdef LDEBUG |
---|
[85e68dd] | 140 | BOOLEAN nlDBTest(number a, const char *f, const int l); |
---|
[35aab3] | 141 | #endif |
---|
| 142 | extern number nlOne; |
---|
| 143 | |
---|
[7d90aa] | 144 | nMapFunc nlSetMap(const coeffs src, const coeffs dst); |
---|
[35aab3] | 145 | |
---|
[f9201c] | 146 | extern omBin rnumber_bin; |
---|
[7d90aa] | 147 | |
---|
| 148 | #define FREE_RNUMBER(x) omFreeBin((void *)x, rnumber_bin) |
---|
[896561] | 149 | #define ALLOC_RNUMBER() (number)omAllocBin(rnumber_bin) |
---|
| 150 | #define ALLOC0_RNUMBER() (number)omAlloc0Bin(rnumber_bin) |
---|
[35aab3] | 151 | |
---|
| 152 | // in-place operations |
---|
[7d90aa] | 153 | void nlInpGcd(number &a, number b, const coeffs r); |
---|
| 154 | void nlInpIntDiv(number &a, number b, const coeffs r); |
---|
[9f1a52] | 155 | |
---|
| 156 | LINLINE void nlInpAdd(number &a, number b, const coeffs r); |
---|
[7d90aa] | 157 | LINLINE void nlInpMult(number &a, number b, const coeffs r); |
---|
[35aab3] | 158 | |
---|
[aaf0b6] | 159 | #ifdef LDEBUG |
---|
[2b957a] | 160 | #define nlTest(a, r) nlDBTest(a,__FILE__,__LINE__, r) |
---|
| 161 | BOOLEAN nlDBTest(number a, char *f,int l, const coeffs r); |
---|
[aaf0b6] | 162 | #else |
---|
[d101b1] | 163 | #define nlTest(a, r) do {} while (0) |
---|
[aaf0b6] | 164 | #endif |
---|
| 165 | |
---|
[35aab3] | 166 | #endif |
---|
| 167 | |
---|
| 168 | |
---|