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

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

""" 

Element Wrapper 

  

Wrapping Sage or Python objects as Sage elements. 

  

AUTHORS: 

  

- Nicolas Thiery (2008-2010): Initial version 

- Travis Scrimshaw (2013-05-04): Cythonized version 

""" 

  

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

# Copyright (C) 2008-2010 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

# 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 cpython.object cimport Py_EQ, Py_NE, Py_LE, Py_GE 

  

from sage.structure.parent cimport Parent 

from sage.structure.element cimport Element, coercion_model 

from copy import copy 

  

cdef class ElementWrapper(Element): 

r""" 

A class for wrapping Sage or Python objects as Sage elements. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent = DummyParent("A parent") 

sage: o = ElementWrapper(parent, "bla"); o 

'bla' 

sage: isinstance(o, sage.structure.element.Element) 

True 

sage: o.parent() 

A parent 

sage: o.value 

'bla' 

  

Note that ``o`` is not *an instance of* ``str``, but rather 

*contains a* ``str``. Therefore, ``o`` does not inherit the string 

methods. On the other hand, it is provided with reasonable default 

implementations for equality testing, hashing, etc. 

  

The typical use case of ``ElementWrapper`` is for trivially 

constructing new element classes from pre-existing Sage or Python 

classes, with a containment relation. Here we construct the 

tropical monoid of integers endowed with ``min`` as 

multiplication. There, it is desirable *not* to inherit the 

``factor`` method from ``Integer``:: 

  

sage: class MinMonoid(Parent): 

....: def _repr_(self): 

....: return "The min monoid" 

....: 

sage: M = MinMonoid() 

sage: class MinMonoidElement(ElementWrapper): 

....: wrapped_class = Integer 

....: 

....: def __mul__(self, other): 

....: return MinMonoidElement(self.parent(), min(self.value, other.value)) 

sage: x = MinMonoidElement(M, 5); x 

5 

sage: x.parent() 

The min monoid 

sage: x.value 

5 

sage: y = MinMonoidElement(M, 3) 

sage: x * y 

3 

  

This example was voluntarily kept to a bare minimum. See the 

examples in the categories (e.g. ``Semigroups().example()``) for 

several full featured applications. 

  

.. WARNING:: 

  

Versions before :trac:`14519` had parent as the second argument and 

the value as the first. 

""" 

def __init__(self, parent, value): 

""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: a = ElementWrapper(DummyParent("A parent"), 1) 

  

TESTS:: 

  

sage: TestSuite(a).run(skip = "_test_category") 

  

sage: a = ElementWrapper(1, DummyParent("A parent")) 

doctest:...: DeprecationWarning: the first argument must be a parent 

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

  

.. NOTE:: 

  

:class:`ElementWrapper` is not intended to be used directly, 

hence the failing category test. 

""" 

#assert isinstance(value, self.wrapped_class) 

if not isinstance(parent, Parent): 

from sage.misc.superseded import deprecation 

deprecation(14519, 'the first argument must be a parent') 

value, parent = parent, value 

Element.__init__(self, parent=parent) 

self.value = value 

  

def __getstate__(self): 

""" 

Return a tuple describing the state of your object. 

  

This emulates :meth:`Element.__getstate__`, playing as if 

``self.value`` was in the dictionary of ``self`` as it used to 

be before :trac:`14519`. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: P = DummyParent("A parent") 

sage: a = ElementWrapper(P, 1) 

sage: a.__getstate__() 

(A parent, {'value': 1}) 

sage: class A(ElementWrapper): 

....: pass 

sage: a = A(P, 1) 

sage: a.x = 2 

sage: a.__getstate__() == (P, {'value': 1, 'x': 2}) 

True 

""" 

try: 

d = self.__dict__ 

except AttributeError: 

d = {} 

else: 

d = d.copy() 

d['value'] = self.value 

return (self._parent, d) 

  

def __setstate__(self, state): 

r""" 

Initialize the state of the object from data saved in a pickle. 

  

This emulates :meth:`Element.__setstate__`, playing as if 

``self.value`` was to be put in the dictionary of ``self`` as 

it used to be before :trac:`14519`. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: class A(ElementWrapper): 

....: pass 

