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);
137 if (*a ==
NULL)
return;
138 mpz_clear((mpz_ptr) *a);
147 mpz_init_set(erg, (mpz_ptr) a);
152 number nrzCopyMap(number a,
const coeffs ,
const coeffs dst)
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_ui((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);
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, 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 mpz_init_set_ui(dummy, ch);
487 info.exp = (
unsigned long) 1;
508 PrintS(
"Farey start with ");
629 r->has_simple_Alloc=
FALSE;
630 r->has_simple_Inverse=
FALSE;
634 #elif SI_INTEGER_VARIANT == 3
642 #define nrzTest(A) nrzDBTest(A,__FILE__,__LINE__,NULL)
649 static inline number nrz_short(number
x)
657 mpz_clear((mpz_ptr)
x);
666 long ui=mpz_get_si((mpz_ptr)
x);
667 if ((((ui<<3)>>3)==ui)
668 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
670 mpz_clear((mpz_ptr)
x);
687 if (a ==
NULL)
return 0;
689 if (n_Z_IS_SMALL(a))
return 1;
690 return ((mpz_ptr)a)->_mp_alloc;
699 number _nrzMult(number, number,
const coeffs);
706 number c = _nrzMult(a,
b,
R);
714 number _nrzMult (number a, number
b,
const coeffs R)
719 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b)) {
725 long r=(long)((
unsigned long)(
SR_HDL(a)-1L))*((
unsigned long)(
SR_HDL(
b)>>1));
728 number u=((number) ((r>>1)+
SR_INT));
736 nrzTest((number)erg);
739 else if (n_Z_IS_SMALL(a))
744 mpz_init_set(erg, (mpz_ptr)
b);
746 nrzTest((number)erg);
749 else if (n_Z_IS_SMALL(
b))
754 mpz_init_set(erg, (mpz_ptr) a);
756 nrzTest((number)erg);
763 mpz_mul(erg, (mpz_ptr) a, (mpz_ptr)
b);
764 nrzTest((number)erg);
770 static long int_gcd(
long a,
long b)
797 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
807 mpz_init_set(erg, (mpz_ptr)
b);
811 else if (n_Z_IS_SMALL(
b))
813 mpz_init_set(erg, (mpz_ptr) a);
820 mpz_lcm(erg, (mpz_ptr) a, (mpz_ptr)
b);
828 if (n_Z_IS_SMALL(a))
return a;
830 mpz_init_set(erg, (mpz_ptr) a);
840 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
845 else if (n_Z_IS_SMALL(a))
852 else if (n_Z_IS_SMALL(
b))
863 mpz_gcd(erg, (mpz_ptr) a, (mpz_ptr)
b);
872 static long int_extgcd(
long a,
long b,
long * u,
long*
x,
long *
v,
long*
y)
917 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
934 mpz_init_set(aa, (mpz_ptr) a);
942 mpz_init_set(bb, (mpz_ptr)
b);
950 mpz_gcdext(erg, bs, bt, aa, bb);
951 *
s = nrz_short((number) bs);
952 *t = nrz_short((number) bt);
955 return nrz_short((number) erg);
959 static number _nrzXExtGcd(number, number, number *, number *, number *, number *,
const coeffs);
960 static number
nrzXExtGcd(number a, number
b, number *
x, number *
y, number * u, number *
v,
const coeffs R)
967 number c = _nrzXExtGcd(a,
b,
x,
y, u,
v,
R);
983 static number _nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
985 static number
nrzXExtGcd (number a, number
b, number *
s, number *t, number *u, number *
v,
const coeffs )
988 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1001 if (n_Z_IS_SMALL(a))
1007 mpz_init_set(aa, (mpz_ptr) a);
1009 if (n_Z_IS_SMALL(
b))
1015 mpz_init_set(bb, (mpz_ptr)
b);
1024 mpz_gcdext(erg, bs, bt, aa, bb);
1029 mpz_init_set(bu, (mpz_ptr) bb);
1030 mpz_init_set(bv, (mpz_ptr) aa);
1034 assume(mpz_cmp_si(erg, 0));
1036 mpz_div(bu, bu, erg);
1037 mpz_div(bv, bv, erg);
1040 *u = nrz_short((number) bu);
1041 *
v = nrz_short((number) bv);
1043 *
s = nrz_short((number) bs);
1044 *t = nrz_short((number) bt);
1045 return nrz_short((number) erg);
1049 static number _nrzQuotRem(number, number, number *,
const coeffs);
1056 number c = _nrzQuotRem(a,
b, r,
R);
1069 static number _nrzQuotRem (number a, number
b, number * r,
const coeffs )
1075 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1081 else if (n_Z_IS_SMALL(a))
1088 else if (n_Z_IS_SMALL(
b))
1095 rr = mpz_divmod_ui(qq, rrr, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)));
1104 return nrz_short((number)qq);
1110 mpz_divmod(qq, rr, (mpz_ptr)a, (mpz_ptr)
b);
1117 nrzTest((number)qq);
1126 if (n_Z_IS_SMALL(a))
1129 mpz_init_set(aa, (mpz_ptr) a);
1130 mpz_pow_ui(erg, aa,
i);
1131 *
result = nrz_short((number) erg);
1141 mpz_init_set_si(erg,
i);
1142 return nrz_short((number) erg);
1148 mpz_init_set(erg,
m);
1149 return nrz_short((number) erg);
1155 if (*a ==
NULL)
return;
1156 if (n_Z_IS_SMALL(*a)==0)
1158 mpz_clear((mpz_ptr) *a);
1169 if (n_Z_IS_SMALL(n))
return SR_TO_INT(n);
1170 return mpz_get_si( (mpz_ptr)n);
1173 static number _nrzAdd(number, number,
const coeffs);
1180 number c = _nrzAdd(a,
b,
R);
1188 static number _nrzAdd (number a, number
b,
const coeffs )
1193 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1196 if (INT_IS_SMALL(c))
1199 mpz_init_set_si(erg, c);
1201 nrzTest((number)erg);
1202 return (number) erg;
1204 else if (n_Z_IS_SMALL(a))
1209 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)
SR_TO_INT(a));
1211 mpz_sub_ui(erg, (mpz_ptr)
b, (
unsigned long)-(
SR_TO_INT(a)));
1212 return nrz_short((number) erg);
1214 else if (n_Z_IS_SMALL(
b))
1219 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
1221 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)-(
SR_TO_INT(
b)));
1222 return nrz_short((number) erg);
1228 mpz_add(erg, (mpz_ptr) a, (mpz_ptr)
b);
1229 return nrz_short((number) erg);
1235 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1238 if (INT_IS_SMALL(c))
1241 mpz_init_set_si(erg, c);
1242 nrzTest((number)erg);
1243 return (number) erg;
1245 else if (n_Z_IS_SMALL(a))
1251 mpz_ui_sub(erg, (
unsigned long)
SR_TO_INT(a), (mpz_ptr)
b);
1254 mpz_add_ui(erg, (mpz_ptr)
b, (
unsigned long)-
SR_TO_INT(a));
1257 return nrz_short((number) erg);
1259 else if (n_Z_IS_SMALL(
b))
1264 mpz_sub_ui(erg, (mpz_ptr) a, (
unsigned long)
SR_TO_INT(
b));
1266 mpz_add_ui(erg, (mpz_ptr) a, (
unsigned long)-
SR_TO_INT(
b));
1267 return nrz_short((number) erg);
1273 mpz_sub(erg, (mpz_ptr) a, (mpz_ptr)
b);
1274 return nrz_short((number) erg);
1280 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1281 return ((
long)a)>((long)
b);
1282 else if (n_Z_IS_SMALL(a))
1283 return 0 > mpz_cmp_si((mpz_ptr)
b,
SR_TO_INT(a));
1284 else if (n_Z_IS_SMALL(
b))
1285 return 0 < mpz_cmp_si((mpz_ptr)a,
SR_TO_INT(
b));
1286 return 0 < mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
1302 static number nrzAnn(number n,
const coeffs)
1332 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1334 else if (n_Z_IS_SMALL(a) || n_Z_IS_SMALL(
b))
1337 return 0 == mpz_cmp((mpz_ptr) a, (mpz_ptr)
b);
1342 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1346 else if (n_Z_IS_SMALL(a))
1350 else if (n_Z_IS_SMALL(
b))
1352 return mpz_divisible_ui_p((mpz_ptr)a, (
unsigned long)
ABS(
SR_TO_INT(
b))) != 0;
1355 return mpz_divisible_p((mpz_ptr) a, (mpz_ptr)
b) != 0;
1372 if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(
b))
1381 else if (n_Z_IS_SMALL(a))
1390 else if (n_Z_IS_SMALL(
b))
1396 if (mpz_divmod_ui(erg, r, (mpz_ptr) a, (
unsigned long)
ABS(
SR_TO_INT(
b)))) {
1403 return nrz_short((number) erg);
1409 mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr)
b);
1431 return nrz_short((number) erg);
1438 if (n_Z_IS_SMALL(a))
1441 mpz_init_set(aa, (mpz_ptr) a);
1442 if (n_Z_IS_SMALL(
b))
1445 mpz_init_set(bb, (mpz_ptr)
b);
1448 mpz_tdiv_q(erg, (mpz_ptr) aa, (mpz_ptr) bb);
1451 nrzTest((number)erg);
1452 return (number) erg;
1459 if (n_Z_IS_SMALL(a))
1462 mpz_init_set(aa, (mpz_ptr) a);
1463 if (n_Z_IS_SMALL(
b))
1466 mpz_init_set(bb, (mpz_ptr)
b);
1472 mpz_tdiv_qr(erg, r, (mpz_ptr) aa, (mpz_ptr) bb);
1477 return nrz_short((number) r);
1484 WerrorS(
"Non invertible element.");
1493 if (n_Z_IS_SMALL(c))
1507 PrintS(
"Farey start with ");
1573 mpz_init_set_ui(erg, (
unsigned long) from);
1574 return nrz_short((number) erg);
1580 mpz_init_set_si(erg, (
long) from);
1581 return nrz_short((number) erg);
1584 static number nrzModNMap(number from,
const coeffs ,
const coeffs )
1587 mpz_init_set(erg, (mpz_ptr) from);
1588 return nrz_short((number) erg);
1597 WerrorS(
"rational in map to integer");
1601 mpz_init_set(erg, from->z);
1602 return nrz_short((number) erg);
1654 long ui=mpz_get_si((mpz_ptr)
x);
1655 if ((((ui<<3)>>3)==ui)
1656 && (mpz_cmp_si((mpz_ptr)
x,ui)==0))
1658 Print(
"gmp-small %s:%d\n",
f,
l);
1675 if (n_Z_IS_SMALL(a))
1681 int l=mpz_sizeinbase((mpz_ptr) a, 10) + 2;
1683 z=mpz_get_str(
s,10,(mpz_ptr) a);
1695 const char * start=
s;
1697 if (*s<'0' || *s>
'9')
1702 while (*
s >=
'0' && *
s <=
'9')
s++;
1705 mpz_set_str(
i,start,10);
1711 mpz_set_str(
i,start,10);
1717 static const char *
nrzRead (
const char *
s, number *a,
const coeffs)
1724 *a = nrz_short((number) z);
1731 PrintS(
"// coeff. ring is : Integers\n");
1744 if ( n_Z_IS_SMALL(n))
1751 mpz_init_set( dummy,n->z );
1767 WerrorS(
"rational in conversion to integer");
1772 return nrz_short((number)z);
1776 static void nrzMPZ(mpz_t
res, number &a,
const coeffs)
1778 if (n_Z_IS_SMALL(a))
1781 mpz_init_set(
res, (mpz_ptr) a);
1789 long ch = r->cfInt(c, r);
1790 mpz_init_set_ui(dummy, ch);
1794 mpz_init_set(dummy, (mpz_ptr)c);
1798 info.exp = (
unsigned long) 1;
1867 r->has_simple_Alloc=
FALSE;
1868 r->has_simple_Inverse=
FALSE;
1872 #elif SI_INTEGER_VARIANT == 1
1878 #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_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_Ring_ModN(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
static FORCE_INLINE BOOLEAN nCoeff_is_Ring_Z(const coeffs r)
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 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)
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)
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)
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)
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)
static int nrzSize(number a, const coeffs)
BOOLEAN nrzInitChar(coeffs r, void *)
void nrzSetExp(int, coeffs)
static void nrzDelete(number *a, const 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)
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)