[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 | */ |
---|
[f5f5c9] | 9 | #include "misc/auxiliary.h" |
---|
[2bc4f5] | 10 | |
---|
[f5f5c9] | 11 | #include "coeffs/si_gmp.h" |
---|
| 12 | #include "coeffs/coeffs.h" |
---|
[40094f] | 13 | #include "factory/si_log2.h" |
---|
[7d90aa] | 14 | |
---|
[c087b3] | 15 | number nlGetDenom(number &n, const coeffs r); /*for SAGE,, better: n_GetDenom */ |
---|
| 16 | number nlGetNumerator(number &n, const coeffs r); /*for SAGE, better: n_GetNumerator*/ |
---|
[d31cb91] | 17 | |
---|
[8181f5] | 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 |
---|
[1b2761d] | 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 | |
---|
[2206753] | 73 | BOOLEAN nlInitChar(coeffs, void*); |
---|
[35aab3] | 74 | |
---|
[2206753] | 75 | /// only used by slimgb (tgb.cc) |
---|
[fc2acf] | 76 | static FORCE_INLINE int nlQlogSize (number n, const coeffs r) |
---|
[caf5880] | 77 | { |
---|
[a0dfbc] | 78 | assume( nCoeff_is_Q (r) ); |
---|
[caf5880] | 79 | |
---|
[798f685] | 80 | if(SR_HDL(n)&SR_INT) |
---|
[caf5880] | 81 | { |
---|
[798f685] | 82 | if (SR_HDL(n)==SR_INT) return 0; |
---|
[caf5880] | 83 | long i = SR_TO_INT (n); |
---|
| 84 | unsigned long v; |
---|
[798f685] | 85 | v = ABS(i); |
---|
[40094f] | 86 | return SI_LOG2_LONG(v) + 1; |
---|
[caf5880] | 87 | } |
---|
| 88 | //assume denominator is 0 |
---|
| 89 | number nn=(number) n; |
---|
| 90 | return mpz_sizeinbase (nn->z, 2); |
---|
| 91 | } |
---|
| 92 | |
---|
[7d90aa] | 93 | |
---|
[fc2acf] | 94 | static FORCE_INLINE BOOLEAN nlIsInteger(number q, const coeffs r) |
---|
| 95 | { |
---|
| 96 | assume( nCoeff_is_Q (r) ); |
---|
| 97 | n_Test(q, r); |
---|
[e9478b] | 98 | |
---|
[fc2acf] | 99 | if (SR_HDL(q) & SR_INT) |
---|
[166e733] | 100 | return TRUE; // immediate int |
---|
[e9478b] | 101 | |
---|
[3f2942] | 102 | return ( q->s == 3 ); |
---|
[fc2acf] | 103 | } |
---|
| 104 | |
---|
[b0e0160] | 105 | void nlMPZ(mpz_t m, number &n, const coeffs r); |
---|
[2206753] | 106 | number nlModP(number q, const coeffs Q, const coeffs Zp); |
---|
| 107 | void nlNormalize(number &x, const coeffs r); |
---|
| 108 | void nlInpGcd(number &a, number b, const coeffs r); |
---|
[b412eeb] | 109 | void nlDelete(number *a, const coeffs r); /*for SAGE,, better: n_Delete */ |
---|
| 110 | |
---|
[35aab3] | 111 | |
---|
[e38553] | 112 | |
---|
| 113 | /// create a rational i/j (implicitly) over Q |
---|
| 114 | /// NOTE: make sure to use correct Q in debug mode |
---|
| 115 | number nlInit2 (int i, int j, const coeffs r); |
---|
| 116 | |
---|
| 117 | /// create a rational i/j (implicitly) over Q |
---|
| 118 | /// NOTE: make sure to use correct Q in debug mode |
---|
| 119 | number nlInit2gmp (mpz_t i, mpz_t j, const coeffs r); |
---|
| 120 | |
---|
[1950d7] | 121 | #if 0 // substituted by nlMPZ |
---|
| 122 | void nlGMP(number &i, mpz_t n, const coeffs r); |
---|
| 123 | #endif |
---|
[8887b4] | 124 | // for ring similiar to Q/Z (char 0 required): |
---|
[608ba4] | 125 | number nlChineseRemainderSym(number *x, number *q,int rl, BOOLEAN sym, CFArray &inv_cache,const coeffs CF); |
---|
[8887b4] | 126 | |
---|
[aaf0b6] | 127 | |
---|
[35aab3] | 128 | #endif |
---|
| 129 | |
---|
| 130 | |
---|