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

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

""" 

Unpickling methods 

  

Python saves objects by providing a pair ``(f, data)`` such that ``f(data)`` 

reconstructs the object. This module collects the loading (_unpickling_ in 

Python terminology) functions for Sage's matroids. 

  

.. NOTE:: 

  

The reason this code was separated out from the classes was to make it 

play nice with lazy importing of the ``Matroid()`` and ``matroids`` 

keywords. 

  

AUTHORS: 

  

- Rudi Pendavingh, Stefan van Zwam (2013-07-01): initial version 

""" 

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

# Copyright (C) 2013 Rudi Pendavingh <rudi.pendavingh@gmail.com> 

# Copyright (C) 2013 Stefan van Zwam <stefanvanzwam@gmail.com> 

# 

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

# 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 __future__ import absolute_import 

  

include 'sage/data_structures/bitset.pxi' 

import sage.matroids.matroid 

import sage.matroids.basis_exchange_matroid 

from .minor_matroid import MinorMatroid 

from .dual_matroid import DualMatroid 

from .circuit_closures_matroid cimport CircuitClosuresMatroid 

from .basis_matroid cimport BasisMatroid 

from .linear_matroid cimport LinearMatroid, RegularMatroid, BinaryMatroid, TernaryMatroid, QuaternaryMatroid 

from .lean_matrix cimport GenericMatrix, BinaryMatrix, TernaryMatrix, QuaternaryMatrix, IntegerMatrix 

from .graphic_matroid import GraphicMatroid 

  

  

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

# BasisMatroid 

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

  

def unpickle_basis_matroid(version, data): 

""" 

Unpickle a BasisMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer, expected to be 0 

- ``data`` -- a tuple ``(E, R, name, BB)`` in which ``E`` is the groundset 

of the matroid, ``R`` is the rank, ``name`` is a custom name, and ``BB`` 

is the bitpacked list of bases, as pickled by Sage's ``bitset_pickle``. 

  

OUTPUT: 

  

A matroid. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.advanced import * 

sage: M = BasisMatroid(matroids.named_matroids.Vamos()) 

sage: M == loads(dumps(M)) # indirect doctest 

True 

  

""" 

cdef BasisMatroid M 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

E, R, name, BB = data 

M = BasisMatroid(groundset=E, rank=R) 

bitset_unpickle(M._bb, BB) 

M._reset_invariants() 

M.reset_current_basis() 

if name is not None: 

M.rename(name) 

return M 

  

  

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

# CircuitClosuresMatroid 

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

  

def unpickle_circuit_closures_matroid(version, data): 

""" 

Unpickle a CircuitClosuresMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer, expected to be 0 

- ``data`` -- a tuple ``(E, CC, name)`` in which ``E`` is the groundset 

of the matroid, ``CC`` is the dictionary of circuit closures, and 

``name`` is a custom name. 

  

OUTPUT: 

  

A matroid. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = matroids.named_matroids.Vamos() 

sage: M == loads(dumps(M)) # indirect doctest 

True 

""" 

cdef CircuitClosuresMatroid M 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

M = CircuitClosuresMatroid(groundset=data[0], circuit_closures=data[1]) 

if data[2] is not None: 

M.rename(data[2]) 

return M 

  

  

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

# DualMatroid 

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

  

def unpickle_dual_matroid(version, data): 

""" 

Unpickle a DualMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer, expected to be 0 

- ``data`` -- a tuple ``(M, name)`` in which ``M`` is 

the internal matroid, and ``name`` is a custom name. 

  

OUTPUT: 

  

A matroid. 

  

.. WARNING:: 

  

Users should not call this function directly. Instead, use load/save. 

  

EXAMPLES:: 

  

sage: M = matroids.named_matroids.Vamos().dual() 

sage: M == loads(dumps(M)) # indirect doctest 

True 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

M = DualMatroid(data[0]) 

if data[1] is not None: 

M.rename(data[1]) 

return M 

  

  

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

# LeanMatrix subclasses 

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

  

def unpickle_generic_matrix(version, data): 

""" 

Reconstruct a ``GenericMatrix`` object (internal Sage data structure). 

  

.. WARNING:: 

  

Users should not call this method directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.lean_matrix import * 

sage: A = GenericMatrix(2, 5, ring=QQ) 

sage: A == loads(dumps(A)) # indirect doctest 

True 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

cdef GenericMatrix A = GenericMatrix(0, 0, ring=data[2]) 

A._entries = data[3][:] 

A._nrows = data[0] 

A._ncols = data[1] 

return A 

  

  

def unpickle_binary_matrix(version, data): 

""" 

Reconstruct a ``BinaryMatrix`` object (internal Sage data structure). 

  

.. WARNING:: 

  

Users should not call this method directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.lean_matrix import * 

sage: A = BinaryMatrix(2, 5) 

sage: A == loads(dumps(A)) # indirect doctest 

True 

sage: C = BinaryMatrix(2, 2, Matrix(GF(2), [[1, 1], [0, 1]])) 

sage: C == loads(dumps(C)) 

True 

""" 

