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