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

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

from .sage_object cimport SageObject 

from .parent cimport Parent 

from cpython.number cimport PyNumber_Check 

from sage.misc.inherit_comparison cimport InheritComparisonMetaclass 

  

  

cpdef inline parent(x): 

""" 

Return the parent of the element ``x``. 

  

Usually, this means the mathematical object of which ``x`` is an 

element. 

  

INPUT: 

  

- ``x`` -- an element 

  

OUTPUT: 

  

- If ``x`` is a Sage :class:`Element`, return ``x.parent()``. 

  

- Otherwise, return ``type(x)``. 

  

.. SEEALSO:: 

  

`Parents, Conversion and Coercion <http://doc.sagemath.org/html/en/tutorial/tour_coercion.html>`_ 

Section in the Sage Tutorial 

  

EXAMPLES:: 

  

sage: a = 42 

sage: parent(a) 

Integer Ring 

sage: b = 42/1 

sage: parent(b) 

Rational Field 

sage: c = 42.0 

sage: parent(c) 

Real Field with 53 bits of precision 

  

Some more complicated examples:: 

  

sage: x = Partition([3,2,1,1,1]) 

sage: parent(x) 

Partitions 

sage: v = vector(RDF, [1,2,3]) 

sage: parent(v) 

Vector space of dimension 3 over Real Double Field 

  

The following are not considered to be elements, so the type is 

returned:: 

  

sage: d = int(42) # Python int 

sage: parent(d) 

<... 'int'> 

sage: L = list(range(10)) 

sage: parent(L) 

<... 'list'> 

""" 

if isinstance(x, Element): 

return (<Element>x)._parent 

return type(x) 

  

  

cdef inline int classify_elements(left, right): 

""" 

Given two objects, at least one which is an :class:`Element`, 

classify their type and parent. This is a finer version of 

:func:`have_same_parent`. 

  

OUTPUT: the sum of the following bits: 

  

- 0o01: left is an Element 

- 0o02: right is an Element 

- 0o04: both are Element 

- 0o10: left and right have the same type 

- 0o20: left and right have the same parent 

  

These are the possible outcomes: 

  

- 0o01: left is an Element, right is not 

- 0o02: right is an Element, left is not 

- 0o07: both are Element, different types, different parents 

- 0o17: both are Element, same type, different parents 

- 0o27: both are Element, different types, same parent 

- 0o37: both are Element, same type, same parent 

""" 

if type(left) is type(right): 

# We know at least one of the arguments is an Element. So if 

# their types are *equal* (fast to check) then they are both 

# Elements. 

if (<Element>left)._parent is (<Element>right)._parent: 

return 0o37 

else: 

return 0o17 

if not isinstance(right, Element): 

return 0o01 

if not isinstance(left, Element): 

return 0o02 

if (<Element>left)._parent is (<Element>right)._parent: 

return 0o27 

else: 

return 0o07 

  

# Functions to help understand the result of classify_elements() 

cdef inline bint BOTH_ARE_ELEMENT(int cl): 

return cl & 0o04 

cdef inline bint HAVE_SAME_PARENT(int cl): 

return cl & 0o20 

  

  

cpdef inline bint have_same_parent(left, right): 

""" 

Return ``True`` if and only if ``left`` and ``right`` have the 

same parent. 

  

.. WARNING:: 

  

This function assumes that at least one of the arguments is a 

Sage :class:`Element`. When in doubt, use the slower 

``parent(left) is parent(right)`` instead. 

  

EXAMPLES:: 

  

sage: from sage.structure.element import have_same_parent 

sage: have_same_parent(1, 3) 

True 

sage: have_same_parent(1, 1/2) 

False 

sage: have_same_parent(gap(1), gap(1/2)) 

True 

  

These have different types but the same parent:: 

  

sage: a = RLF(2) 

sage: b = exp(a) 

sage: type(a) 

<... 'sage.rings.real_lazy.LazyWrapper'> 

sage: type(b) 

<... 'sage.rings.real_lazy.LazyNamedUnop'> 

sage: have_same_parent(a, b) 

True 

""" 

return HAVE_SAME_PARENT(classify_elements(left, right)) 

  

  

cdef inline parent_c(x): 

""" 

Deprecated alias for :func:`parent`. 

  

TESTS:: 

  

sage: cython(''' 

....: from sage.structure.element cimport parent_c 

....: from sage.all import ZZ 

....: print(parent_c(ZZ.one())) 

....: ''') 

doctest:...: 

DeprecationWarning: parent_c() is deprecated, use parent() instead 

See http://trac.sagemath.org/22311 for details. 

Integer Ring 

""" 