cdef BinaryMatrix A 

cdef long i 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

nrows, ncols, versionB, size, limbs, longsize, M = data 

A = BinaryMatrix(nrows, ncols) 

for i from 0 <= i < nrows: 

bitset_unpickle(A._M[i], (versionB, size, limbs, longsize, M[i])) 

return A 

  

  

def unpickle_ternary_matrix(version, data): 

""" 

Reconstruct a ``TernaryMatrix`` object (internal Sage data structure). 

  

.. WARNING:: 

  

Users should not call this method directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.lean_matrix import * 

sage: A = TernaryMatrix(2, 5) 

sage: A == loads(dumps(A)) # indirect doctest 

True 

sage: C = TernaryMatrix(2, 2, Matrix(GF(3), [[1, 1], [0, 1]])) 

sage: C == loads(dumps(C)) 

True 

""" 

cdef TernaryMatrix A 

cdef long i 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

nrows, ncols, versionB, size, limbs, longsize, M0, M1 = data 

A = TernaryMatrix(nrows, ncols) 

for i from 0 <= i < nrows: 

bitset_unpickle(A._M0[i], (versionB, size, limbs, longsize, M0[i])) 

bitset_unpickle(A._M1[i], (versionB, size, limbs, longsize, M1[i])) 

return A 

  

  

def unpickle_quaternary_matrix(version, data): 

""" 

Reconstruct a ``QuaternaryMatrix`` object (internal Sage data structure). 

  

.. WARNING:: 

  

Users should not call this method directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.lean_matrix import * 

sage: A = QuaternaryMatrix(2, 5, ring=GF(4, 'x')) 

sage: A == loads(dumps(A)) # indirect doctest 

True 

sage: C = QuaternaryMatrix(2, 2, Matrix(GF(4, 'x'), [[1, 1], [0, 1]])) 

sage: C == loads(dumps(C)) 

True 

""" 

cdef QuaternaryMatrix A 

cdef long i 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

nrows, ncols, ring, versionB, size, limbs, longsize, M0, M1 = data 

A = QuaternaryMatrix(nrows, ncols, ring=ring) 

for i from 0 <= i < nrows: 

bitset_unpickle(A._M0[i], (versionB, size, limbs, longsize, M0[i])) 

bitset_unpickle(A._M1[i], (versionB, size, limbs, longsize, M1[i])) 

return A 

  

  

def unpickle_integer_matrix(version, data): 

""" 

Reconstruct an ``IntegerMatrix`` object (internal Sage data structure). 

  

.. WARNING:: 

  

Users should not call this method directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.lean_matrix import * 

sage: A = IntegerMatrix(2, 5) 

sage: A == loads(dumps(A)) # indirect doctest 

True 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

cdef IntegerMatrix A = IntegerMatrix(data[0], data[1]) 

cdef long i 

for i from 0 <= i < A._nrows * A._ncols: 

A._entries[i] = data[2][i] 

return A 

  

  

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

# LinearMatroid and subclasses 

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

  

def unpickle_linear_matroid(version, data): 

""" 

Unpickle a LinearMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple ``(A, E, reduced, name)`` where ``A`` is the 

representation matrix, ``E`` is the groundset of the matroid, 

``reduced`` is a boolean indicating whether ``A`` is a reduced matrix, 

and ``name`` is a custom name. 

  

OUTPUT: 

  

A :class:`LinearMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = Matroid(Matrix(GF(7), [[1, 0, 0, 1, 1], [0, 1, 0, 1, 2], 

....: [0, 1, 1, 1, 3]])) 

sage: M == loads(dumps(M)) # indirect doctest 

True 

sage: M.rename("U35") 

sage: loads(dumps(M)) 

U35 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

A, gs, reduced, name = data 

if not reduced: 

M = LinearMatroid(groundset=gs, matrix=A, keep_initial_representation=True) 

else: 

M = LinearMatroid(groundset=gs, reduced_matrix=A) 

if name is not None: 

M.rename(name) 

return M 

  

  

def unpickle_binary_matroid(version, data): 

""" 

Unpickle a BinaryMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the 

representation matrix, ``E`` is the groundset of the matroid, ``B`` is 

the currently displayed basis, and ``name`` is a custom name. 

  

OUTPUT: 

  

A :class:`BinaryMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = Matroid(Matrix(GF(2), [[1, 0, 0, 1], [0, 1, 0, 1], 

....: [0, 0, 1, 1]])) 

sage: M == loads(dumps(M)) # indirect doctest 

True 

sage: M.rename("U34") 

sage: loads(dumps(M)) 

U34 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

A, gs, basis, name = data 

