source: git/libpolys/coeffs/longrat.h @ 45cc512

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