8 #if SI_INTEGER_VARIANT == 3 16 #define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL) 23 static 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);
62 if (n_Z_IS_SMALL(a))
return 1;
63 return ((mpz_ptr)a)->_mp_alloc;
72 number _nrzMult(number, number,
const coeffs);
73 number nrzMult(number a, number
b,
const coeffs R)
79 number c = _nrzMult(a, b, R);
87 number _nrzMult (number a, number
b,
const coeffs R)
89 number nrzMult (number a, number b,
const coeffs R)
92 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));
110 nrzTest((number)erg);
113 else if (n_Z_IS_SMALL(a))
118 mpz_init_set(erg, (mpz_ptr) b);
120 nrzTest((number)erg);
123 else if (n_Z_IS_SMALL(b))
128 mpz_init_set(erg, (mpz_ptr) a);
130 nrzTest((number)erg);
137 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr) b);
138 nrzTest((number)erg);
144 static long int_gcd(
long a,
long b)
165 static number nrzLcm (number a, number b,
const coeffs R)
171 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
181 mpz_init_set(erg, (mpz_ptr) b);
185 else if (n_Z_IS_SMALL(b))
187 mpz_init_set(erg, (mpz_ptr) a);
194 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr) b);
200 static number nrzCopy(number a,
const coeffs)
202 if (n_Z_IS_SMALL(a))
return a;
204 mpz_init_set(erg, (mpz_ptr) a);
212 static number nrzGcd (number a,number b,
const coeffs R)
214 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
219 else if (n_Z_IS_SMALL(a))
222 return nrzCopy(b, R);
223 unsigned long g = mpz_gcd_ui(
NULL, (mpz_ptr)b, (
unsigned long)
ABS(
SR_TO_INT(a)));
226 else if (n_Z_IS_SMALL(b))
229 return nrzCopy(a, R);
230 unsigned long g = mpz_gcd_ui(
NULL, (mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(b)));
237 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr) b);
246 static long int_extgcd(
long a,
long b,
long * u,
long* x,
long *
v,
long*
y)
289 static number nrzExtGcd (number a, number b, number *
s, number *t,
const coeffs)
291 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
308 mpz_init_set(aa, (mpz_ptr) a);
316 mpz_init_set(bb, (mpz_ptr) b);
324 mpz_gcdext(erg, bs, bt, aa, bb);
325 *s = nrz_short((number) bs);
326 *t = nrz_short((number) bt);
329 return nrz_short((number) erg);
333 static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
334 static number nrzXExtGcd(number a, number b, number *x, number * y, number * u, number * v,
const coeffs R)
341 number c = _nrzXExtGcd(a, b, x, y, u, v, R);
357 static number _nrzXExtGcd (number a, number b, number *s, number *t, number *u, number *v,
const coeffs )
359 static number nrzXExtGcd (number a, number b, number *s, number *t, number *u, number *v,
const coeffs )
362 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
381 mpz_init_set(aa, (mpz_ptr) a);
389 mpz_init_set(bb, (mpz_ptr) b);
398 mpz_gcdext(erg, bs, bt, aa, bb);
403 mpz_init_set(bu, (mpz_ptr) bb);
404 mpz_init_set(bv, (mpz_ptr) aa);
408 assume(mpz_cmp_si(erg, 0));
410 mpz_div(bu, bu, erg);
411 mpz_div(bv, bv, erg);
414 *u = nrz_short((number) bu);
415 *v = nrz_short((number) bv);
417 *s = nrz_short((number) bs);
418 *t = nrz_short((number) bt);
419 return nrz_short((number) erg);
423 static number _nrzQuotRem(number, number, number *,
const coeffs);
424 static number nrzQuotRem(number a, number b, number * r,
const coeffs R)
430 number c = _nrzQuotRem(a, b, r, R);
443 static number _nrzQuotRem (number a, number b, number * r,
const coeffs )
445 static number nrzQuotRem (number a, number b, number * r,
const coeffs )
449 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
455 else if (n_Z_IS_SMALL(a))
462 else if (n_Z_IS_SMALL(b))
469 rr = mpz_divmod_ui(qq, rrr, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(b)));
478 return nrz_short((number)qq);
484 mpz_divmod(qq, rr, (mpz_ptr)a, (mpz_ptr)b);
495 static void nrzPower (number a,
int i, number *
result,
const coeffs)
503 mpz_init_set(aa, (mpz_ptr) a);
504 mpz_pow_ui(erg, aa, i);
505 *result = nrz_short((number) erg);
515 mpz_init_set_si(erg, i);
516 return nrz_short((number) erg);
519 static number nrzInitMPZ(mpz_t
m,
const coeffs)
522 mpz_init_set(erg, m);
523 return nrz_short((number) erg);
529 if (*a ==
NULL)
return;
530 if (n_Z_IS_SMALL(*a)==0)
532 mpz_clear((mpz_ptr) *a);
541 static long nrzInt(number &
n,
const coeffs)
543 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
544 return mpz_get_si( (mpz_ptr)n);
547 static number _nrzAdd(number, number,
const coeffs);
548 static number nrzAdd(number a, number b,
const coeffs R)
554 number c = _nrzAdd(a, b, R);
562 static number _nrzAdd (number a, number b,
const coeffs )
564 static number nrzAdd (number a, number b,
const coeffs )
567 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
573 mpz_init_set_si(erg, c);
575 nrzTest((number)erg);
578 else if (n_Z_IS_SMALL(a))
583 mpz_add_ui(erg, (mpz_ptr) b, (
unsigned long)
SR_TO_INT(a));
585 mpz_sub_ui(erg, (mpz_ptr) b, (
unsigned long)-(
SR_TO_INT(a)));
586 return nrz_short((number) erg);
588 else if (n_Z_IS_SMALL(b))
593 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(b));
595 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(b)));
596 return nrz_short((number) erg);
602 mpz_add(erg, (mpz_ptr) a, (mpz_ptr) b);
603 return nrz_short((number) erg);
607 static number nrzSub (number a, number b,
const coeffs )
609 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
615 mpz_init_set_si(erg, c);
616 nrzTest((number)erg);
619 else if (n_Z_IS_SMALL(a))
625 mpz_ui_sub(erg, (
unsigned long)
SR_TO_INT(a), (mpz_ptr) b);
628 mpz_add_ui(erg, (mpz_ptr) b, (
unsigned long)-
SR_TO_INT(a));
631 return nrz_short((number) erg);
633 else if (n_Z_IS_SMALL(b))
638 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(b));
640 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(b));
641 return nrz_short((number) erg);
647 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr) b);
648 return nrz_short((number) erg);
654 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
655 return ((
long)a)>((long)b);
656 else if (n_Z_IS_SMALL(a))
657 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
658 else if (n_Z_IS_SMALL(b))
659 return 0 < mpz_cmp_si((mpz_ptr)a,
SR_TO_INT(b));
660 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
668 static number nrzGetUnit (number n,
const coeffs r)
670 if (nrzGreaterZero(n, r))
676 static number nrzAnn(number n,
const coeffs)
706 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
708 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(b))
711 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
716 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
720 else if (n_Z_IS_SMALL(a))
724 else if (n_Z_IS_SMALL(b))
726 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(b))) != 0;
729 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr) b) != 0;
732 static int nrzDivComp(number a, number b,
const coeffs r)
734 if (nrzDivBy(a, b, r))
736 if (nrzDivBy(b, a, r))
return 2;
739 if (nrzDivBy(b, a, r))
return 1;
743 static number nrzDiv (number a,number b,
const coeffs)
746 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
755 else if (n_Z_IS_SMALL(a))
764 else if (n_Z_IS_SMALL(b))
770 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(b)))) {
777 return nrz_short((number) erg);
783 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
805 return nrz_short((number) erg);
808 static number nrzExactDiv (number a,number b,
const coeffs)
815 mpz_init_set(aa, (mpz_ptr) a);
819 mpz_init_set(bb, (mpz_ptr) b);
822 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
825 nrzTest((number)erg);
829 static number nrzIntMod (number a,number b,
const coeffs)
836 mpz_init_set(aa, (mpz_ptr) a);
840 mpz_init_set(bb, (mpz_ptr) b);
846 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
851 return nrz_short((number) r);
854 static number nrzInvers (number c,
const coeffs r)
856 if (!nrzIsUnit((number) c, r))
858 WerrorS(
"Non invertible element.");
864 static number nrzNeg (number c,
const coeffs)
873 static number nrzFarey(number r, number
N,
const coeffs R)
875 number a0 = nrzCopy(N, R);
877 number a1 = nrzCopy(r, R);
881 PrintS(
"Farey start with ");
889 number as = nrzMult(a1, a1, R);
891 if (nrzGreater(N, as, R))
897 number q = nrzDiv(a0, a1, R);
898 number t = nrzMult(a1, q, R),
899 s = nrzSub(a0, t, R);
905 t = nrzMult(b1, q, R);
906 s = nrzSub(b0, t, R);
913 number as = nrzMult(b1, b1, R);
916 if (nrzGreater(as, N, R))
944 static number nrzMapMachineInt(number from,
const coeffs ,
const coeffs )
947 mpz_init_set_ui(erg, (
unsigned long) from);
948 return nrz_short((number) erg);
951 static number nrzMapZp(number from,
const coeffs ,
const coeffs )
954 mpz_init_set_si(erg, (
long) from);
955 return nrz_short((number) erg);
958 static number nrzModNMap(number from,
const coeffs ,
const coeffs )
961 mpz_init_set(erg, (mpz_ptr) from);
962 return nrz_short((number) erg);
965 static number nrzMapQ(number from,
const coeffs ,
const coeffs dst)
971 WerrorS(
"rational in map to integer");
975 mpz_init_set(erg, from->z);
976 return nrz_short((number) erg);
995 return nrzMapMachineInt;
1009 void nrzSetExp(
int,
coeffs)
1013 void nrzInitExp(
int,
coeffs)
1018 BOOLEAN nrzDBTest (number x,
const char *
f,
const int l,
const coeffs)
1023 Print(
"gmp-0 %s:%d\n",f,l);
1028 long ui=mpz_get_si((mpz_ptr)x);
1029 if ((((ui<<3)>>3)==ui)
1030 && (mpz_cmp_si((mpz_ptr)x,ui)==0))
1032 Print(
"gmp-small %s:%d\n",f,l);
1049 if (n_Z_IS_SMALL(a))
1055 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1057 z=mpz_get_str(s,10,(mpz_ptr) a);
1067 static const char * nlEatLongC(
char *s, mpz_ptr i)
1069 const char * start=
s;
1071 if (*s<'0' || *s>
'9')
1076 while (*s >=
'0' && *s <=
'9') s++;
1079 mpz_set_str(i,start,10);
1085 mpz_set_str(i,start,10);
1091 static const char * nrzRead (
const char *s, number *a,
const coeffs)
1096 s = nlEatLongC((
char *) s, z);
1098 *a = nrz_short((number) z);
1107 if ( n_Z_IS_SMALL(n))
1114 mpz_init_set( dummy,n->z );
1130 WerrorS(
"rational in conversion to integer");
1135 return nrz_short((number)z);
1139 static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1141 if (n_Z_IS_SMALL(a))
1144 mpz_init_set(res, (mpz_ptr) a);
1152 long ch = r->cfInt(c, r);
1153 mpz_init_set_ui(dummy, ch);
1157 mpz_init_set(dummy, (mpz_ptr)c);
1161 info.
exp = (
unsigned long) 1;
1182 r->cfMult = nrzMult;
1186 r->cfIntMod= nrzIntMod;
1187 r->cfExactDiv= nrzExactDiv;
1189 r->cfInitMPZ = nrzInitMPZ;
1193 r->cfDivComp = nrzDivComp;
1194 r->cfIsUnit = nrzIsUnit;
1195 r->cfGetUnit = nrzGetUnit;
1197 r->cfExtGcd = nrzExtGcd;
1198 r->cfXExtGcd = nrzXExtGcd;
1199 r->cfEucNorm = nrzEucNorm;
1200 r->cfQuotRem = nrzSmallestQuotRem;
1201 r->cfDivBy = nrzDivBy;
1203 r->cfInpNeg = nrzNeg;
1204 r->cfInvers= nrzInvers;
1205 r->cfCopy = nrzCopy;
1207 r->cfRead = nrzRead;
1208 r->cfGreater = nrzGreater;
1209 r->cfEqual = nrzEqual;
1210 r->cfIsZero = nrzIsZero;
1211 r->cfIsOne = nrzIsOne;
1212 r->cfIsMOne = nrzIsMOne;
1213 r->cfGreaterZero = nrzGreaterZero;
1214 r->cfPower = nrzPower;
1218 r->cfSetMap = nrzSetMap;
1220 r->convSingNFactoryN = nrzConvSingNFactoryN;
1221 r->convFactoryNSingN = nrzConvFactoryNSingN;
1223 r->cfFarey = nrzFarey;
1227 r->cfQuot1 = nrzQuot1;
1233 r->cfDBTest=nrzDBTest;
1237 r->has_simple_Alloc=
FALSE;
1238 r->has_simple_Inverse=
FALSE;
void nrzWrite(number a, const coeffs r)
const CanonicalForm int s
const CanonicalForm int const CFList const Variable & y
void mpz_mul_si(mpz_ptr r, mpz_srcptr s, long int si)
only used if HAVE_RINGS is defined
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
static int int_extgcd(int a, int b, int *u, int *x, int *v, int *y)
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
number ndCopyMap(number a, const coeffs aRing, const coeffs r)
static void nrzCoeffWrite(const coeffs, BOOLEAN)
#define omFreeSize(addr, size)
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_2toM(const coeffs r)
(), see rinteger.h, new impl.
void WerrorS(const char *s)
CanonicalForm make_cf(const mpz_ptr n)
void nlWriteFd(number n, const ssiInfo *d, const coeffs)
static FORCE_INLINE BOOLEAN nCoeff_is_Z(const coeffs r)
const CanonicalForm CFMap CFMap & N
number nlReadFd(const ssiInfo *d, const coeffs)
The main handler for Singular numbers which are suitable for Singular polynomials.
void StringSetS(const char *st)
void StringAppendS(const char *st)
const ExtensionInfo & info
< [in] sqrfree poly
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
only used if HAVE_RINGS is defined
void PrintS(const char *s)
number nrzInit(long i, const coeffs r)
(mpz_ptr), see rmodulon,h
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 n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
static char * nrzCoeffName(const coeffs)
int nrzSize(number a, const coeffs)
void gmp_numerator(const CanonicalForm &f, mpz_ptr result)
number nlChineseRemainderSym(number *x, number *q, int rl, BOOLEAN sym, CFArray &inv_cache, const coeffs CF)
const Variable & v
< [in] a sqrfree bivariate poly
BOOLEAN nrzInitChar(coeffs r, void *parameter)
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...
void nrzDelete(number *a, const coeffs)
static char * nrzCoeffString(const coeffs cf)
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
#define omFreeBin(addr, bin)
void nKillChar(coeffs r)
undo all initialisations
void n_Print(number &a, const coeffs r)
print a number (BEWARE of string buffers!) mostly for debugging
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL