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

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

"Operands" 

  

#***************************************************************************** 

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

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

#***************************************************************************** 

  

from sage.libs.pynac.pynac cimport GEx 

from sage.symbolic.expression cimport new_Expression_from_GEx 

  

cdef inline int normalize_index(object arg, int nops, object err_msg) except -1: 

""" 

Given an index ``arg`` and the number of operands ``nops`` return 

an integer between 0 and ``nops`` which will be used as the index to fetch 

the data from the underlying vector. 

  

TESTS:: 

  

sage: from sage.symbolic.getitem import normalize_index_for_doctests as normalize_index 

sage: normalize_index(-1, 4) 

3 

sage: normalize_index(1, 4) 

1 

sage: normalize_index(1.5, 4) 

Traceback (most recent call last): 

... 

TypeError: some error message 

sage: normalize_index(-5, 4) 

Traceback (most recent call last): 

... 

IndexError: operand index out of range, got -5, expect between -4 and 3 

sage: normalize_index(5, 4) 

Traceback (most recent call last): 

... 

IndexError: operand index out of range, got 5, expect between -4 and 3 

""" 

cdef int i 

i = arg 

if i != arg: 

raise TypeError(err_msg) 

if i < 0: 

i = nops + i 

if i >= nops or i < 0: 

raise IndexError("operand index out of range, got %s, expect between %s and %s"%(arg,-nops,nops-1)) 

return i 

  

def normalize_index_for_doctests(arg, nops): 

""" 

Wrapper function to test ``normalize_index``. 

  

TESTS:: 

  

sage: from sage.symbolic.getitem import normalize_index_for_doctests 

sage: normalize_index_for_doctests(-1, 4) 

3 

""" 

return normalize_index(arg, nops, "some error message") 

  

cdef class OperandsWrapper(SageObject): 

""" 

Operands wrapper for symbolic expressions. 

  

EXAMPLES:: 

  

sage: x,y,z = var('x,y,z') 

sage: e = x + x*y + z^y + 3*y*z; e 

x*y + 3*y*z + x + z^y 

sage: e.op[1] 

3*y*z 

sage: e.op[1,1] 

z 

sage: e.op[-1] 

z^y 

sage: e.op[1:] 

[3*y*z, x, z^y] 

sage: e.op[:2] 

[x*y, 3*y*z] 

sage: e.op[-2:] 

[x, z^y] 

sage: e.op[:-2] 

[x*y, 3*y*z] 

sage: e.op[-5] 

Traceback (most recent call last): 

... 

IndexError: operand index out of range, got -5, expect between -4 and 3 

sage: e.op[5] 

Traceback (most recent call last): 

... 

IndexError: operand index out of range, got 5, expect between -4 and 3 

sage: e.op[1,1,0] 

Traceback (most recent call last): 

... 

TypeError: expressions containing only a numeric coefficient, constant or symbol have no operands 

sage: e.op[:1.5] 

Traceback (most recent call last): 

... 

TypeError: slice indices must be integers or None or have an __index__ method 

sage: e.op[:2:1.5] 

Traceback (most recent call last): 

... 

ValueError: step value must be an integer 

""" 

def __getitem__(self, arg): 

""" 

TESTS:: 

  

sage: t = 1+x+x^2 

sage: t.op[1:] 

[x, 1] 

""" 

cdef int ind, nops 

cdef int bind, eind, step 

cdef GEx cur_ex 

if isinstance(arg, slice): 

nops = self._expr._gobj.nops() 

  

slice_err_msg = "slice indices must be integers or None or have an __index__ method" 

if arg.start: 

bind = normalize_index(arg.start, nops, slice_err_msg) 

else: 

bind = 0 

if arg.stop: 

eind = normalize_index(arg.stop, nops, slice_err_msg) 

else: 

eind = nops 

if arg.step: 

step = arg.step 

if step != arg.step: 

raise ValueError("step value must be an integer") 

else: 

step = 1 

return [new_Expression_from_GEx(self._expr._parent, 

self._expr._gobj.op(ind)) for ind in xrange(bind, eind, step)] 

  

  

ind_err_msg = "index should either be a slice object, an integer or a list of integers" 

if isinstance(arg, (list, tuple)): 

# handle nested index 

if len(arg) == 0: # or not all(lambda x: x in ZZ for t in args): 

raise TypeError(ind_err_msg) 

cur_ex = GEx(self._expr._gobj) 

for x in arg: 

nops = cur_ex.nops() 

if nops == 0: 

raise TypeError("expressions containing only a numeric coefficient, constant or symbol have no operands") 

ind = normalize_index(x, nops, ind_err_msg) 

cur_ex = cur_ex.op(ind) 

return new_Expression_from_GEx(self._expr._parent, cur_ex) 

  

ind = normalize_index(arg, self._expr._gobj.nops(), ind_err_msg) 

return new_Expression_from_GEx(self._expr._parent, 

self._expr._gobj.op(ind)) 

  

def _repr_(self): 

""" 

TESTS:: 

  

sage: (x^2).op 

Operands of x^2 

""" 

return "Operands of %s"%(self._expr) 

  

def _latex_(self): 

r""" 

TESTS:: 

  

sage: latex((x^2).op) 

\text{Operands wrapper for expression }x^{2} 

""" 

return r"\text{Operands wrapper for expression }%s"%(self._expr._latex_()) 

  

def __reduce__(self): 

""" 

TESTS:: 

  

sage: (x^2).op.__reduce__() 

(<built-in function restore_op_wrapper>, (x^2,)) 

sage: loads(dumps((x^2).op)) 

Operands of x^2 

""" 

return restore_op_wrapper, (self._expr,) 

  

def restore_op_wrapper(expr): 

""" 

TESTS:: 

  

sage: from sage.symbolic.getitem import restore_op_wrapper 

sage: restore_op_wrapper(x^2) 

Operands of x^2 

""" 

return expr.op