from sage.misc.superseded import deprecation 

deprecation(22311, "parent_c() is deprecated, use parent() instead") 

return parent(x) 

  

  

cdef unary_op_exception(op, x) 

cdef bin_op_exception(op, x, y) 

  

  

cdef class Element(SageObject): 

cdef Parent _parent 

cpdef _richcmp_(left, right, int op) 

cpdef int _cmp_(left, right) except -2 

cpdef base_extend(self, R) 

  

cdef getattr_from_category(self, name) 

  

cpdef _act_on_(self, x, bint self_on_left) 

cpdef _acted_upon_(self, x, bint self_on_left) 

  

cdef _add_(self, other) 

cdef _sub_(self, other) 

cdef _neg_(self) 

cdef _add_long(self, long n) 

  

cdef _mul_(self, other) 

cdef _mul_long(self, long n) 

cdef _div_(self, other) 

cdef _floordiv_(self, other) 

cdef _mod_(self, other) 

  

cdef _pow_(self, other) 

cdef _pow_int(self, n) 

cdef _pow_long(self, long n) 

  

  

cdef class ElementWithCachedMethod(Element): 

cdef public dict __cached_methods 

  

cdef class ModuleElement(Element) # forward declaration 

  

cdef class RingElement(ModuleElement) # forward declaration 

  

cdef class ModuleElement(Element): 

cpdef _add_(self, other) 

cpdef _sub_(self, other) 

cpdef _neg_(self) 

  

# self._rmul_(x) is x * self 

cpdef _lmul_(self, Element right) 

# self._lmul_(x) is self * x 

cpdef _rmul_(self, Element left) 

  

cdef class MonoidElement(Element): 

cpdef _pow_int(self, n) 

  

cdef class MultiplicativeGroupElement(MonoidElement): 

cpdef _div_(self, other) 

  

cdef class AdditiveGroupElement(ModuleElement): 

pass 

  

cdef class RingElement(ModuleElement): 

cpdef _mul_(self, other) 

cpdef _div_(self, other) 

cpdef _pow_int(self, n) 

  

cdef class CommutativeRingElement(RingElement): 

pass 

  

cdef class IntegralDomainElement(CommutativeRingElement): 

pass 

  

cdef class DedekindDomainElement(IntegralDomainElement): 

pass 

  

cdef class PrincipalIdealDomainElement(DedekindDomainElement): 

pass 

  

cdef class EuclideanDomainElement(PrincipalIdealDomainElement): 

cpdef _floordiv_(self, other) 

cpdef _mod_(self, other) 

  

cdef class FieldElement(CommutativeRingElement): 

cpdef _floordiv_(self, other) 

  

cdef class AlgebraElement(RingElement): 

pass 

  

cdef class CommutativeAlgebraElement(CommutativeRingElement): 

pass 

  

cdef class InfinityElement(RingElement): 

pass 

  

  

cdef class Vector(ModuleElement): 

cdef Py_ssize_t _degree 

  

# Return the dot product using the simple metric 

# $e_i \cdot e_j = \delta_{ij}$. The first assumes that the parents 

# are equal, both assume that the degrees are equal. 

cpdef _dot_product_(Vector left, Vector right) 

cpdef _dot_product_coerce_(Vector left, Vector right) 

  

cpdef _pairwise_product_(Vector left, Vector right) # override, call if parents the same 

  

cdef bint is_sparse_c(self) 

cdef bint is_dense_c(self) 

  

  

cdef class Matrix(ModuleElement): 

# All matrix classes must be written in Cython 

cdef Py_ssize_t _nrows 

cdef Py_ssize_t _ncols 

  

cdef _vector_times_matrix_(matrix_right, Vector vector_left) # OK to override, AND call directly 

cdef _matrix_times_vector_(matrix_left, Vector vector_right) # OK to override, AND call directly 

cdef _matrix_times_matrix_(left, Matrix right) # OK to override, AND call directly 

  

cdef bint is_sparse_c(self) 

cdef bint is_dense_c(self) 

  

  

cdef class CoercionModel: 

cpdef canonical_coercion(self, x, y) 

cpdef bin_op(self, x, y, op) 

cpdef richcmp(self, x, y, int op) 

  

cdef CoercionModel coercion_model