Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/libs/pari/misc.h
8817 views
1
/*****************************************
2
PARI misc macros and functions
3
*****************************************/
4
#ifndef SAGE_LIBS_PARI_MISC_H
5
#define SAGE_LIBS_PARI_MISC_H
6
7
#include <pari/pari.h>
8
9
10
inline int strcmp_to_cmp(int f) {
11
if (f > 0) {
12
return 1;
13
} else if (f) {
14
return -1;
15
} else {
16
return 0;
17
}
18
}
19
20
inline int
21
gcmp_sage(GEN x, GEN y)
22
{
23
long tx = typ(x), ty = typ(y), f;
24
GEN tmp;
25
pari_sp av;
26
27
if (is_intreal_t(tx) && is_intreal_t(ty)) {
28
/* Compare two numbers that can be considered as reals. */
29
return mpcmp(x,y);
30
}
31
32
/***** comparing strings *****/
33
if (tx==t_STR) {
34
/* Compare two strings */
35
if (ty != t_STR) {
36
return 1;
37
}
38
39
return strcmp_to_cmp(strcmp(GSTR(x),GSTR(y)));
40
}
41
if (ty == t_STR) /* tx is not a string */
42
return -1;
43
/***** end comparing strings *****/
44
45
/*if (!is_intreal_t(ty) && ty != t_FRAC) */
46
/* return 1; */
47
/* pari_err(typeer,"comparison"); */
48
49
av = avma;
50
char *c, *d;
51
c = GENtostr(x);
52
d = GENtostr(y);
53
f = strcmp_to_cmp(strcmp(c, d));
54
free(c);
55
free(d);
56
avma = av;
57
return f;
58
59
/*
60
av = avma;
61
y = gneg_i(y);
62
tmp = gadd(x,y);
63
switch(typ(tmp)) {
64
case t_INT:
65
case t_REAL:
66
return signe(tmp);
67
case t_FRAC:
68
return signe(tmp[1]);
69
}
70
avma = av;
71
*/
72
}
73
74
75
int factorint_withproof_sage(GEN* ans, GEN x, GEN cutoff) {
76
/*
77
Factors and proves that the prime factors are really prime.
78
If any aren't an ERROR condition (signal) is raised.
79
80
INPUT:
81
x -- a t_INT
82
cutoff -- only check for primality of numbers at least this large.
83
*/
84
85
GEN F = factorint(x, 0);
86
*ans = F;
87
88
long i, l;
89
if (lg(F) == 1) return F; // x = 1
90
F = gel(F,1); l = lg(F);
91
for (i = 1; i < l; i++) {
92
GEN p = gel(F,i);
93
if (mpcmp(p, cutoff) > 0 && !isprime(p)) {
94
char *c, *d;
95
c = GENtostr(x);
96
d = GENtostr(p);
97
fprintf(stderr, "***\nPARI's factor(%s): Found composite pseudoprime %s (very rare and exciting -- PLEASE REPORT!!)\n***\n",
98
c, d);
99
fprintf(stderr, "Do not worry, SAGE will further factor the number until each factor is proven prime.\n");
100
free(c);
101
free(d);
102
return 1;
103
}
104
}
105
return 0;
106
}
107
108
#endif /* SAGE_LIBS_PARI_MISC_H */
109
110