8#if SI_INTEGER_VARIANT == 3
9#define POW_2_28 (1L<<60)
17#define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL)
24static inline number nrz_short(number
x)
32 mpz_clear((mpz_ptr)
x);
41 long ui=mpz_get_si((mpz_ptr)
x);
42 if ((((ui<<3)>>3)==ui)
43 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
45 mpz_clear((mpz_ptr)
x);
64 if (n_Z_IS_SMALL(a))
return 1;
65 return ((mpz_ptr)a)->_mp_alloc;
74number _nrzMult(number, number,
const coeffs);
75number nrzMult(number a, number
b,
const coeffs R)
81 number c = _nrzMult(a,
b,
R);
89number _nrzMult (number a, number
b,
const coeffs R)
91number nrzMult (number a, number
b,
const coeffs R)
94 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
99 long r=(long)((
unsigned long)(
SR_HDL(a)-1L))*((
unsigned long)(
SR_HDL(
b)>>1));
102 number u=((number) ((r>>1)+
SR_INT));
109 nrzTest((number)erg);
110 return nrz_short((number) erg);
112 else if (n_Z_IS_SMALL(a))
116 mpz_init_set(erg, (mpz_ptr)
b);
118 return nrz_short((number) erg);
120 else if (n_Z_IS_SMALL(
b))
124 mpz_init_set(erg, (mpz_ptr) a);
126 return nrz_short((number) erg);
132 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr)
b);
133 nrzTest((number)erg);
134 return nrz_short((number) erg);
138static long int_gcd(
long a,
long b)
159static number nrzLcm (number a, number
b,
const coeffs R)
165 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
175 mpz_init_set(erg, (mpz_ptr)
b);
179 else if (n_Z_IS_SMALL(
b))
181 mpz_init_set(erg, (mpz_ptr) a);
188 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr)
b);
191 nrzTest((number)erg);
195static number nrzCopy(number a,
const coeffs)
197 if (n_Z_IS_SMALL(a))
return a;
199 mpz_init_set(erg, (mpz_ptr) a);
200 nrzTest((number)erg);
208static number nrzGcd (number a,number
b,
const coeffs R)
210 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
215 else if (n_Z_IS_SMALL(a))
218 return nrzCopy(
b,
R);
222 else if (n_Z_IS_SMALL(
b))
225 return nrzCopy(a,
R);
233 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr)
b);
234 nrzTest((number)erg);
243static long int_extgcd(
long a,
long b,
long * u,
long*
x,
long *
v,
long*
y)
286static number nrzExtGcd (number a, number
b, number *
s, number *t,
const coeffs)
288 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
305 mpz_init_set(aa, (mpz_ptr) a);
313 mpz_init_set(bb, (mpz_ptr)
b);
321 mpz_gcdext(erg, bs, bt, aa, bb);
322 *
s = nrz_short((number) bs);
323 *t = nrz_short((number) bt);
326 return nrz_short((number) erg);
330static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
331static number nrzXExtGcd(number a, number
b, number *
x, number *
y, number * u, number *
v,
const coeffs R)
338 number c = _nrzXExtGcd(a,
b,
x,
y, u,
v,
R);
354static number _nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
356static number nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
359 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
378 mpz_init_set(aa, (mpz_ptr) a);
386 mpz_init_set(bb, (mpz_ptr)
b);
395 mpz_gcdext(erg, bs, bt, aa, bb);
400 mpz_init_set(bu, (mpz_ptr) bb);
401 mpz_init_set(bv, (mpz_ptr) aa);
405 assume(mpz_cmp_si(erg, 0));
407 mpz_div(bu, bu, erg);
408 mpz_div(bv, bv, erg);
411 *u = nrz_short((number) bu);
412 *
v = nrz_short((number) bv);
414 *
s = nrz_short((number) bs);
415 *t = nrz_short((number) bt);
416 return nrz_short((number) erg);
420static number _nrzQuotRem(number, number, number *,
const coeffs);
421static number nrzQuotRem(number a, number
b, number * r,
const coeffs R)
427 number c = _nrzQuotRem(a,
b, r,
R);
440static number _nrzQuotRem (number a, number
b, number * r,
const coeffs )
442static number nrzQuotRem (number a, number
b, number * r,
const coeffs )
460 gsign = mpz_sgn((mpz_ptr)
b);
461 mpz_init_set(bb,(mpz_ptr)
b);
469 mpz_init_set(aa,(mpz_ptr) a);
475 mpz_fdiv_qr(qq, rr, aa, gg);
476 mpz_tdiv_q_2exp(ghalf, gg, 1);
477 if (mpz_cmp(rr, ghalf) > 0)
480 mpz_add_ui(qq, qq, 1);
482 if (gsign < 0) mpz_neg(qq, qq);
495 *r=nrz_short((number)rr);
497 return nrz_short((number)qq);
500static void nrzPower (number a,
int i, number *
result,
const coeffs)
508 mpz_init_set(aa, (mpz_ptr) a);
509 mpz_pow_ui(erg, aa,
i);
510 *
result = nrz_short((number) erg);
520 mpz_init_set_si(erg,
i);
521 return nrz_short((number) erg);
524static number nrzInitMPZ(mpz_t
m,
const coeffs)
527 mpz_init_set(erg,
m);
528 return nrz_short((number) erg);
534 if (*a ==
NULL)
return;
535 if (n_Z_IS_SMALL(*a)==0)
537 mpz_clear((mpz_ptr) *a);
546static long nrzInt(number &n,
const coeffs)
548 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
549 return mpz_get_si( (mpz_ptr)n);
552static number _nrzAdd(number, number,
const coeffs);
553static number nrzAdd(number a, number
b,
const coeffs R)
559 number c = _nrzAdd(a,
b,
R);
567static number _nrzAdd (number a, number
b,
const coeffs )
569static number nrzAdd (number a, number
b,
const coeffs )
572 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
580 mpz_add_ui(erg, erg, (
unsigned long)
SR_TO_INT(
b));
582 mpz_sub_ui(erg, erg, (
unsigned long)-(
SR_TO_INT(
b)));
583 return nrz_short((number)erg);
585 else if (n_Z_IS_SMALL(a))
590 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)
SR_TO_INT(a));
592 mpz_sub_ui(erg, (mpz_ptr)
b, (
unsigned long)-(
SR_TO_INT(a)));
593 return nrz_short((number) erg);
595 else if (n_Z_IS_SMALL(
b))
600 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
602 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(
b)));
603 return nrz_short((number) erg);
609 mpz_add(erg, (mpz_ptr) a, (mpz_ptr)
b);
610 return nrz_short((number) erg);
614static number nrzSub (number a, number
b,
const coeffs )
616 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
624 mpz_sub_ui(erg, erg, (
unsigned long)
SR_TO_INT(
b));
626 mpz_add_ui(erg, erg, (
unsigned long)-(
SR_TO_INT(
b)));
627 return nrz_short((number)erg);
629 else if (n_Z_IS_SMALL(a))
633 mpz_sub(erg, erg, (mpz_ptr)
b);
634 return nrz_short((number) erg);
636 else if (n_Z_IS_SMALL(
b))
641 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
643 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(
b));
644 return nrz_short((number) erg);
650 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr)
b);
651 return nrz_short((number) erg);
657 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
658 return ((
long)a)>((long)
b);
659 else if (n_Z_IS_SMALL(a))
660 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
661 else if (n_Z_IS_SMALL(
b))
662 return 0 < mpz_cmp_si((mpz_ptr)a,
SR_TO_INT(
b));
663 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
671static number nrzGetUnit (number n,
const coeffs r)
673 if (nrzGreaterZero(n, r))
679static number nrzAnn(number n,
const coeffs)
709 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
711 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(
b))
714 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
719 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
723 else if (n_Z_IS_SMALL(a))
727 else if (n_Z_IS_SMALL(
b))
729 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(
b))) != 0;
732 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr)
b) != 0;
735static int nrzDivComp(number a, number
b,
const coeffs r)
737 if (nrzDivBy(a,
b, r))
739 if (nrzDivBy(
b, a, r))
return 2;
742 if (nrzDivBy(
b, a, r))
return 1;
746static number nrzDiv (number a,number
b,
const coeffs cf)
753 else if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
769 else if (n_Z_IS_SMALL(a))
778 else if (n_Z_IS_SMALL(
b))
784 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)))) {
791 return nrz_short((number) erg);
797 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
819 return nrz_short((number) erg);
822static number nrzExactDiv (number a,number
b,
const coeffs cf)
831 else if (n_Z_IS_SMALL(a))
834 mpz_init_set(aa, (mpz_ptr) a);
838 mpz_init_set(bb, (mpz_ptr)
b);
841 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
844 return nrz_short((number) erg);
847static number nrzIntMod (number a,number
b,
const coeffs)
854 mpz_init_set(aa, (mpz_ptr) a);
858 mpz_init_set(bb, (mpz_ptr)
b);
864 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
869 return nrz_short((number) r);
872static number nrzInvers (number c,
const coeffs r)
874 if (!nrzIsUnit((number) c, r))
876 WerrorS(
"Non invertible element.");
882static number nrzNeg (number c,
const coeffs)
891static number nrzFarey(number r, number
N,
const coeffs R)
893 number a0 = nrzCopy(
N,
R);
895 number a1 = nrzCopy(r,
R);
899 PrintS(
"Farey start with ");
907 number as = nrzMult(a1, a1,
R);
909 if (nrzGreater(
N, as,
R))
915 number q = nrzDiv(a0, a1,
R);
916 number t = nrzMult(a1, q,
R),
917 s = nrzSub(a0, t,
R);
923 t = nrzMult(b1, q,
R);
924 s = nrzSub(b0, t,
R);
931 number as = nrzMult(b1, b1,
R);
934 if (nrzGreater(as,
N,
R))
963static number nrzMapMachineInt(number from,
const coeffs ,
const coeffs )
966 mpz_init_set_ui(erg, (
unsigned long) from);
967 return nrz_short((number) erg);
970static number nrzMapZp(number from,
const coeffs ,
const coeffs )
973 mpz_init_set_si(erg, (
long) from);
974 return nrz_short((number) erg);
977static number nrzModNMap(number from,
const coeffs ,
const coeffs )
980 mpz_init_set(erg, (mpz_ptr) from);
981 return nrz_short((number) erg);
984static number nrzMapQ(number from,
const coeffs ,
const coeffs dst)
990 WerrorS(
"rational in map to integer");
994 mpz_init_set(erg, from->z);
995 return nrz_short((number) erg);
1014 return nrzMapMachineInt;
1035 long ui=mpz_get_si((mpz_ptr)
x);
1036 if ((((ui<<3)>>3)==ui)
1037 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
1039 Print(
"gmp-small %s:%d\n",
f,
l);
1056 if (n_Z_IS_SMALL(a))
1062 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1064 z=mpz_get_str(
s,10,(mpz_ptr) a);
1074static const char * nlEatLongC(
char *
s, mpz_ptr
i)
1076 const char * start=
s;
1078 if (*s<'0' || *s>
'9')
1083 while (*
s >=
'0' && *
s <=
'9')
s++;
1086 mpz_set_str(
i,start,10);
1092 mpz_set_str(
i,start,10);
1098static const char * nrzRead (
const char *
s, number *a,
const coeffs)
1103 s = nlEatLongC((
char *)
s, z);
1105 *a = nrz_short((number) z);
1114 if ( n_Z_IS_SMALL(n))
1121 mpz_init_set( dummy,n->z );
1137 WerrorS(
"rational in conversion to integer");
1142 return nrz_short((number)z);
1146static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1148 if (n_Z_IS_SMALL(a))
1151 mpz_init_set(
res, (mpz_ptr) a);
1154static number nrzEucNorm (number a,
const coeffs )
1156 if (n_Z_IS_SMALL(a))
1165 mpz_abs(
abs, (mpz_ptr)a);
1166 nrzTest((number)
abs);
1167 return (number)
abs;
1176 long ch = r->cfInt(c, r);
1177 mpz_init_set_ui(dummy, ch);
1181 mpz_init_set(dummy, (mpz_ptr)c);
1185 info.exp = (
unsigned long) 1;
1205 r->cfMult = nrzMult;
1209 r->cfIntMod= nrzIntMod;
1210 r->cfExactDiv= nrzExactDiv;
1212 r->cfInitMPZ = nrzInitMPZ;
1216 r->cfDivComp = nrzDivComp;
1217 r->cfIsUnit = nrzIsUnit;
1218 r->cfGetUnit = nrzGetUnit;
1220 r->cfExtGcd = nrzExtGcd;
1221 r->cfXExtGcd = nrzXExtGcd;
1222 r->cfEucNorm = nrzEucNorm;
1223 r->cfQuotRem = nrzQuotRem;
1224 r->cfDivBy = nrzDivBy;
1226 r->cfInpNeg = nrzNeg;
1227 r->cfInvers= nrzInvers;
1228 r->cfCopy = nrzCopy;
1230 r->cfRead = nrzRead;
1231 r->cfGreater = nrzGreater;
1232 r->cfEqual = nrzEqual;
1233 r->cfIsZero = nrzIsZero;
1234 r->cfIsOne = nrzIsOne;
1235 r->cfIsMOne = nrzIsMOne;
1236 r->cfGreaterZero = nrzGreaterZero;
1237 r->cfPower = nrzPower;
1241 r->cfSetMap = nrzSetMap;
1242 r->convSingNFactoryN = nrzConvSingNFactoryN;
1243 r->convFactoryNSingN = nrzConvFactoryNSingN;
1245 r->cfFarey = nrzFarey;
1249 r->cfQuot1 = nrzQuot1;
1255 r->cfDBTest=nrzDBTest;
1259 r->has_simple_Alloc=
FALSE;
1260 r->has_simple_Inverse=
FALSE;
Rational abs(const Rational &a)
const CanonicalForm CFMap CFMap & N
static FORCE_INLINE BOOLEAN nCoeff_is_Z(const coeffs r)
number ndCopyMap(number a, const coeffs src, const coeffs dst)
@ n_Q
rational (GMP) numbers
@ n_Zn
only used if HAVE_RINGS is defined
@ n_Z
only used if HAVE_RINGS is defined
void n_Print(number &a, const coeffs r)
print a number (BEWARE of string buffers!) mostly for debugging
static FORCE_INLINE nMapFunc n_SetMap(const coeffs src, const coeffs dst)
set the mapping function pointers for translating numbers from src to dst
static FORCE_INLINE number n_Div(number a, number b, const coeffs r)
return the quotient of 'a' and 'b', i.e., a/b; raises an error if 'b' is not invertible in r exceptio...
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
static FORCE_INLINE void n_InpMult(number &a, number b, const coeffs r)
multiplication of 'a' and 'b'; replacement of 'a' by the product a*b
@ n_rep_gap_rat
(number), see longrat.h
@ n_rep_gap_gmp
(), see rinteger.h, new impl.
@ n_rep_int
(int), see modulop.h
@ n_rep_gmp
(mpz_ptr), see rmodulon,h
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
void nKillChar(coeffs r)
undo all initialisations
const CanonicalForm int s
const CanonicalForm int const CFList const Variable & y
const Variable & v
< [in] a sqrfree bivariate poly
void WerrorS(const char *s)
void nlWriteFd(number n, const ssiInfo *d, const coeffs)
void mpz_mul_si(mpz_ptr r, mpz_srcptr s, long int si)
number nlChineseRemainderSym(number *x, number *q, int rl, BOOLEAN sym, CFArray &inv_cache, const coeffs CF)
number nlReadFd(const ssiInfo *d, const coeffs)
static int int_extgcd(int a, int b, int *u, int *x, int *v, int *y)
The main handler for Singular numbers which are suitable for Singular polynomials.
const char *const nDivBy0
#define omFreeSize(addr, size)
#define omFreeBin(addr, bin)
void StringSetS(const char *st)
void StringAppendS(const char *st)
void PrintS(const char *s)
static char * nrzCoeffName(const coeffs)
void nrzWrite(number a, const coeffs r)
void nrzDelete(number *a, const coeffs)
int nrzSize(number a, const coeffs)
BOOLEAN nrzInitChar(coeffs r, void *parameter)
number nrzInit(long i, const coeffs r)
CanonicalForm make_cf(const mpz_ptr n)
void gmp_numerator(const CanonicalForm &f, mpz_ptr result)