sage: a = A(DummyParent("A parent"), 1) 

sage: a.__setstate__((DummyParent("Another parent"), {'value':0,'x':3})) 

sage: a.parent() 

Another parent 

sage: a.value 

0 

sage: a.x 

3 

  

TESTS:: 

  

sage: a = A(DummyParent("A parent"), 1) 

sage: import __main__; __main__.A = A # Fake A being defined in a python module 

sage: a.x = 2 

sage: a == loads(dumps(a)) 

True 

sage: a = ElementWrapper(DummyParent("A parent"), 1) 

sage: a == loads(dumps(a)) 

True 

  

Checking that we can still load pickle from before :trac:`14519`:: 

  

sage: f = loads(b'x\x9c\x85\x8d\xbb\n\xc2@\x14D\xf1\x11\x8d\xebO\xd8\xda,\xf8\t\x82\xf6\x12\x08\x96a\x8dC\x08f\xd7\xdc\xbb{\x15\x8b\x80\x16\xd1\xdf6\x10\xad,,\xcf0s\xe6>\xcc\xbd)\xa0}`\xc9\x8304*X\xb8\x90]\xd9\xd45Xm{\xde\x7f\x90\x06\xcb\x07\xfd\x8c\xc4\x95$\xc8\x185\xc3wm\x13\xca\xb3S\xe2\x18G\xc9\xa1h\xf4\xefe#\xd6\xdev\x86\xbbL\xd18\x8d\xd7\x8b\x1e(j\x9b\x17M\x12\x9a6\x14\xa7\xd1\xc5T\x02\x9a\xf56.]\xe1u\xe9\x02\x8a\xce`\xcd\t\xd9\x17H\xa5\x83U\x9b\xd0\xdc?\x0f\xfa\rl4S\xbc') 

sage: f == ElementWrapper(DummyParent("A Parent"), 1) 

True 

""" 

# Make sure the first part of the state is the parent 

if not isinstance(state[0], Parent): 

state[0], state[1] = state[1], state[0] 

self._set_parent(state[0]) 

d = state[1].copy() 

self.value = d.pop('value') 

if d: 

self.__dict__ = d 

  

def _repr_(self): 

""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: ElementWrapper(DummyParent("A parent"), 1) 

1 

""" 

return repr(self.value) 

  

def _latex_(self): 

r""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: ElementWrapper(DummyParent("A parent"), 1)._latex_() 

1 

sage: ElementWrapper(DummyParent("A parent"), 3/5)._latex_() 

\frac{3}{5} 

""" 

from sage.misc.latex import latex 

return latex(self.value) 

  

def _ascii_art_(self): 

r""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: ElementWrapper(DummyParent("A parent"), 1)._ascii_art_() 

1 

sage: x = var('x') 

sage: ElementWrapper(DummyParent("A parent"), x^2 + x)._ascii_art_() 

2 

x + x 

""" 

from sage.typeset.ascii_art import ascii_art 

return ascii_art(self.value) 

  

def __hash__(self): 

""" 

Return the same hash as for the wrapped element. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent1 = DummyParent("A parent") 

sage: parent2 = DummyParent("Another parent") 

sage: hash(ElementWrapper(parent1, 1)) 

1 

sage: hash(ElementWrapper(parent2, 1)) 

1 

  

.. TODO:: 

  

Should this take the parent and/or the class into account? 

""" 

return hash(self.value) 

  

def __richcmp__(left, right, int op): 

""" 

Return ``True`` if ``left`` compares with ``right`` based on ``op``. 

  

Default implementation of ``self == other``: two elements are 

equal if they have equal parents and equal values. 

  

Default implementation of ``self < other``: two elements are 

always incomparable. 

  

.. NOTE:: 

  

Another option would be to not define ``__lt__``, but 

given the current implementation of SageObject, sorted(...) 

would break. 

  

TESTS: 

  

Check that elements of equal-but-not-identical parents compare 

properly (see :trac:`19488`):: 

  

sage: from sage.misc.nested_class_test import TestParent4 

sage: P = TestParent4() 

sage: Q = TestParent4() 

sage: P == Q 

True 

sage: P is Q 

False 

sage: x = P.an_element(); x 

'_an_element_' 

sage: y = Q.an_element(); y 

'_an_element_' 

sage: x == y 

True 

""" 

