Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interfaces/magma_free.py
4069 views
1
#*****************************************************************************
2
# Copyright (C) 2007 William Stein <[email protected]>
3
#
4
# Distributed under the terms of the GNU General Public License (GPL)
5
#
6
# This code is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
# General Public License for more details.
10
#
11
# The full text of the GPL is available at:
12
#
13
# http://www.gnu.org/licenses/
14
#*****************************************************************************
15
16
class MagmaExpr(str):
17
def __repr__(self):
18
return str(self)
19
20
def magma_free_eval(code, strip=True, columns=0):
21
"""
22
Use the free online MAGMA calculator to evaluate the given
23
input code and return the answer as a string.
24
25
LIMITATIONS: The code must evaluate in at most 20 seconds
26
and there is a limitation on the amount of RAM.
27
28
EXAMPLES:
29
sage: magma_free("Factorization(9290348092384)") # optional - internet
30
[ <2, 5>, <290323377887, 1> ]
31
"""
32
import urllib, httplib
33
from xml.dom.minidom import parseString
34
from string import join
35
36
server = "magma.maths.usyd.edu.au"
37
processPath = "/xml/calculator.xml"
38
refererPath = "/calc/"
39
refererUrl = "http://%s%s" % ( server, refererPath)
40
code = "SetColumns(%s);\n"%columns + code
41
params = urllib.urlencode({'input':code})
42
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"Accept: text/html, application/xml, application/xhtml+xml", "Referer": refererUrl}
43
conn = httplib.HTTPConnection(server)
44
conn.request("POST", processPath, params, headers)
45
response = conn.getresponse()
46
results = response.read()
47
conn.close()
48
49
xmlDoc = parseString(results)
50
res = []
51
resultsNodeList = xmlDoc.getElementsByTagName('results')
52
if len(resultsNodeList) > 0:
53
resultsNode = resultsNodeList[0]
54
lines = resultsNode.getElementsByTagName('line')
55
for line in lines:
56
for textNode in line.childNodes:
57
res.append(textNode.data)
58
res = join(res, "\n")
59
60
class MagmaExpr(str):
61
def __repr__(self):
62
return str(self)
63
return MagmaExpr(res)
64
65
class MagmaFree:
66
"""
67
Evaluate MAGMA code without requiring that MAGMA be installed
68
on your computer by using the free online MAGMA calculator.
69
70
EXAMPLES:
71
sage: magma_free("Factorization(9290348092384)") # optional - internet
72
[ <2, 5>, <290323377887, 1> ]
73
"""
74
def eval(self, x, **kwds):
75
return magma_free_eval(x)
76
def __call__(self, code, strip=True, columns=0):
77
return magma_free_eval(code, strip=strip, columns=columns)
78
79
magma_free = MagmaFree()
80
81