Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

"Access to Maxima methods" 

 

############################################################################### 

# Sage: Open Source Mathematical Software 

# Copyright (C) 2010 Burcin Erocal <burcin@erocal.org> 

# Distributed under the terms of the GNU General Public License (GPL), 

# version 2 or any later version. The full text of the GPL is available at: 

# http://www.gnu.org/licenses/ 

############################################################################### 

 

from sage.structure.sage_object import SageObject 

from sage.interfaces.maxima import MaximaFunctionElement 

from sage.docs.instancedoc import instancedoc 

 

 

@instancedoc 

class MaximaFunctionElementWrapper(MaximaFunctionElement): 

def __call__(self, *args, **kwds): 

""" 

Return a Sage expression instead of a Maxima pexpect interface element. 

 

EXAMPLES:: 

 

sage: t = sin(x)^2 + cos(x)^2; t 

cos(x)^2 + sin(x)^2 

sage: res = t.maxima_methods().trigsimp(); res 

1 

sage: parent(res) 

Symbolic Ring 

""" 

return super(MaximaFunctionElementWrapper, self).__call__(*args, 

**kwds).sage() 

 

class MaximaWrapper(SageObject): 

def __init__(self, exp): 

""" 

Wrapper around Sage expressions to give access to Maxima methods. 

 

We convert the given expression to Maxima and convert the return value 

back to a Sage expression. Tab completion and help strings of Maxima 

methods also work as expected. 

 

EXAMPLES:: 

 

sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t 

log(sqrt(2) + 1) + log(sqrt(2) - 1) 

sage: u = t.maxima_methods(); u 

MaximaWrapper(log(sqrt(2) + 1) + log(sqrt(2) - 1)) 

sage: type(u) 

<class 'sage.symbolic.maxima_wrapper.MaximaWrapper'> 

sage: u.logcontract() 

log((sqrt(2) + 1)*(sqrt(2) - 1)) 

sage: u.logcontract().parent() 

Symbolic Ring 

 

TESTS: 

 

Test tab completions:: 

 

sage: import sagenb.misc.support as s 

sage: u = t.maxima_methods() 

sage: s.completions('u.elliptic_',globals(),system='python') 

['u.elliptic_e', 'u.elliptic_ec', 'u.elliptic_eu', 'u.elliptic_f', 'u.elliptic_kc', 'u.elliptic_pi'] 

""" 

self._exp = exp 

self._maxima_exp = None 

 

def __getattr__(self, s): 

""" 

Direct attempts to get attributes of this wrapper to the corresponding 

Maxima expression. This allows tab completion to work as expected. 

 

We wrap the function calls in order to convert results back to Sage. 

 

EXAMPLES:: 

 

sage: t = sin(x)^2 + cos(x)^2; t 

cos(x)^2 + sin(x)^2 

sage: u = t.maxima_methods() 

sage: import sagenb.misc.support as s 

sage: s.completions('u.airy_',globals(),system='python') 

['u.airy_ai', 'u.airy_bi', 'u.airy_dai', 'u.airy_dbi'] 

sage: type(u.airy_ai) 

<class 'sage.symbolic.maxima_wrapper.MaximaFunctionElementWrapper'> 

sage: u.airy_ai() 

airy_ai(cos(x)^2 + sin(x)^2) 

""" 

if self._maxima_exp is None: 

self._maxima_exp = self._exp._maxima_() 

if s[0] == '_': 

return getattr(self._maxima_exp, s) 

if s == 'trait_names': # SageNB backward compatibility 

return self._maxima_()._tab_completion 

else: 

# add a wrapper function which converts the result back to 

# a Sage expression 

return MaximaFunctionElementWrapper(self._maxima_exp, s) 

 

def sage(self): 

""" 

Return the Sage expression this wrapper corresponds to. 

 

EXAMPLES:: 

 

sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t 

log(sqrt(2) + 1) + log(sqrt(2) - 1) 

sage: u = t.maxima_methods().sage() 

sage: u is t 

True 

""" 

return self._exp 

 

def _symbolic_(self, parent): 

""" 

EXAMPLES:: 

 

sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t 

log(sqrt(2) + 1) + log(sqrt(2) - 1) 

sage: u = t.maxima_methods() 

sage: u._symbolic_(SR) is t 

True 

sage: SR(u) is t # indirect doctest 

True 

""" 

return parent(self._exp) 

 

def __reduce__(self): 

""" 

EXAMPLES:: 

 

sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t 

log(sqrt(2) + 1) + log(sqrt(2) - 1) 

sage: u = t.maxima_methods(); u 

MaximaWrapper(log(sqrt(2) + 1) + log(sqrt(2) - 1)) 

sage: loads(dumps(u)) 

MaximaWrapper(log(sqrt(2) + 1) + log(sqrt(2) - 1)) 

""" 

return (MaximaWrapper, (self._exp,)) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: t = log(sqrt(2) - 1) + log(sqrt(2) + 1); t 

log(sqrt(2) + 1) + log(sqrt(2) - 1) 

sage: u = t.maxima_methods(); u 

MaximaWrapper(log(sqrt(2) + 1) + log(sqrt(2) - 1)) 

sage: u._repr_() 

'MaximaWrapper(log(sqrt(2) + 1) + log(sqrt(2) - 1))' 

""" 

return "MaximaWrapper(%s)"%(self._exp)