if isinstance(right, ElementWrapper) and left.parent() == right.parent(): 

return left._richcmp_(right, op) 

return coercion_model.richcmp(left, right, op) 

  

cpdef _richcmp_(left, right, int op): 

""" 

Return ``True`` if ``left`` compares with ``right`` based on ``op``. 

  

TESTS: 

  

Testing equality:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent1 = DummyParent("A parent") 

sage: parent2 = DummyParent("Another parent") 

sage: parent1 == parent2 

False 

sage: l11 = ElementWrapper(parent1, 1) 

sage: l12 = ElementWrapper(parent1, 2) 

sage: l21 = ElementWrapper(parent2, 1) 

sage: l22 = ElementWrapper(parent2, 2) 

sage: l11 == l11 

True 

sage: l11 == l12 

False 

sage: l11 == l21 

False 

  

Testing inequality:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent1 = DummyParent("A parent") 

sage: parent2 = DummyParent("Another parent") 

sage: parent1 == parent2 

False 

sage: l11 = ElementWrapper(parent1, 1) 

sage: l12 = ElementWrapper(parent1, 2) 

sage: l21 = ElementWrapper(parent2, 1) 

sage: l22 = ElementWrapper(parent2, 2) 

sage: l11 != l11 

False 

sage: l11 != l12 

True 

sage: l11 != l21 

True 

  

Testing less than:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent = DummyParent("A parent") 

sage: x = ElementWrapper(parent, 1) 

sage: y = ElementWrapper(parent, 2) 

sage: x.__lt__(x), x.__lt__(y), y.__lt__(x) 

(False, False, False) 

sage: x < x, x < y, y < x 

(False, False, False) 

sage: sorted([x,y]) 

[1, 2] 

sage: sorted([y,x]) 

[2, 1] 

""" 

cdef ElementWrapper self = left 

if op == Py_EQ or op == Py_LE or op == Py_GE: 

return self.value == (<ElementWrapper>right).value 

if op == Py_NE: 

return self.value != (<ElementWrapper>right).value 

return False 

  

cpdef bint _lt_by_value(self, other): 

""" 

Return whether ``self`` is strictly smaller than ``other``. 

  

With this implementation 'by value', they are always 

incomparable unless ``self`` and ``other`` have the same 

class, parent, and ``self.value < other.value``. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: class MyElement(ElementWrapper): 

....: __lt__ = ElementWrapper._lt_by_value 

....: 

sage: parent1 = DummyParent("A parent") 

sage: parent2 = DummyParent("Another parent") 

sage: l11 = MyElement(parent1, 1) 

sage: l12 = MyElement(parent1, 2) 

sage: l21 = MyElement(parent2, 1) 

sage: l22 = MyElement(parent2, 2) 

sage: l11 < l11 

False 

sage: l11 < l12, l12 < l11 # values differ 

(True, False) 

sage: l11 < l21 # parents differ 

False 

sage: l11 < 1 # class differ 

False 

sage: 1 < l11 # random, since it depends on what the Integer 1 decides to do, which may just involve memory locations 

False 

""" 

return (self.__class__ is other.__class__ 

and self._parent is other.parent() 

and self.value < (<ElementWrapper>other).value) 

  

def __copy__(self): 

""" 

Copy ``self`` and in particular its ``value`` attribute. 

  

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent = DummyParent("A parent") 

sage: o1 = ElementWrapper(parent, [1]); o1 

[1] 

sage: o2 = copy(o1); o2 

[1] 

sage: o1 is o2, o1.value is o2.value 

(False, False) 

sage: o2.value[0] = 3; o2 

[3] 

sage: o1 

[1] 

sage: class bla(ElementWrapper): pass 

sage: o3 = bla(parent, [1]) 

sage: o4 = copy(o3) 

sage: o3.value[0] = 3; o4 

[1] 

sage: o3.__class__ 

<class '__main__.bla'> 

sage: o4.__class__ 

<class '__main__.bla'> 

""" 

# Note : copy(super(ElementWrapper, self)) does not work. 

res = super(ElementWrapper, self).__copy__() 

