7#if SI_INTEGER_VARIANT == 3
8#define POW_2_28 (1L<<60)
16#define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL)
23static inline number nrz_short(number
x)
31 mpz_clear((mpz_ptr)
x);
40 long ui=mpz_get_si((mpz_ptr)
x);
41 if ((((ui<<3)>>3)==ui)
42 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
44 mpz_clear((mpz_ptr)
x);
63 if (n_Z_IS_SMALL(a))
return 1;
64 return ((mpz_ptr)a)->_mp_alloc;
73number _nrzMult(number, number,
const coeffs);
74number nrzMult(number a, number
b,
const coeffs R)
80 number c = _nrzMult(a,
b,
R);
88number _nrzMult (number a, number
b,
const coeffs R)
90number nrzMult (number a, number
b,
const coeffs R)
93 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
98 long r=(long)((
unsigned long)(
SR_HDL(a)-1L))*((
unsigned long)(
SR_HDL(
b)>>1));
101 number u=((number) ((r>>1)+
SR_INT));
108 nrzTest((number)erg);
109 return nrz_short((number) erg);
111 else if (n_Z_IS_SMALL(a))
115 mpz_init_set(erg, (mpz_ptr)
b);
117 return nrz_short((number) erg);
119 else if (n_Z_IS_SMALL(
b))
123 mpz_init_set(erg, (mpz_ptr) a);
125 return nrz_short((number) erg);
131 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr)
b);
132 nrzTest((number)erg);
133 return nrz_short((number) erg);
137static long int_gcd(
long a,
long b)
158static number nrzLcm (number a, number
b,
const coeffs R)
164 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
174 mpz_init_set(erg, (mpz_ptr)
b);
178 else if (n_Z_IS_SMALL(
b))
180 mpz_init_set(erg, (mpz_ptr) a);
187 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr)
b);
190 nrzTest((number)erg);
194static number nrzCopy(number a,
const coeffs)
196 if (n_Z_IS_SMALL(a))
return a;
198 mpz_init_set(erg, (mpz_ptr) a);
199 nrzTest((number)erg);
207static number nrzGcd (number a,number
b,
const coeffs R)
209 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
214 else if (n_Z_IS_SMALL(a))
217 return nrzCopy(
b,
R);
221 else if (n_Z_IS_SMALL(
b))
224 return nrzCopy(a,
R);
232 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr)
b);
233 nrzTest((number)erg);
242static long int_extgcd(
long a,
long b,
long * u,
long*
x,
long *
v,
long*
y)
285static number nrzExtGcd (number a, number
b, number *
s, number *t,
const coeffs)
287 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
304 mpz_init_set(aa, (mpz_ptr) a);
312 mpz_init_set(bb, (mpz_ptr)
b);
320 mpz_gcdext(erg, bs, bt, aa, bb);
321 *
s = nrz_short((number) bs);
322 *t = nrz_short((number) bt);
325 return nrz_short((number) erg);
329static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
330static number nrzXExtGcd(number a, number
b, number *
x, number *
y, number * u, number *
v,
const coeffs R)
337 number c = _nrzXExtGcd(a,
b,
x,
y, u,
v,
R);
353static number _nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
355static number nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
358 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
377 mpz_init_set(aa, (mpz_ptr) a);
385 mpz_init_set(bb, (mpz_ptr)
b);
394 mpz_gcdext(erg, bs, bt, aa, bb);
399 mpz_init_set(bu, (mpz_ptr) bb);
400 mpz_init_set(bv, (mpz_ptr) aa);
404 assume(mpz_cmp_si(erg, 0));
406 mpz_div(bu, bu, erg);
407 mpz_div(bv, bv, erg);
410 *u = nrz_short((number) bu);
411 *
v = nrz_short((number) bv);
413 *
s = nrz_short((number) bs);
414 *t = nrz_short((number) bt);
415 return nrz_short((number) erg);
419static number _nrzQuotRem(number, number, number *,
const coeffs);
420static number nrzQuotRem(number a, number
b, number * r,
const coeffs R)
426 number c = _nrzQuotRem(a,
b, r,
R);
439static number _nrzQuotRem (number a, number
b, number * r,
const coeffs )
441static number nrzQuotRem (number a, number
b, number * r,
const coeffs )
459 gsign = mpz_sgn((mpz_ptr)
b);
460 mpz_init_set(bb,(mpz_ptr)
b);
468 mpz_init_set(aa,(mpz_ptr) a);
474 mpz_fdiv_qr(qq, rr, aa, gg);
475 mpz_tdiv_q_2exp(ghalf, gg, 1);
476 if (mpz_cmp(rr, ghalf) > 0)
479 mpz_add_ui(qq, qq, 1);
481 if (gsign < 0) mpz_neg(qq, qq);
494 *r=nrz_short((number)rr);
496 return nrz_short((number)qq);
499static void nrzPower (number a,
int i, number *
result,
const coeffs)
507 mpz_init_set(aa, (mpz_ptr) a);
508 mpz_pow_ui(erg, aa,
i);
509 *
result = nrz_short((number) erg);
519 mpz_init_set_si(erg,
i);
520 return nrz_short((number) erg);
523static number nrzInitMPZ(mpz_t
m,
const coeffs)
526 mpz_init_set(erg,
m);
527 return nrz_short((number) erg);
533 if (*a ==
NULL)
return;
534 if (n_Z_IS_SMALL(*a)==0)
536 mpz_clear((mpz_ptr) *a);
545static long nrzInt(number &n,
const coeffs)
547 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
548 return mpz_get_si( (mpz_ptr)n);
551static number _nrzAdd(number, number,
const coeffs);
552static number nrzAdd(number a, number
b,
const coeffs R)
558 number c = _nrzAdd(a,
b,
R);
566static number _nrzAdd (number a, number
b,
const coeffs )
568static number nrzAdd (number a, number
b,
const coeffs )
571 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
579 mpz_add_ui(erg, erg, (
unsigned long)
SR_TO_INT(
b));
581 mpz_sub_ui(erg, erg, (
unsigned long)-(
SR_TO_INT(
b)));
582 return nrz_short((number)erg);
584 else if (n_Z_IS_SMALL(a))
589 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)
SR_TO_INT(a));
591 mpz_sub_ui(erg, (mpz_ptr)
b, (
unsigned long)-(
SR_TO_INT(a)));
592 return nrz_short((number) erg);
594 else if (n_Z_IS_SMALL(
b))
599 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
601 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(
b)));
602 return nrz_short((number) erg);
608 mpz_add(erg, (mpz_ptr) a, (mpz_ptr)
b);
609 return nrz_short((number) erg);
613static number nrzSub (number a, number
b,
const coeffs )
615 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
623 mpz_sub_ui(erg, erg, (
unsigned long)
SR_TO_INT(
b));
625 mpz_add_ui(erg, erg, (
unsigned long)-(
SR_TO_INT(
b)));
626 return nrz_short((number)erg);
628 else if (n_Z_IS_SMALL(a))
632 mpz_sub(erg, erg, (mpz_ptr)
b);
633 return nrz_short((number) erg);
635 else if (n_Z_IS_SMALL(
b))
640 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
642 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(
b));
643 return nrz_short((number) erg);
649 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr)
b);
650 return nrz_short((number) erg);
656 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
657 return ((
long)a)>((long)
b);
658 else if (n_Z_IS_SMALL(a))
659 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
660 else if (n_Z_IS_SMALL(
b))
661 return 0 < mpz_cmp_si((mpz_ptr)a,
SR_TO_INT(
b));
662 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
670static number nrzGetUnit (number n,
const coeffs r)
672 if (nrzGreaterZero(n, r))
678static number nrzAnn(number n,
const coeffs)
708 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
710 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(
b))
713 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
718 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
722 else if (n_Z_IS_SMALL(a))
726 else if (n_Z_IS_SMALL(
b))
728 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(
b))) != 0;
731 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr)
b) != 0;
734static int nrzDivComp(number a, number
b,
const coeffs r)
736 if (nrzDivBy(a,
b, r))
738 if (nrzDivBy(
b, a, r))
return 2;
741 if (nrzDivBy(
b, a, r))
return 1;
745static number nrzDiv (number a,number
b,
const coeffs cf)
752 else if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
768 else if (n_Z_IS_SMALL(a))
777 else if (n_Z_IS_SMALL(
b))
783 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)))) {
790 return nrz_short((number) erg);
796 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
818 return nrz_short((number) erg);
821static number nrzExactDiv (number a,number
b,
const coeffs cf)
830 else if (n_Z_IS_SMALL(a))
833 mpz_init_set(aa, (mpz_ptr) a);
837 mpz_init_set(bb, (mpz_ptr)
b);
840 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
843 return nrz_short((number) erg);
846static number nrzIntMod (number a,number
b,
const coeffs)
853 mpz_init_set(aa, (mpz_ptr) a);
857 mpz_init_set(bb, (mpz_ptr)
b);
863 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
868 return nrz_short((number) r);
871static number nrzInvers (number c,
const coeffs r)
873 if (!nrzIsUnit((number) c, r))
875 WerrorS(
"Non invertible element.");
881static number nrzNeg (number c,
const coeffs)
890static number nrzFarey(number r, number
N,
const coeffs R)
892 number a0 = nrzCopy(
N,
R);
894 number a1 = nrzCopy(r,
R);
898 PrintS(
"Farey start with ");
906 number as = nrzMult(a1, a1,
R);
908 if (nrzGreater(
N, as,
R))
914 number q = nrzDiv(a0, a1,
R);
915 number t = nrzMult(a1, q,
R),
916 s = nrzSub(a0, t,
R);
922 t = nrzMult(b1, q,
R);
923 s = nrzSub(b0, t,
R);
930 number as = nrzMult(b1, b1,
R);
933 if (nrzGreater(as,
N,
R))
962static number nrzMapMachineInt(number from,
const coeffs ,
const coeffs )
965 mpz_init_set_ui(erg, (
unsigned long) from);
966 return nrz_short((number) erg);
969static number nrzMapZp(number from,
const coeffs ,
const coeffs )
972 mpz_init_set_si(erg, (
long) from);
973 return nrz_short((number) erg);
976static number nrzModNMap(number from,
const coeffs ,
const coeffs )
979 mpz_init_set(erg, (mpz_ptr) from);
980 return nrz_short((number) erg);
983static number nrzMapQ(number from,
const coeffs ,
const coeffs dst)
989 WerrorS(
"rational in map to integer");
993 mpz_init_set(erg, from->z);
994 return nrz_short((number) erg);
1013 return nrzMapMachineInt;
1034 long ui=mpz_get_si((mpz_ptr)
x);
1035 if ((((ui<<3)>>3)==ui)
1036 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
1038 Print(
"gmp-small %s:%d\n",
f,
l);
1055 if (n_Z_IS_SMALL(a))
1061 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1063 z=mpz_get_str(
s,10,(mpz_ptr) a);
1073static const char * nlEatLongC(
char *
s, mpz_ptr
i)
1075 const char * start=
s;
1077 if (*
s<
'0' || *
s>
'9')
1082 while (*
s >=
'0' && *
s <=
'9')
s++;
1085 mpz_set_str(
i,start,10);
1091 mpz_set_str(
i,start,10);
1097static const char * nrzRead (
const char *
s, number *a,
const coeffs)
1102 s = nlEatLongC((
char *)
s, z);
1104 *a = nrz_short((number) z);
1113 if ( n_Z_IS_SMALL(n))
1120 mpz_init_set( dummy,n->z );
1136 WerrorS(
"rational in conversion to integer");
1141 return nrz_short((number)z);
1145static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1147 if (n_Z_IS_SMALL(a))
1150 mpz_init_set(
res, (mpz_ptr) a);
1153static number nrzEucNorm (number a,
const coeffs )
1155 if (n_Z_IS_SMALL(a))
1164 mpz_abs(
abs, (mpz_ptr)a);
1165 nrzTest((number)
abs);
1166 return (number)
abs;
1175 long ch = r->cfInt(c, r);
1176 mpz_init_set_ui(dummy, ch);
1180 mpz_init_set(dummy, (mpz_ptr)c);
1184 info.exp = (
unsigned long) 1;
1204 r->cfMult = nrzMult;
1208 r->cfIntMod= nrzIntMod;
1209 r->cfExactDiv= nrzExactDiv;
1211 r->cfInitMPZ = nrzInitMPZ;
1214 r->cfDivComp = nrzDivComp;
1215 r->cfIsUnit = nrzIsUnit;
1216 r->cfGetUnit = nrzGetUnit;
1218 r->cfExtGcd = nrzExtGcd;
1219 r->cfXExtGcd = nrzXExtGcd;
1220 r->cfEucNorm = nrzEucNorm;
1221 r->cfQuotRem = nrzQuotRem;
1222 r->cfDivBy = nrzDivBy;
1223 r->cfInpNeg = nrzNeg;
1224 r->cfInvers= nrzInvers;
1225 r->cfCopy = nrzCopy;
1227 r->cfRead = nrzRead;
1228 r->cfGreater = nrzGreater;
1229 r->cfEqual = nrzEqual;
1230 r->cfIsZero = nrzIsZero;
1231 r->cfIsOne = nrzIsOne;
1232 r->cfIsMOne = nrzIsMOne;
1233 r->cfGreaterZero = nrzGreaterZero;
1234 r->cfPower = nrzPower;
1238 r->cfSetMap = nrzSetMap;
1239 r->convSingNFactoryN = nrzConvSingNFactoryN;
1240 r->convFactoryNSingN = nrzConvFactoryNSingN;
1242 r->cfFarey = nrzFarey;
1246 r->cfQuot1 = nrzQuot1;
1252 r->cfDBTest=nrzDBTest;
1256 r->has_simple_Alloc=
FALSE;
1257 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
CanonicalForm FACTORY_PUBLIC make_cf(const mpz_ptr n)
void FACTORY_PUBLIC gmp_numerator(const CanonicalForm &f, mpz_ptr result)
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)