Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interfaces/mwrank.py
4056 views
1
r"""
2
Interface to mwrank
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2005 William Stein <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
import os, weakref
21
from expect import Expect
22
23
instances={}
24
def Mwrank(options="", server=None, server_tmpdir=None):
25
"""
26
Create and return an mwrank interpreter, with given options.
27
28
INPUT:
29
30
- ``options`` - string; passed when starting mwrank.
31
The format is::
32
33
-h help prints this info and quits
34
-q quiet turns OFF banner display and prompt
35
-v n verbosity sets verbosity to n (default=1)
36
-o PARI/GP output turns ON extra PARI/GP short output (default is OFF)
37
-p n precision sets precision to n decimals (default=15)
38
-b n quartic bound bound on quartic point search (default=10)
39
-x n n aux number of aux primes used for sieving (default=6)
40
-l list turns ON listing of points (default ON unless v=0)
41
-s selmer_only if set, computes Selmer rank only (default: not set)
42
-d skip_2nd_descent if set, skips the second descent for curves with 2-torsion (default: not set)
43
-S n sat_bd upper bound on saturation primes (default=100, -1 for automatic)
44
45
.. warning:
46
47
Do not use the option "-q" which turns off the prompt.
48
49
EXAMPLES::
50
51
sage: M = Mwrank('-v 0 -l')
52
sage: print M('0 0 1 -1 0')
53
Curve [0,0,1,-1,0] : Rank = 1
54
Generator 1 is [0:-1:1]; height 0.0511114082399688
55
Regulator = 0.0511114082399688
56
"""
57
global instances
58
try:
59
X = instances[options]()
60
if X:
61
return X
62
except KeyError:
63
pass
64
X = Mwrank_class(options, server=server,server_tmpdir=server_tmpdir)
65
instances[options] = weakref.ref(X)
66
return X
67
68
69
class Mwrank_class(Expect):
70
"""
71
Interface to the Mwrank interpreter.
72
"""
73
def __init__(self, options="", server=None,server_tmpdir=None):
74
"""
75
INPUT:
76
77
78
- ``options`` - string; passed when starting mwrank.
79
The format is::
80
81
-h help prints this info and quits
82
-q quiet turns OFF banner display and prompt
83
-v n verbosity sets verbosity to n (default=1)
84
-o PARI/GP output turns ON extra PARI/GP short output (default is OFF)
85
-p n precision sets precision to n decimals (default=15)
86
-b n quartic bound bound on quartic point search (default=10)
87
-x n n aux number of aux primes used for sieving (default=6)
88
-l list turns ON listing of points (default ON unless v=0)
89
-s selmer_only if set, computes Selmer rank only (default: not set)
90
-d skip_2nd_descent if set, skips the second descent for curves with 2-torsion (default: not set)
91
-S n sat_bd upper bound on saturation primes (default=100, -1 for automatic)
92
93
.. warning:
94
95
Do not use the option "-q" which turns off the prompt.
96
97
98
.. note::
99
100
Normally instances of this class would be created by
101
calling the global function :meth:`Mwrank`.
102
103
EXAMPLES::
104
105
sage: from sage.interfaces.mwrank import Mwrank_class
106
sage: M = Mwrank_class('-v 0 -l')
107
sage: M('0 -1 1 0 0')
108
'Curve [0,-1,1,0,0] : Rank = 0\n\n\nRegulator = 1\n'
109
110
sage: from sage.interfaces.mwrank import Mwrank_class
111
sage: TestSuite(Mwrank_class).run()
112
"""
113
Expect.__init__(self,
114
name = 'mwrank',
115
prompt = 'Enter curve: ',
116
command = "mwrank %s"%options,
117
server = server,
118
server_tmpdir = server_tmpdir,
119
maxread = 10000,
120
restart_on_ctrlc = True,
121
verbose_start = False)
122
123
def __getattr__(self, attrname):
124
"""
125
Standard function to return an attribute.
126
127
EXAMPLES::
128
129
sage: mwrank.zzz
130
Traceback (most recent call last):
131
...
132
AttributeError
133
"""
134
raise AttributeError
135
136
def __reduce__(self):
137
"""
138
EXAMPLES::
139
140
sage: mwrank.__reduce__()
141
(<function _reduce_load_mwrank at 0x...>, ())
142
"""
143
144
return _reduce_load_mwrank, tuple([])
145
146
def __call__(self, cmd):
147
"""
148
Standard call function.
149
150
EXAMPLES::
151
152
sage: s = mwrank("0 0 0 0 1"); print s
153
Curve [0,0,0,0,1] :
154
...
155
Rank = 0
156
...
157
"""
158
return self.eval(str(cmd))
159
160
def eval(self, *args, **kwds):
161
"""
162
Send a line of input to mwrank, then when it finishes return
163
everything that mwrank output.
164
165
.. note::
166
167
If a RuntimeError exception is raised, then the mwrank
168
interface is restarted and the command is retried once.
169
170
EXAMPLES::
171
172
sage: mwrank.eval('12 3 4 5 6')
173
'Curve [12,3,4,5,6] :...'
174
"""
175
if self._expect is not None and not self._expect.isalive():
176
# if mwrank is interrupted twice in rapid succession,
177
# then it doesn't restart correctly, and we're left with:
178
# "RuntimeError: [Errno 9] Bad file descriptor"
179
# Doing _start again fixes that always. See trac #5157.
180
self._start()
181
return Expect.eval(self, *args, **kwds).replace('\t',' ')
182
183
def console(self):
184
"""
185
Start the mwrank console.
186
187
EXAMPLE::
188
189
sage: mwrank.console() # not tested: expects console input
190
Program mwrank: ...
191
192
"""
193
mwrank_console()
194
195
def quit(self, verbose=False):
196
"""
197
Quit the mwrank process using kill -9 (so exit doesn't dump core, etc.).
198
199
INPUT:
200
201
- ``verbose`` -- ignored
202
203
EXAMPLES::
204
205
sage: m = Mwrank()
206
sage: e = m('1 2 3 4 5')
207
sage: m.quit()
208
"""
209
if self._expect is None: return
210
try:
211
os.kill(self._expect.pid, 9)
212
except: pass
213
self._expect = None
214
215
216
# An instance
217
mwrank = Mwrank()
218
219
def _reduce_load_mwrank():
220
"""
221
Return the standard mwrank instance
222
223
EXAMPLES::
224
225
sage: from sage.interfaces.mwrank import _reduce_load_mwrank
226
sage: _reduce_load_mwrank()
227
Mwrank
228
"""
229
return mwrank
230
231
import os
232
def mwrank_console():
233
"""
234
Start the mwrank console.
235
236
EXAMPLE::
237
238
sage: mwrank_console() # not tested: expects console input
239
Program mwrank: ...
240
"""
241
os.system('mwrank')
242
243
244