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