11 #include <factory/factory.h> 33 #if SI_INTEGER_VARIANT == 2 41 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr) b);
52 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr) b);
64 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr) b);
80 mpz_gcdext(erg, bs, bt, (mpz_ptr) a, (mpz_ptr) b);
95 mpz_gcdext(erg, bs, bt, (mpz_ptr)a, (mpz_ptr)b);
100 mpz_init_set(bu, (mpz_ptr) b);
101 mpz_init_set(bv, (mpz_ptr) a);
103 assume(mpz_cmp_si(erg, 0));
105 mpz_div(bu, bu, erg);
106 mpz_div(bv, bv, erg);
121 mpz_pow_ui(erg, (mpz_ptr) a, i);
122 *result = (number) erg;
131 mpz_init_set_si(erg, i);
137 if (*a ==
NULL)
return;
138 mpz_clear((mpz_ptr) *a);
147 mpz_init_set(erg, (mpz_ptr) a);
160 if (a ==
NULL)
return 0;
161 return (((mpz_ptr)a)->_mp_alloc);
169 return mpz_get_si( (mpz_ptr)n);
176 mpz_add(erg, (mpz_ptr) a, (mpz_ptr) b);
184 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr) b);
195 return 0 == mpz_cmpabs_ui((mpz_ptr) a, 1);
200 return 0 == mpz_cmpabs_ui((mpz_ptr) a, 0);
205 return (a!=
NULL) && (0 == mpz_cmp_si((mpz_ptr) a, 1));
210 return (a!=
NULL) && (0 == mpz_cmp_si((mpz_ptr) a, -1));
215 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
220 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
225 return 0 < mpz_cmp_si((mpz_ptr) k, 0);
230 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr) b) != 0;
250 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
265 mpz_tdiv_q(erg, (mpz_ptr) a, (mpz_ptr) b);
275 mpz_tdiv_qr(qq, rr, (mpz_ptr) a, (mpz_ptr) b);
294 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
304 WerrorS(
"Non invertible element.");
320 mpz_init_set_ui(erg, (
unsigned long) from);
327 mpz_init_set_si(erg, (
long) from);
335 nlGMP(from, (number) erg, src);
395 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
397 z=mpz_get_str(s,10,(mpz_ptr) a);
408 const char * start=
s;
410 if (*s<'0' || *s>
'9')
415 while (*s >=
'0' && *s <=
'9') s++;
418 mpz_set_str(i,start,10);
424 mpz_set_str(i,start,10);
437 mpz_init_set(num, *((mpz_t*)n));
482 long ch = r->cfInt(c, r);
484 dummy = (mpz_ptr)
omAlloc(
sizeof(mpz_t));
485 mpz_init_set_ui(dummy, ch);
488 info.
exp = (
unsigned long) 1;
508 PrintS(
"Farey start with ");
516 number as =
nrzMult(a1, a1, R);
524 number q =
nrzDiv(a0, a1, R);
540 number as =
nrzMult(b1, b1, R);
630 r->has_simple_Alloc=
FALSE;
631 r->has_simple_Inverse=
FALSE;
635 #elif SI_INTEGER_VARIANT == 3 643 #define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL) 650 static inline number nrz_short(number x)
656 if (mpz_cmp_ui((mpz_ptr) x,0L)==0)
658 mpz_clear((mpz_ptr)x);
667 long ui=mpz_get_si((mpz_ptr)x);
668 if ((((ui<<3)>>3)==ui)
669 && (mpz_cmp_si((mpz_ptr)x,ui)==0))
671 mpz_clear((mpz_ptr)x);
688 if (a ==
NULL)
return 0;
690 if (n_Z_IS_SMALL(a))
return 1;
691 return ((mpz_ptr)a)->_mp_alloc;
700 number _nrzMult(number, number,
const coeffs);
707 number c = _nrzMult(a, b, R);
715 number _nrzMult (number a, number b,
const coeffs R)
720 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b)) {
726 long r=(long)((
unsigned long)(
SR_HDL(a)-1L))*((
unsigned long)(
SR_HDL(b)>>1));
729 number u=((number) ((r>>1)+
SR_INT));
737 nrzTest((number)erg);
740 else if (n_Z_IS_SMALL(a))
745 mpz_init_set(erg, (mpz_ptr) b);
747 nrzTest((number)erg);
750 else if (n_Z_IS_SMALL(b))
755 mpz_init_set(erg, (mpz_ptr) a);
757 nrzTest((number)erg);
764 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr) b);
765 nrzTest((number)erg);
771 static long int_gcd(
long a,
long b)
792 static number
nrzLcm (number a, number b,
const coeffs R)
798 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
808 mpz_init_set(erg, (mpz_ptr) b);
812 else if (n_Z_IS_SMALL(b))
814 mpz_init_set(erg, (mpz_ptr) a);
821 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr) b);
829 if (n_Z_IS_SMALL(a))
return a;
831 mpz_init_set(erg, (mpz_ptr) a);
841 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
846 else if (n_Z_IS_SMALL(a))
850 unsigned long g = mpz_gcd_ui(
NULL, (mpz_ptr)b, (
unsigned long)
ABS(
SR_TO_INT(a)));
853 else if (n_Z_IS_SMALL(b))
857 unsigned long g = mpz_gcd_ui(
NULL, (mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(b)));
864 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr) b);
873 static long int_extgcd(
long a,
long b,
long * u,
long* x,
long *
v,
long*
y)
916 static number
nrzExtGcd (number a, number b, number *
s, number *t,
const coeffs)
918 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
935 mpz_init_set(aa, (mpz_ptr) a);
943 mpz_init_set(bb, (mpz_ptr) b);
951 mpz_gcdext(erg, bs, bt, aa, bb);
952 *s = nrz_short((number) bs);
953 *t = nrz_short((number) bt);
956 return nrz_short((number) erg);
960 static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
961 static number
nrzXExtGcd(number a, number b, number *x, number * y, number * u, number * v,
const coeffs R)
968 number c = _nrzXExtGcd(a, b, x, y, u, v, R);
984 static number _nrzXExtGcd (number a, number b, number *s, number *t, number *u, number *v,
const coeffs )
986 static number
nrzXExtGcd (number a, number b, number *s, number *t, number *u, number *v,
const coeffs )
989 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1002 if (n_Z_IS_SMALL(a))
1008 mpz_init_set(aa, (mpz_ptr) a);
1010 if (n_Z_IS_SMALL(b))
1016 mpz_init_set(bb, (mpz_ptr) b);
1025 mpz_gcdext(erg, bs, bt, aa, bb);
1030 mpz_init_set(bu, (mpz_ptr) bb);
1031 mpz_init_set(bv, (mpz_ptr) aa);
1035 assume(mpz_cmp_si(erg, 0));
1037 mpz_div(bu, bu, erg);
1038 mpz_div(bv, bv, erg);
1041 *u = nrz_short((number) bu);
1042 *v = nrz_short((number) bv);
1044 *s = nrz_short((number) bs);
1045 *t = nrz_short((number) bt);
1046 return nrz_short((number) erg);
1050 static number _nrzQuotRem(number, number, number *,
const coeffs);
1057 number c = _nrzQuotRem(a, b, r, R);
1070 static number _nrzQuotRem (number a, number b, number * r,
const coeffs )
1076 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1082 else if (n_Z_IS_SMALL(a))
1089 else if (n_Z_IS_SMALL(b))
1096 rr = mpz_divmod_ui(qq, rrr, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(b)));
1105 return nrz_short((number)qq);
1111 mpz_divmod(qq, rr, (mpz_ptr)a, (mpz_ptr)b);
1118 nrzTest((number)qq);
1127 if (n_Z_IS_SMALL(a))
1130 mpz_init_set(aa, (mpz_ptr) a);
1131 mpz_pow_ui(erg, aa, i);
1132 *result = nrz_short((number) erg);
1142 mpz_init_set_si(erg, i);
1143 return nrz_short((number) erg);
1149 mpz_init_set(erg, m);
1150 return nrz_short((number) erg);
1156 if (*a ==
NULL)
return;
1157 if (n_Z_IS_SMALL(*a)==0)
1159 mpz_clear((mpz_ptr) *a);
1170 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
1171 return mpz_get_si( (mpz_ptr)n);
1174 static number _nrzAdd(number, number,
const coeffs);
1175 static number
nrzAdd(number a, number b,
const coeffs R)
1181 number c = _nrzAdd(a, b, R);
1189 static number _nrzAdd (number a, number b,
const coeffs )
1191 static number
nrzAdd (number a, number b,
const coeffs )
1194 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1197 if (INT_IS_SMALL(c))
1200 mpz_init_set_si(erg, c);
1202 nrzTest((number)erg);
1203 return (number) erg;
1205 else if (n_Z_IS_SMALL(a))
1210 mpz_add_ui(erg, (mpz_ptr) b, (
unsigned long)
SR_TO_INT(a));
1212 mpz_sub_ui(erg, (mpz_ptr) b, (
unsigned long)-(
SR_TO_INT(a)));
1213 return nrz_short((number) erg);
1215 else if (n_Z_IS_SMALL(b))
1220 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(b));
1222 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(b)));
1223 return nrz_short((number) erg);
1229 mpz_add(erg, (mpz_ptr) a, (mpz_ptr) b);
1230 return nrz_short((number) erg);
1234 static number
nrzSub (number a, number b,
const coeffs )
1236 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1239 if (INT_IS_SMALL(c))
1242 mpz_init_set_si(erg, c);
1243 nrzTest((number)erg);
1244 return (number) erg;
1246 else if (n_Z_IS_SMALL(a))
1252 mpz_ui_sub(erg, (
unsigned long)
SR_TO_INT(a), (mpz_ptr) b);
1255 mpz_add_ui(erg, (mpz_ptr) b, (
unsigned long)-
SR_TO_INT(a));
1258 return nrz_short((number) erg);
1260 else if (n_Z_IS_SMALL(b))
1265 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(b));
1267 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(b));
1268 return nrz_short((number) erg);
1274 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr) b);
1275 return nrz_short((number) erg);
1281 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1282 return ((
long)a)>((long)b);
1283 else if (n_Z_IS_SMALL(a))
1284 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
1285 else if (n_Z_IS_SMALL(b))
1286 return 0 < mpz_cmp_si((mpz_ptr)
a,
SR_TO_INT(b));
1287 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
1303 static number nrzAnn(number n,
const coeffs)
1333 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1335 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(b))
1338 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr) b);
1343 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1347 else if (n_Z_IS_SMALL(a))
1351 else if (n_Z_IS_SMALL(b))
1353 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(b))) != 0;
1356 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr) b) != 0;
1373 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
1382 else if (n_Z_IS_SMALL(a))
1391 else if (n_Z_IS_SMALL(b))
1397 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(b)))) {
1404 return nrz_short((number) erg);
1410 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
1426 if (mpz_cmp_si(r, 0)!=0)
1432 return nrz_short((number) erg);
1439 if (n_Z_IS_SMALL(a))
1442 mpz_init_set(aa, (mpz_ptr) a);
1443 if (n_Z_IS_SMALL(b))
1446 mpz_init_set(bb, (mpz_ptr) b);
1449 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
1452 nrzTest((number)erg);
1453 return (number) erg;
1460 if (n_Z_IS_SMALL(a))
1463 mpz_init_set(aa, (mpz_ptr) a);
1464 if (n_Z_IS_SMALL(b))
1467 mpz_init_set(bb, (mpz_ptr) b);
1473 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
1478 return nrz_short((number) r);
1485 WerrorS(
"Non invertible element.");
1494 if (n_Z_IS_SMALL(c))
1508 PrintS(
"Farey start with ");
1516 number as =
nrzMult(a1, a1, R);
1524 number q =
nrzDiv(a0, a1, R);
1540 number as =
nrzMult(b1, b1, R);
1561 ab =
n_Div(a, b, Q);
1574 mpz_init_set_ui(erg, (
unsigned long) from);
1575 return nrz_short((number) erg);
1581 mpz_init_set_si(erg, (
long) from);
1582 return nrz_short((number) erg);
1585 static number nrzModNMap(number from,
const coeffs ,
const coeffs )
1588 mpz_init_set(erg, (mpz_ptr) from);
1589 return nrz_short((number) erg);
1598 WerrorS(
"rational in map to integer");
1602 mpz_init_set(erg, from->z);
1603 return nrz_short((number) erg);
1648 if (mpz_cmp_ui((mpz_ptr) x,0L)==0)
1650 Print(
"gmp-0 %s:%d\n",f,l);
1655 long ui=mpz_get_si((mpz_ptr)x);
1656 if ((((ui<<3)>>3)==ui)
1657 && (mpz_cmp_si((mpz_ptr)x,ui)==0))
1659 Print(
"gmp-small %s:%d\n",f,l);
1676 if (n_Z_IS_SMALL(a))
1682 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1684 z=mpz_get_str(s,10,(mpz_ptr) a);
1694 static const char *
nlEatLongC(
char *s, mpz_ptr i)
1696 const char * start=
s;
1698 if (*s<'0' || *s>
'9')
1703 while (*s >=
'0' && *s <=
'9') s++;
1706 mpz_set_str(i,start,10);
1712 mpz_set_str(i,start,10);
1718 static const char *
nrzRead (
const char *s, number *a,
const coeffs)
1725 *a = nrz_short((number) z);
1732 PrintS(
"// coeff. ring is : Integers\n");
1745 if ( n_Z_IS_SMALL(n))
1752 mpz_init_set( dummy,n->z );
1768 WerrorS(
"rational in conversion to integer");
1773 return nrz_short((number)z);
1777 static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1779 if (n_Z_IS_SMALL(a))
1782 mpz_init_set(res, (mpz_ptr) a);
1788 dummy = (mpz_ptr)
omAlloc(
sizeof(mpz_t));
1791 long ch = r->cfInt(c, r);
1792 mpz_init_set_ui(dummy, ch);
1796 mpz_init_set(dummy, (mpz_ptr)c);
1800 info.
exp = (
unsigned long) 1;
1869 r->has_simple_Alloc=
FALSE;
1870 r->has_simple_Inverse=
FALSE;
1874 #elif SI_INTEGER_VARIANT == 1 1880 #error set SI_INTEGER_VARIANT
static const char * nrzRead(const char *s, number *a, const coeffs)
static number nrzMapZp(number from, const coeffs, const coeffs)
static BOOLEAN nrzEqual(number a, number b, const coeffs)
static number nrzMapMachineInt(number from, const coeffs, const coeffs)
static number nrzExtGcd(number a, number b, number *s, number *t, const coeffs)
const CanonicalForm int s
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_ModN(const coeffs r)
const CanonicalForm int const CFList const Variable & y
void mpz_mul_si(mpz_ptr r, mpz_srcptr s, long int si)
static number nrzMult(number a, number b, const coeffs)
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)
void nrzWrite(number a, const coeffs)
static number nrzNeg(number c, const coeffs)
static void nrzDelete(number *a, const coeffs)
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 number nrzAdd(number a, number b, const coeffs)
static number nrzInitMPZ(mpz_t m, const coeffs)
static void nrzCoeffWrite(const coeffs, BOOLEAN)
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_Z(const coeffs r)
#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)
static number nrzGcd(number a, number b, const coeffs)
void nlGMP(number &i, number n, const coeffs r)
BOOLEAN nlInitChar(coeffs r, void *p)
static BOOLEAN nrzDBTest(number, const char *, const int, const coeffs)
static BOOLEAN nrzDivBy(number a, number b, const coeffs)
static long nrzInt(number &n, const coeffs)
static BOOLEAN nrzGreaterZero(number k, const coeffs)
static int nrzDivComp(number a, number b, const coeffs r)
static number nrzIntMod(number a, number b, const coeffs)
static number nrzCopy(number a, const coeffs)
static number nrzExactDiv(number a, number b, const coeffs)
Coefficient rings, fields and other domains suitable for Singular polynomials.
static BOOLEAN nrzGreater(number a, number b, const coeffs)
static number nrzMapQ(number from, const coeffs src, const coeffs)
const CanonicalForm CFMap CFMap & N
void nrzSetExp(int, coeffs)
static number nrzGetUnit(number, const coeffs r)
static BOOLEAN nrzIsUnit(number a, const coeffs)
The main handler for Singular numbers which are suitable for Singular polynomials.
BOOLEAN nrzInitChar(coeffs r, void *)
void StringSetS(const char *st)
static BOOLEAN nrzIsOne(number a, const coeffs)
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
static number nrzLcm(number a, number b, const coeffs)
static CanonicalForm nrzConvSingNFactoryN(number n, BOOLEAN setChar, const coeffs)
only used if HAVE_RINGS is defined
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_PtoM(const coeffs r)
void PrintS(const char *s)
(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 int nrzSize(number a, const coeffs)
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
static char * nrzCoeffName(const coeffs)
void gmp_numerator(const CanonicalForm &f, mpz_ptr result)
#define omGetSpecBin(size)
number nrzInit(long i, const coeffs)
number nlChineseRemainderSym(number *x, number *q, int rl, BOOLEAN sym, CFArray &inv_cache, const coeffs CF)
const Variable & v
< [in] a sqrfree bivariate poly
static number nrzDiv(number a, number b, const coeffs)
static BOOLEAN nrzIsZero(number a, const coeffs)
void nrzInitExp(int, coeffs)
static number nrzFarey(number r, number N, const coeffs R)
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...
static BOOLEAN nrzIsMOne(number a, const coeffs)
static void nrzPower(number a, int i, number *result, const coeffs)
static coeffs nrzQuot1(number c, const coeffs r)
static number nrzQuotRem(number a, number b, number *r, const coeffs)
static char * nrzCoeffString(const coeffs cf)
static number nrzXExtGcd(number a, number b, number *s, number *t, number *u, number *v, const coeffs)
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
static number nrzConvFactoryNSingN(const CanonicalForm n, const coeffs r)
#define omFreeBin(addr, bin)
static const char * nlEatLongC(char *s, mpz_ptr i)
static number nrzSub(number a, number b, const coeffs)
void nKillChar(coeffs r)
undo all initialisations
static number nrzInvers(number c, const coeffs r)
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
static nMapFunc nrzSetMap(const coeffs src, const coeffs)