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);
86 static number
nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
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);
131 mpz_init_set_si(erg,
i);
139 mpz_clear((mpz_ptr) *a);
149 mpz_init_set(erg, (mpz_ptr) a);
154 number nrzCopyMap(number a,
const coeffs ,
const coeffs dst)
162 mpz_ptr
p=(mpz_ptr)a;
164 if (
s==1)
s=(mpz_cmp_ui(
p,0)!=0);
174 return mpz_get_si( (mpz_ptr)n);
181 mpz_add(erg, (mpz_ptr) a, (mpz_ptr)
b);
189 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr)
b);
200 return 0 == mpz_cmpabs_ui((mpz_ptr) a, 1);
205 return 0 == mpz_cmpabs_ui((mpz_ptr) a, 0);
210 return (0 == mpz_cmp_ui((mpz_ptr) a, 1));
215 return (0 == mpz_cmp_si((mpz_ptr) a, -1));
220 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
225 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
235 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr)
b) != 0;
255 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
270 mpz_tdiv_q(erg, (mpz_ptr) a, (mpz_ptr)
b);
280 mpz_tdiv_qr(qq, rr, (mpz_ptr) a, (mpz_ptr)
b);
299 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
309 WerrorS(
"Non invertible element.");
325 mpz_init_set_ui(erg, (
unsigned long) from);
332 mpz_init_set_si(erg, (
long) from);
340 nlGMP(from, erg, src);
400 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
402 z=mpz_get_str(
s,10,(mpz_ptr) a);
413 const char * start=
s;
415 if (*s<'0' || *s>
'9')
420 while (*
s >=
'0' && *
s <=
'9')
s++;
423 mpz_set_str(
i,start,10);
429 mpz_set_str(
i,start,10);
442 mpz_init_set(
num, *((mpz_t*)n));
457 WarnS(
"denominator is not 1 in factory");
491 long ch = r->cfInt(c, r);
493 mpz_init_set_ui(dummy, ch);
496 info.exp = (
unsigned long) 1;
517 PrintS(
"Farey start with ");
654 r->has_simple_Alloc=
FALSE;
655 r->has_simple_Inverse=
FALSE;
659 #elif SI_INTEGER_VARIANT == 3
667 #define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL)
674 static inline number nrz_short(number
x)
682 mpz_clear((mpz_ptr)
x);
691 long ui=mpz_get_si((mpz_ptr)
x);
692 if ((((ui<<3)>>3)==ui)
693 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
695 mpz_clear((mpz_ptr)
x);
713 if (n_Z_IS_SMALL(a))
return 1;
714 return ((mpz_ptr)a)->_mp_alloc;
723 number _nrzMult(number, number,
const coeffs);
730 number c = _nrzMult(a,
b,
R);
738 number _nrzMult (number a, number
b,
const coeffs R)
743 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
750 long r=(long)((
unsigned long)(
SR_HDL(a)-1L))*((
unsigned long)(
SR_HDL(
b)>>1));
753 number u=((number) ((r>>1)+
SR_INT));
761 nrzTest((number)erg);
764 else if (n_Z_IS_SMALL(a))
769 mpz_init_set(erg, (mpz_ptr)
b);
771 nrzTest((number)erg);
774 else if (n_Z_IS_SMALL(
b))
779 mpz_init_set(erg, (mpz_ptr) a);
781 nrzTest((number)erg);
788 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr)
b);
789 nrzTest((number)erg);
795 static long int_gcd(
long a,
long b)
822 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
832 mpz_init_set(erg, (mpz_ptr)
b);
836 else if (n_Z_IS_SMALL(
b))
838 mpz_init_set(erg, (mpz_ptr) a);
845 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr)
b);
853 if (n_Z_IS_SMALL(a))
return a;
855 mpz_init_set(erg, (mpz_ptr) a);
865 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
870 else if (n_Z_IS_SMALL(a))
877 else if (n_Z_IS_SMALL(
b))
888 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr)
b);
897 static long int_extgcd(
long a,
long b,
long * u,
long*
x,
long *
v,
long*
y)
942 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
959 mpz_init_set(aa, (mpz_ptr) a);
967 mpz_init_set(bb, (mpz_ptr)
b);
975 mpz_gcdext(erg, bs, bt, aa, bb);
976 *
s = nrz_short((number) bs);
977 *t = nrz_short((number) bt);
980 return nrz_short((number) erg);
984 static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
985 static number
nrzXExtGcd(number a, number
b, number *
x, number *
y, number * u, number *
v,
const coeffs R)
992 number c = _nrzXExtGcd(a,
b,
x,
y, u,
v,
R);
1008 static number _nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
1010 static number
nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
1013 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1026 if (n_Z_IS_SMALL(a))
1032 mpz_init_set(aa, (mpz_ptr) a);
1034 if (n_Z_IS_SMALL(
b))
1040 mpz_init_set(bb, (mpz_ptr)
b);
1049 mpz_gcdext(erg, bs, bt, aa, bb);
1054 mpz_init_set(bu, (mpz_ptr) bb);
1055 mpz_init_set(bv, (mpz_ptr) aa);
1059 assume(mpz_cmp_si(erg, 0));
1061 mpz_div(bu, bu, erg);
1062 mpz_div(bv, bv, erg);
1065 *u = nrz_short((number) bu);
1066 *
v = nrz_short((number) bv);
1068 *
s = nrz_short((number) bs);
1069 *t = nrz_short((number) bt);
1070 return nrz_short((number) erg);
1074 static number _nrzQuotRem(number, number, number *,
const coeffs);
1081 number c = _nrzQuotRem(a,
b, r,
R);
1094 static number _nrzQuotRem (number a, number
b, number * r,
const coeffs )
1100 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1106 else if (n_Z_IS_SMALL(a))
1113 else if (n_Z_IS_SMALL(
b))
1120 rr = mpz_divmod_ui(qq, rrr, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)));
1129 return nrz_short((number)qq);
1135 mpz_divmod(qq, rr, (mpz_ptr)a, (mpz_ptr)
b);
1142 nrzTest((number)qq);
1151 if (n_Z_IS_SMALL(a))
1154 mpz_init_set(aa, (mpz_ptr) a);
1155 mpz_pow_ui(erg, aa,
i);
1156 *
result = nrz_short((number) erg);
1166 mpz_init_set_si(erg,
i);
1167 return nrz_short((number) erg);
1173 mpz_init_set(erg,
m);
1174 return nrz_short((number) erg);
1180 if (*a ==
NULL)
return;
1181 if (n_Z_IS_SMALL(*a)==0)
1183 mpz_clear((mpz_ptr) *a);
1194 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
1195 return mpz_get_si( (mpz_ptr)n);
1198 static number _nrzAdd(number, number,
const coeffs);
1205 number c = _nrzAdd(a,
b,
R);
1213 static number _nrzAdd (number a, number
b,
const coeffs )
1218 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1221 if (INT_IS_SMALL(c))
1224 mpz_init_set_si(erg, c);
1226 nrzTest((number)erg);
1227 return (number) erg;
1229 else if (n_Z_IS_SMALL(a))
1234 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)
SR_TO_INT(a));
1236 mpz_sub_ui(erg, (mpz_ptr)
b, (
unsigned long)-(
SR_TO_INT(a)));
1237 return nrz_short((number) erg);
1239 else if (n_Z_IS_SMALL(
b))
1244 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
1246 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(
b)));
1247 return nrz_short((number) erg);
1253 mpz_add(erg, (mpz_ptr) a, (mpz_ptr)
b);
1254 return nrz_short((number) erg);
1260 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1263 if (INT_IS_SMALL(c))
1266 mpz_init_set_si(erg, c);
1267 nrzTest((number)erg);
1268 return (number) erg;
1270 else if (n_Z_IS_SMALL(a))
1276 mpz_ui_sub(erg, (
unsigned long)
SR_TO_INT(a), (mpz_ptr)
b);
1279 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)-
SR_TO_INT(a));
1282 return nrz_short((number) erg);
1284 else if (n_Z_IS_SMALL(
b))
1289 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
1291 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(
b));
1292 return nrz_short((number) erg);
1298 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr)
b);
1299 return nrz_short((number) erg);
1305 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1306 return ((
long)a)>((long)
b);
1307 else if (n_Z_IS_SMALL(a))
1308 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
1309 else if (n_Z_IS_SMALL(
b))
1310 return 0 < mpz_cmp_si((mpz_ptr)a,
SR_TO_INT(
b));
1311 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
1327 static number nrzAnn(number n,
const coeffs)
1357 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1359 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(
b))
1362 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
1367 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1371 else if (n_Z_IS_SMALL(a))
1375 else if (n_Z_IS_SMALL(
b))
1377 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(
b))) != 0;
1380 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr)
b) != 0;
1397 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1406 else if (n_Z_IS_SMALL(a))
1415 else if (n_Z_IS_SMALL(
b))
1421 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)))) {
1428 return nrz_short((number) erg);
1434 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
1456 return nrz_short((number) erg);
1463 if (n_Z_IS_SMALL(a))
1466 mpz_init_set(aa, (mpz_ptr) a);
1467 if (n_Z_IS_SMALL(
b))
1470 mpz_init_set(bb, (mpz_ptr)
b);
1473 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
1476 nrzTest((number)erg);
1477 return (number) erg;
1484 if (n_Z_IS_SMALL(a))
1487 mpz_init_set(aa, (mpz_ptr) a);
1488 if (n_Z_IS_SMALL(
b))
1491 mpz_init_set(bb, (mpz_ptr)
b);
1497 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
1502 return nrz_short((number) r);
1509 WerrorS(
"Non invertible element.");
1510 return (number)
NULL;
1518 if (n_Z_IS_SMALL(c))
1532 PrintS(
"Farey start with ");
1598 mpz_init_set_ui(erg, (
unsigned long) from);
1599 return nrz_short((number) erg);
1605 mpz_init_set_si(erg, (
long) from);
1606 return nrz_short((number) erg);
1609 static number nrzModNMap(number from,
const coeffs ,
const coeffs )
1612 mpz_init_set(erg, (mpz_ptr) from);
1613 return nrz_short((number) erg);
1622 WerrorS(
"rational in map to integer");
1626 mpz_init_set(erg, from->z);
1627 return nrz_short((number) erg);
1679 long ui=mpz_get_si((mpz_ptr)
x);
1680 if ((((ui<<3)>>3)==ui)
1681 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
1683 Print(
"gmp-small %s:%d\n",
f,
l);
1700 if (n_Z_IS_SMALL(a))
1706 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1708 z=mpz_get_str(
s,10,(mpz_ptr) a);
1720 const char * start=
s;
1722 if (*s<'0' || *s>
'9')
1727 while (*
s >=
'0' && *
s <=
'9')
s++;
1730 mpz_set_str(
i,start,10);
1736 mpz_set_str(
i,start,10);
1742 static const char *
nrzRead (
const char *
s, number *a,
const coeffs)
1749 *a = nrz_short((number) z);
1756 PrintS(
"// coeff. ring is : Integers\n");
1769 if ( n_Z_IS_SMALL(n))
1776 mpz_init_set( dummy,n->z );
1792 WerrorS(
"rational in conversion to integer");
1797 return nrz_short((number)z);
1801 static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1803 if (n_Z_IS_SMALL(a))
1806 mpz_init_set(
res, (mpz_ptr) a);
1814 long ch = r->cfInt(c, r);
1815 mpz_init_set_ui(dummy, ch);
1819 mpz_init_set(dummy, (mpz_ptr)c);
1823 info.exp = (
unsigned long) 1;
1897 r->has_simple_Alloc=
FALSE;
1898 r->has_simple_Inverse=
FALSE;
1902 #elif SI_INTEGER_VARIANT == 1
1908 #error set SI_INTEGER_VARIANT
const CanonicalForm CFMap CFMap & N
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE BOOLEAN nCoeff_is_Z(const coeffs r)
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_PtoM(const coeffs r)
@ 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_Zn(const coeffs r)
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
const ExtensionInfo & info
< [in] sqrfree poly
CanonicalForm make_cf(const mpz_ptr n)
void 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)
BOOLEAN nlInitChar(coeffs r, void *p)
number nlReadFd(const ssiInfo *d, const coeffs)
void nlGMP(number &i, mpz_t n, const coeffs r)
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.
number ndCopyMap(number a, const coeffs aRing, const coeffs r)
#define omFreeSize(addr, size)
#define omFreeBin(addr, bin)
#define omGetSpecBin(size)
void StringSetS(const char *st)
void StringAppendS(const char *st)
void PrintS(const char *s)
static number nrzSub(number a, number b, const coeffs)
number nrzReadFd(const ssiInfo *d, const coeffs)
static number nrzNeg(number c, const coeffs)
static BOOLEAN nrzGreater(number a, number b, const coeffs)
static BOOLEAN nrzEqual(number a, number b, const coeffs)
void nrzWrite(number a, const coeffs)
static number nrzGcd(number a, number b, const coeffs)
static number nrzDiv(number a, number b, const coeffs)
static number nrzMult(number a, number b, const coeffs)
static BOOLEAN nrzIsMOne(number a, const coeffs)
static number nrzMapQ(number from, const coeffs src, const coeffs)
void nrzDelete(number *a, const coeffs)
static number nrzLcm(number a, number b, const coeffs)
static BOOLEAN nrzIsZero(number a, const coeffs)
static number nrzConvFactoryNSingN(const CanonicalForm n, const coeffs r)
static void nrzPower(number a, int i, number *result, const coeffs)
number nrzInit(long i, const coeffs)
static number nrzInvers(number c, const coeffs r)
static number nrzXExtGcd(number a, number b, number *s, number *t, number *u, number *v, const coeffs)
static number nrzExactDiv(number a, number b, const coeffs)
static long nrzInt(number &n, const coeffs)
int nrzSize(number a, const coeffs)
void nrzInitExp(int, coeffs)
static number nrzIntMod(number a, number b, const coeffs)
static BOOLEAN nrzIsUnit(number a, const coeffs)
static int nrzDivComp(number a, number b, const coeffs r)
static BOOLEAN nrzDivBy(number a, number b, const coeffs)
static void nrzCoeffWrite(const coeffs, BOOLEAN)
static number nrzAdd(number a, number b, const coeffs)
static number nrzExtGcd(number a, number b, number *s, number *t, const coeffs)
static coeffs nrzQuot1(number c, const coeffs r)
BOOLEAN nrzInitChar(coeffs r, void *)
void nrzSetExp(int, coeffs)
static BOOLEAN nrzGreaterZero(number k, const coeffs)
static nMapFunc nrzSetMap(const coeffs src, const coeffs)
static BOOLEAN nrzIsOne(number a, const coeffs)
static char * nrzCoeffName(const coeffs)
static number nrzQuotRem(number a, number b, number *r, const coeffs)
static number nrzGetUnit(number, const coeffs r)
static number nrzInitMPZ(mpz_t m, const coeffs)
static const char * nlEatLongC(char *s, mpz_ptr i)
static BOOLEAN nrzDBTest(number, const char *, const int, const coeffs)
static const char * nrzRead(const char *s, number *a, const coeffs)
void nrzWriteFd(number n, const ssiInfo *d, const coeffs)
static CanonicalForm nrzConvSingNFactoryN(number n, BOOLEAN setChar, const coeffs)
static number nrzMapZp(number from, const coeffs, const coeffs)
static number nrzFarey(number r, number N, const coeffs R)
static char * nrzCoeffString(const coeffs cf)
static number nrzMapMachineInt(number from, const coeffs, const coeffs)
static number nrzCopy(number a, const coeffs)
void s_readmpz_base(s_buff F, mpz_ptr a, int base)