res.value = copy(self.value) 

return res 

  

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

class DummyParent(UniqueRepresentation, Parent): 

""" 

A class for creating dummy parents for testing ElementWrapper 

""" 

def __init__(self, name): 

""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: parent = DummyParent("A Parent") 

sage: TestSuite(parent).run(skip = ["_test_an_element",\ 

"_test_category",\ 

"_test_elements",\ 

"_test_elements_eq_reflexive",\ 

"_test_elements_eq_symmetric",\ 

"_test_elements_eq_transitive",\ 

"_test_elements_neq",\ 

"_test_some_elements"]) 

""" 

self.name = name 

  

def _repr_(self): 

""" 

EXAMPLES:: 

  

sage: from sage.structure.element_wrapper import DummyParent 

sage: DummyParent("A Parent") # indirect doctest 

A Parent 

""" 

return self.name 

  

class ElementWrapperTester(ElementWrapper): 

""" 

Test class for the default :meth:`.__copy` method of subclasses of 

:class:`ElementWrapper`. 

  

TESTS:: 

  

sage: from sage.structure.element_wrapper import ElementWrapperTester 

sage: x = ElementWrapperTester() 

sage: x.append(2); y = copy(x); y.append(42) 

sage: type(y) 

<class 'sage.structure.element_wrapper.ElementWrapperTester'> 

sage: x, y 

([n=1, value=[2]], [n=2, value=[2, 42]]) 

sage: x.append(21); x.append(7) 

sage: x, y 

([n=3, value=[2, 21, 7]], [n=2, value=[2, 42]]) 

sage: x.value, y.value 

([2, 21, 7], [2, 42]) 

sage: x.__dict__, y.__dict__ 

({'n': 3}, {'n': 2}) 

""" 

def __init__(self): 

""" 

TESTS:: 

  

sage: from sage.structure.element_wrapper import ElementWrapperTester 

sage: x = ElementWrapperTester(); x 

[n=0, value=[]] 

""" 

from sage.categories.sets_cat import Sets 

super(ElementWrapperTester, self).__init__(Sets().example("facade"), []) 

self.n = 0 

  

def append(self, x): 

""" 

TESTS:: 

  

sage: from sage.structure.element_wrapper import ElementWrapperTester 

sage: x = ElementWrapperTester() 

sage: x.append(2); x 

[n=1, value=[2]] 

""" 

self.n +=1 

self.value.append(x) 

  

def _repr_(self): 

""" 

TESTS:: 

  

sage: from sage.structure.element_wrapper import ElementWrapperTester 

sage: x = ElementWrapperTester 

sage: x = ElementWrapperTester(); x 

[n=0, value=[]] 

sage: x.value = [2,32]; x # indirect doctest 

[n=0, value=[2, 32]] 

""" 

return "[n=%s, value=%s]"%(self.n, self.value) 

  

cdef class ElementWrapperCheckWrappedClass(ElementWrapper): 

""" 

An :class:`element wrapper <ElementWrapper>` such that comparison 

operations are done against subclasses of ``wrapped_class``. 

""" 

wrapped_class = object 

  

def __richcmp__(left, right, int op): 

""" 

Return ``True`` if ``left`` compares with ``right`` based on ``op``. 

  

.. SEEALSO:: 

  

:meth:`ElementWrapper.__richcmp__` 

  

TESTS:: 

  

sage: A = cartesian_product([ZZ, ZZ]) 

sage: elt = A((1,1)) 

sage: (1, 1) == elt 

True 

sage: elt == (1, 1) 

True 

sage: A((1, 2)) == elt 

False 

""" 

cdef ElementWrapperCheckWrappedClass self 

self = left 

  

if self.__class__ != right.__class__: 

if isinstance(right, self.wrapped_class): 

if op == Py_EQ or op == Py_LE or op == Py_GE: 

return self.value == right 

if op == Py_NE: 

return self.value != right 

return False 

return op == Py_NE 

if self._parent != (<ElementWrapper>right)._parent: 

return op == Py_NE 

if op == Py_EQ or op == Py_LE or op == Py_GE: 

return self.value == (<ElementWrapper>right).value 

if op == Py_NE: 

return self.value != (<ElementWrapper>right).value 

return False