Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/symbolic/maxima_wrapper.py
4057 views
1
###############################################################################
2
# Sage: Open Source Mathematical Software
3
# Copyright (C) 2010 Burcin Erocal <[email protected]>
4
# Distributed under the terms of the GNU General Public License (GPL),
5
# version 2 or any later version. The full text of the GPL is available at:
6
# http://www.gnu.org/licenses/
7
###############################################################################
8
9
from sage.structure.sage_object import SageObject
10
from sage.interfaces.maxima import MaximaFunctionElement
11
12
class MaximaFunctionElementWrapper(MaximaFunctionElement):
13
def __call__(self, *args, **kwds):
14
"""
15
Returns a Sage expression instead of a Maxima pexpect interface element.
16
17
EXAMPLES::
18
19
sage: t = sin(x)^2 + cos(x)^2; t
20
sin(x)^2 + cos(x)^2
21
sage: res = t.maxima_methods().trigsimp(); res
22
1
23
sage: type(res)
24
<type 'sage.symbolic.expression.Expression'>
25
"""
26
return super(MaximaFunctionElementWrapper, self).__call__(*args,
27
**kwds).sage()
28
29
class MaximaWrapper(SageObject):
30
def __init__(self, exp):
31
"""
32
Wrapper around Sage expressions to give access to Maxima methods.
33
34
We convert the given expression to Maxima and convert the return value
35
back to a Sage expression. Tab completion and help strings of Maxima
36
methods also work as expected.
37
38
EXAMPLES::
39
40
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t
41
log(sqrt(2) - 1) + log(sqrt(2) + 1)
42
sage: u = t.maxima_methods(); u
43
MaximaWrapper(log(sqrt(2) - 1) + log(sqrt(2) + 1))
44
sage: type(u)
45
<class 'sage.symbolic.maxima_wrapper.MaximaWrapper'>
46
sage: u.logcontract()
47
log((sqrt(2) - 1)*(sqrt(2) + 1))
48
sage: u.logcontract().parent()
49
Symbolic Ring
50
51
TESTS:
52
53
Test tab completions::
54
55
sage: import sagenb.misc.support as s
56
sage: u = t.maxima_methods()
57
sage: s.completions('u.elliptic_',globals(),system='python')
58
['u.elliptic_e', 'u.elliptic_ec', 'u.elliptic_eu', 'u.elliptic_f', 'u.elliptic_kc', 'u.elliptic_pi']
59
"""
60
self._exp = exp
61
self._maxima_exp = None
62
63
def __getattr__(self, s):
64
"""
65
Direct attempts to get attributes of this wrapper to the corresponding
66
Maxima expression. This allows tab completion to work as expected.
67
68
We wrap the function calls in order to convert results back to Sage.
69
70
EXAMPLES::
71
72
sage: t = sin(x)^2 + cos(x)^2; t
73
sin(x)^2 + cos(x)^2
74
sage: u = t.maxima_methods()
75
sage: import sagenb.misc.support as s
76
sage: s.completions('u.airy_',globals(),system='python')
77
['u.airy_ai', 'u.airy_bi', 'u.airy_dai', 'u.airy_dbi']
78
sage: type(u.airy_ai)
79
<class 'sage.symbolic.maxima_wrapper.MaximaFunctionElementWrapper'>
80
sage: u.airy_ai()
81
airy_ai(sin(x)^2 + cos(x)^2)
82
"""
83
if self._maxima_exp is None:
84
self._maxima_exp = self._exp._maxima_()
85
if s == 'trait_names' or s[:1] == '_':
86
return getattr(self._maxima_exp, s)
87
else:
88
# add a wrapper function which converts the result back to
89
# a Sage expression
90
return MaximaFunctionElementWrapper(self._maxima_exp, s)
91
92
def sage(self):
93
"""
94
Return the Sage expression this wrapper corresponds to.
95
96
EXAMPLES::
97
98
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t
99
log(sqrt(2) - 1) + log(sqrt(2) + 1)
100
sage: u = t.maxima_methods().sage()
101
sage: u is t
102
True
103
"""
104
return self._exp
105
106
def _symbolic_(self, parent):
107
"""
108
EXAMPLES::
109
110
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t
111
log(sqrt(2) - 1) + log(sqrt(2) + 1)
112
sage: u = t.maxima_methods()
113
sage: SR(u) is t # indirect doctest
114
True
115
"""
116
return parent(self._exp)
117
118
def __reduce__(self):
119
"""
120
EXAMPLES::
121
122
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t
123
log(sqrt(2) - 1) + log(sqrt(2) + 1)
124
sage: u = t.maxima_methods(); u
125
MaximaWrapper(log(sqrt(2) - 1) + log(sqrt(2) + 1))
126
sage: loads(dumps(u))
127
MaximaWrapper(log(sqrt(2) - 1) + log(sqrt(2) + 1))
128
"""
129
return (MaximaWrapper, (self._exp,))
130
131
def _repr_(self):
132
"""
133
EXAMPLES::
134
135
sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t
136
log(sqrt(2) - 1) + log(sqrt(2) + 1)
137
sage: u = t.maxima_methods(); u
138
MaximaWrapper(log(sqrt(2) - 1) + log(sqrt(2) + 1))
139
sage: u._repr_()
140
'MaximaWrapper(log(sqrt(2) - 1) + log(sqrt(2) + 1))'
141
"""
142
return "MaximaWrapper(%s)"%(self._exp)
143
144