if basis is None: 

M = BinaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True) 

else: 

M = BinaryMatroid(groundset=gs, matrix=A, basis=basis) 

if name is not None: 

M.rename(name) 

return M 

  

  

def unpickle_ternary_matroid(version, data): 

""" 

Unpickle a TernaryMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the 

representation matrix, ``E`` is the groundset of the matroid, ``B`` is 

the currently displayed basis, and ``name`` is a custom name. 

  

OUTPUT: 

  

A :class:`TernaryMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.advanced import * 

sage: M = TernaryMatroid(Matrix(GF(3), [[1, 0, 0, 1], [0, 1, 0, 1], 

....: [0, 0, 1, 1]])) 

sage: M == loads(dumps(M)) # indirect doctest 

True 

sage: M.rename("U34") 

sage: loads(dumps(M)) 

U34 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

A, gs, basis, name = data 

if basis is None: 

M = TernaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True) 

else: 

M = TernaryMatroid(groundset=gs, matrix=A, basis=basis) 

if name is not None: 

M.rename(name) 

return M 

  

  

def unpickle_quaternary_matroid(version, data): 

""" 

Unpickle a QuaternaryMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the 

representation matrix, ``E`` is the groundset of the matroid, ``B`` is 

the currently displayed basis, and ``name`` is a custom name. 

  

OUTPUT: 

  

A :class:`TernaryMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: from sage.matroids.advanced import * 

sage: M = QuaternaryMatroid(Matrix(GF(3), [[1, 0, 0, 1], [0, 1, 0, 1], 

....: [0, 0, 1, 1]])) 

sage: M == loads(dumps(M)) # indirect doctest 

True 

sage: M.rename("U34") 

sage: loads(dumps(M)) 

U34 

sage: M = QuaternaryMatroid(Matrix(GF(4, 'x'), [[1, 0, 1], 

....: [1, 0, 1]])) 

sage: loads(dumps(M)).representation() 

[1 0 1] 

[1 0 1] 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

A, gs, basis, name = data 

if basis is None: 

M = QuaternaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True) 

else: 

M = QuaternaryMatroid(groundset=gs, matrix=A, basis=basis) 

if name is not None: 

M.rename(name) 

return M 

  

  

def unpickle_regular_matroid(version, data): 

""" 

Unpickle a RegularMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple ``(A, E, reduced, name)`` where ``A`` is the 

representation matrix, ``E`` is the groundset of the matroid, 

``reduced`` is a boolean indicating whether ``A`` is a reduced matrix, 

and ``name`` is a custom name. 

  

OUTPUT: 

  

A :class:`RegularMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = matroids.named_matroids.R10() 

sage: M == loads(dumps(M)) # indirect doctest 

True 

sage: M.rename("R_{10}") 

sage: loads(dumps(M)) 

R_{10} 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

A, gs, reduced, name = data 

if not reduced: 

M = RegularMatroid(groundset=gs, matrix=A, keep_initial_representation=True) 

else: 

M = RegularMatroid(groundset=gs, reduced_matrix=A) 

if name is not None: 

M.rename(name) 

return M 

  

  

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

# Minor matroids 

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

  

def unpickle_minor_matroid(version, data): 

""" 

Unpickle a MinorMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer, currently `0`. 

- ``data`` -- a tuple ``(M, C, D, name)``, where ``M`` is the original 

matroid of which the output is a minor, ``C`` is the set of 

contractions, ``D`` is the set of deletions, and ``name`` is a custom 

name. 

  

OUTPUT: 

  

A :class:`MinorMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = matroids.named_matroids.Vamos().minor('abc', 'g') 

sage: M == loads(dumps(M)) # indirect doctest 

True 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

M = MinorMatroid(matroid=data[0], contractions=data[1], deletions=data[2]) 

if data[3] is not None: 

M.rename(data[3]) 

return M 

  

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

# Graphic Matroids 

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

  

def unpickle_graphic_matroid(version, data): 

""" 

Unpickle a GraphicMatroid. 

  

*Pickling* is Python's term for the loading and saving of objects. 

Functions like these serve to reconstruct a saved object. This all happens 

transparently through the ``load`` and ``save`` commands, and you should 

never have to call this function directly. 

  

INPUT: 

  

- ``version`` -- an integer (currently 0). 

- ``data`` -- a tuple consisting of a SageMath graph and a name. 

  

OUTPUT: 

  

A :class:`GraphicMatroid` instance. 

  

.. WARNING:: 

  

Users should never call this function directly. 

  

EXAMPLES:: 

  

sage: M = Matroid(graphs.DiamondGraph()) 

sage: M == loads(dumps(M)) 

True 

""" 

if version != 0: 

raise TypeError("object was created with newer version of Sage. Please upgrade.") 

G, name = data 

M = GraphicMatroid(G) 

if name is not None: 

M.rename(name) 

return M