Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/mwrank/wrap.cc
4058 views
1
#include <iostream>
2
#include <sstream>
3
using namespace std;
4
5
#include "wrap.h"
6
7
#include "eclib/htconst.h"
8
#include "eclib/interface.h"
9
10
/**************** Miscellaneous functions ****************/
11
12
long mwrank_get_precision()
13
{
14
return decimal_precision();
15
}
16
17
void mwrank_set_precision(long n)
18
{
19
set_precision(n);
20
/*
21
Conversion from base 10 to base 2 and back is done within eclib by
22
the functions n --> int(3.33*n) and n --> int(0.3*n) which
23
introduces rounding errors. Without the following loop, doing
24
set_precision(get_precision()) reduces the decimal precision by at
25
least 1 (exactly 1 for n<803), which has disastrous effects if done
26
repeatedly.
27
*/
28
long m=n;
29
while (decimal_precision()<m)
30
{n++; set_precision(n);}
31
}
32
33
void mwrank_initprimes(char* pfilename, int verb)
34
{
35
initprimes((const char*)pfilename, verb);
36
}
37
38
39
char* stringstream_to_char(ostringstream& instore) {
40
int n = strlen(instore.str().data());
41
char* buf = (char*)malloc(n+1);
42
strcpy(buf, instore.str().data());
43
return buf;
44
}
45
46
//////// bigint //////////
47
48
bigint* new_bigint() {
49
return new bigint();
50
}
51
52
void del_bigint(bigint* n) {
53
delete n;
54
}
55
56
bigint* str_to_bigint(char* s) {
57
istringstream *out = new istringstream(s);
58
bigint* y = new bigint();
59
*out >> *y;
60
delete out;
61
return y;
62
}
63
64
char* bigint_to_str(bigint* x)
65
{
66
ostringstream instore;
67
instore << (*x);
68
return stringstream_to_char(instore);
69
}
70
71
72
//////// Curvedata //////////
73
74
struct Curvedata* Curvedata_new(const struct bigint* a1, const struct bigint* a2,
75
const struct bigint* a3, const struct bigint* a4,
76
const struct bigint* a6, int min_on_init)
77
{
78
return new Curvedata(*a1, *a2, *a3, *a4, *a6, min_on_init);
79
}
80
81
void Curvedata_del(struct Curvedata* curve)
82
{
83
delete curve;
84
}
85
86
87
char* Curvedata_repr(struct Curvedata* curve)
88
{
89
ostringstream instore;
90
instore << (*curve);
91
return stringstream_to_char(instore);
92
}
93
94
double Curvedata_silverman_bound(const struct Curvedata* curve)
95
{
96
return silverman_bound(*curve);
97
}
98
99
double Curvedata_cps_bound(const struct Curvedata* curve)
100
{
101
return cps_bound(*curve);
102
}
103
104
double Curvedata_height_constant(const struct Curvedata* curve)
105
{
106
return height_constant(*curve);
107
}
108
109
char* Curvedata_getdiscr(struct Curvedata* curve)
110
{
111
ostringstream instore;
112
instore << getdiscr(*curve);
113
return stringstream_to_char(instore);
114
}
115
116
char* Curvedata_conductor(struct Curvedata* curve)
117
{
118
CurveRed E(*curve);
119
ostringstream instore;
120
instore << getconductor(E);
121
return stringstream_to_char(instore);
122
}
123
124
char* Curvedata_isogeny_class(struct Curvedata* E, int verbose)
125
{
126
//copied from allisog.cc
127
ostringstream instore;
128
CurveRed C;
129
C = CurveRed(*E);
130
IsogenyClass cl(C, verbose);
131
cl.grow();
132
vector<CurveRed> crs=cl.getcurves();
133
vector<Curve> cs;
134
for(unsigned int i=0; i<crs.size(); i++) cs.push_back((Curve)(crs[i]));
135
136
//copied from point_vector_to_str
137
// TODO: Would just use instore << cs, but it's buggy and printouts to stdout!
138
instore << "([";
139
for (unsigned int i=0; i<cs.size(); i++) {
140
instore << cs[i];
141
if (i+1 < cs.size())
142
instore << ", ";
143
}
144
instore << "], ";
145
146
instore << cl.getmatrix() << ")";
147
return stringstream_to_char(instore);
148
}
149
150
//////// mw //////////
151
152
153
struct mw* mw_new(struct Curvedata* curve, int verb, int pp, int maxr)
154
{
155
return new mw(curve, verb, pp, maxr);
156
}
157
158
void mw_del(struct mw* m)
159
{
160
delete m;
161
}
162
163
int mw_process(struct Curvedata* curve, struct mw* m,
164
const struct bigint* x, const struct bigint* y,
165
const struct bigint* z, int sat)
166
{
167
Point P(*curve, *x, *y, *z);
168
if (!P.isvalid())
169
return 1;
170
m->process(P, sat);
171
return 0;
172
}
173
174
char* point_vector_to_str(const vector<Point>& v)
175
{
176
ostringstream instore;
177
instore << "[";
178
for (unsigned int i=0; i<v.size(); i++) {
179
instore << v[i];
180
if (i+1 < v.size())
181
instore << ", ";
182
}
183
instore << "]";
184
return stringstream_to_char(instore);
185
}
186
187
char* p2point_vector_to_str(const vector<P2Point>& v)
188
{
189
ostringstream instore;
190
instore << "[";
191
for (unsigned int i=0; i<v.size(); i++) {
192
instore << v[i];
193
if (i+1 < v.size())
194
instore << ", ";
195
}
196
instore << "]";
197
return stringstream_to_char(instore);
198
}
199
200
char* mw_getbasis(struct mw* m)
201
{
202
return point_vector_to_str(m->getbasis());
203
}
204
205
char* mw_regulator(struct mw* m)
206
{
207
bigfloat reg = m->regulator();
208
ostringstream instore;
209
instore << reg;
210
return stringstream_to_char(instore);
211
}
212
213
int mw_rank(struct mw* m)
214
{
215
return m->getrank();
216
}
217
218
/* Returns index and unsat long array, which user must deallocate */
219
int mw_saturate(struct mw* m, struct bigint* index, char** unsat,
220
long sat_bd, int odd_primes_only)
221
{
222
vector<long> v;
223
int s = m->saturate(*index, v, sat_bd, odd_primes_only);
224
ostringstream instore;
225
instore << v;
226
*unsat = stringstream_to_char(instore);
227
return s;
228
}
229
230
bigfloat str_to_bigfloat(char* s) {
231
istringstream *out = new istringstream(s);
232
bigfloat y;
233
*out >> y;
234
delete out;
235
return y;
236
}
237
238
void mw_search(struct mw* m, char* h_lim, int moduli_option, int verb)
239
{
240
241
m->search(str_to_bigfloat(h_lim), moduli_option, verb);
242
}
243
244
245
246
247
//////// two_descent //////////
248
249
struct two_descent* two_descent_new(struct Curvedata* curve, \
250
int verb, int sel,
251
long firstlim, long secondlim,
252
long n_aux, int second_descent)
253
{
254
return new two_descent(curve, verb, sel, firstlim, secondlim, n_aux, second_descent);
255
}
256
257
void two_descent_del(struct two_descent* t)
258
{
259
delete t;
260
}
261
262
long two_descent_get_rank(struct two_descent* t)
263
{
264
return t->getrank();
265
}
266
267
long two_descent_get_rank_bound(struct two_descent* t)
268
{
269
return t->getrankbound();
270
}
271
272
long two_descent_get_selmer_rank(struct two_descent* t)
273
{
274
return t->getselmer();
275
}
276
277
char* two_descent_get_basis(struct two_descent* t)
278
{
279
return p2point_vector_to_str(t->getbasis());
280
}
281
282
int two_descent_ok(const struct two_descent* t)
283
{
284
return t->ok();
285
}
286
287
long two_descent_get_certain(const struct two_descent* t)
288
{
289
return t->getcertain();
290
}
291
292
void two_descent_saturate(struct two_descent* t, long sat_bd)
293
{
294
t->saturate(sat_bd);
295
}
296
297
char* two_descent_regulator(struct two_descent* t)
298
{
299
bigfloat reg = t->regulator();
300
ostringstream instore;
301
instore << reg;
302
return stringstream_to_char(instore);
303
}
304
305