Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/matroids/unpickling.pyx
8817 views
1
"""
2
Unpickling methods
3
4
Python saves objects by providing a pair ``(f, data)`` such that ``f(data)``
5
reconstructs the object. This module collects the loading (_unpickling_ in
6
Python terminology) functions for Sage's matroids.
7
8
.. NOTE::
9
10
The reason this code was separated out from the classes was to make it
11
play nice with lazy importing of the ``Matroid()`` and ``matroids``
12
keywords.
13
14
AUTHORS:
15
16
- Rudi Pendavingh, Stefan van Zwam (2013-07-01): initial version
17
"""
18
#*****************************************************************************
19
# Copyright (C) 2013 Rudi Pendavingh <[email protected]>
20
# Copyright (C) 2013 Stefan van Zwam <[email protected]>
21
#
22
# Distributed under the terms of the GNU General Public License (GPL)
23
# as published by the Free Software Foundation; either version 2 of
24
# the License, or (at your option) any later version.
25
# http://www.gnu.org/licenses/
26
#*****************************************************************************
27
include 'sage/ext/stdsage.pxi'
28
include 'sage/misc/bitset.pxi'
29
import sage.matroids.matroid
30
import sage.matroids.basis_exchange_matroid
31
from minor_matroid import MinorMatroid
32
from dual_matroid import DualMatroid
33
from circuit_closures_matroid cimport CircuitClosuresMatroid
34
from basis_matroid cimport BasisMatroid
35
from linear_matroid cimport LinearMatroid, RegularMatroid, BinaryMatroid, TernaryMatroid, QuaternaryMatroid
36
from lean_matrix cimport GenericMatrix, BinaryMatrix, TernaryMatrix, QuaternaryMatrix, IntegerMatrix
37
38
39
#############################################################################
40
# BasisMatroid
41
#############################################################################
42
43
def unpickle_basis_matroid(version, data):
44
"""
45
Unpickle a BasisMatroid.
46
47
*Pickling* is Python's term for the loading and saving of objects.
48
Functions like these serve to reconstruct a saved object. This all happens
49
transparently through the ``load`` and ``save`` commands, and you should
50
never have to call this function directly.
51
52
INPUT:
53
54
- ``version`` -- an integer, expected to be 0
55
- ``data`` -- a tuple ``(E, R, name, BB)`` in which ``E`` is the groundset
56
of the matroid, ``R`` is the rank, ``name`` is a custom name, and ``BB``
57
is the bitpacked list of bases, as pickled by Sage's ``bitset_pickle``.
58
59
OUTPUT:
60
61
A matroid.
62
63
.. WARNING::
64
65
Users should never call this function directly.
66
67
EXAMPLES::
68
69
sage: from sage.matroids.advanced import *
70
sage: M = BasisMatroid(matroids.named_matroids.Vamos())
71
sage: M == loads(dumps(M)) # indirect doctest
72
True
73
74
"""
75
cdef BasisMatroid M
76
if version != 0:
77
raise TypeError("object was created with newer version of Sage. Please upgrade.")
78
E, R, name, BB = data
79
M = BasisMatroid(groundset=E, rank=R)
80
bitset_unpickle(M._bb, BB)
81
M._reset_invariants()
82
M.reset_current_basis()
83
if name is not None:
84
M.rename(name)
85
return M
86
87
88
#############################################################################
89
# CircuitClosuresMatroid
90
#############################################################################
91
92
def unpickle_circuit_closures_matroid(version, data):
93
"""
94
Unpickle a CircuitClosuresMatroid.
95
96
*Pickling* is Python's term for the loading and saving of objects.
97
Functions like these serve to reconstruct a saved object. This all happens
98
transparently through the ``load`` and ``save`` commands, and you should
99
never have to call this function directly.
100
101
INPUT:
102
103
- ``version`` -- an integer, expected to be 0
104
- ``data`` -- a tuple ``(E, CC, name)`` in which ``E`` is the groundset
105
of the matroid, ``CC`` is the dictionary of circuit closures, and
106
``name`` is a custom name.
107
108
OUTPUT:
109
110
A matroid.
111
112
.. WARNING::
113
114
Users should never call this function directly.
115
116
EXAMPLES::
117
118
sage: M = matroids.named_matroids.Vamos()
119
sage: M == loads(dumps(M)) # indirect doctest
120
True
121
"""
122
cdef CircuitClosuresMatroid M
123
if version != 0:
124
raise TypeError("object was created with newer version of Sage. Please upgrade.")
125
M = CircuitClosuresMatroid(groundset=data[0], circuit_closures=data[1])
126
if data[2] is not None:
127
M.rename(data[2])
128
return M
129
130
131
#############################################################################
132
# DualMatroid
133
#############################################################################
134
135
def unpickle_dual_matroid(version, data):
136
"""
137
Unpickle a DualMatroid.
138
139
*Pickling* is Python's term for the loading and saving of objects.
140
Functions like these serve to reconstruct a saved object. This all happens
141
transparently through the ``load`` and ``save`` commands, and you should
142
never have to call this function directly.
143
144
INPUT:
145
146
- ``version`` -- an integer, expected to be 0
147
- ``data`` -- a tuple ``(M, name)`` in which ``M`` is
148
the internal matroid, and ``name`` is a custom name.
149
150
OUTPUT:
151
152
A matroid.
153
154
.. WARNING::
155
156
Users should not call this function directly. Instead, use load/save.
157
158
EXAMPLES::
159
160
sage: M = matroids.named_matroids.Vamos().dual()
161
sage: M == loads(dumps(M)) # indirect doctest
162
True
163
"""
164
if version != 0:
165
raise TypeError("object was created with newer version of Sage. Please upgrade.")
166
M = DualMatroid(data[0])
167
if data[1] is not None:
168
M.rename(data[1])
169
return M
170
171
172
#############################################################################
173
# LeanMatrix subclasses
174
#############################################################################
175
176
def unpickle_generic_matrix(version, data):
177
"""
178
Reconstruct a ``GenericMatrix`` object (internal Sage data structure).
179
180
.. WARNING::
181
182
Users should not call this method directly.
183
184
EXAMPLES::
185
186
sage: from sage.matroids.lean_matrix import *
187
sage: A = GenericMatrix(2, 5, ring=QQ)
188
sage: A == loads(dumps(A)) # indirect doctest
189
True
190
"""
191
if version != 0:
192
raise TypeError("object was created with newer version of Sage. Please upgrade.")
193
cdef GenericMatrix A = GenericMatrix(0, 0, ring=data[2])
194
A._entries = data[3][:]
195
A._nrows = data[0]
196
A._ncols = data[1]
197
return A
198
199
200
def unpickle_binary_matrix(version, data):
201
"""
202
Reconstruct a ``BinaryMatrix`` object (internal Sage data structure).
203
204
.. WARNING::
205
206
Users should not call this method directly.
207
208
EXAMPLES::
209
210
sage: from sage.matroids.lean_matrix import *
211
sage: A = BinaryMatrix(2, 5)
212
sage: A == loads(dumps(A)) # indirect doctest
213
True
214
sage: C = BinaryMatrix(2, 2, Matrix(GF(2), [[1, 1], [0, 1]]))
215
sage: C == loads(dumps(C))
216
True
217
"""
218
cdef BinaryMatrix A
219
cdef long i
220
if version != 0:
221
raise TypeError("object was created with newer version of Sage. Please upgrade.")
222
nrows, ncols, versionB, size, limbs, longsize, M = data
223
A = BinaryMatrix(nrows, ncols)
224
for i from 0 <= i < nrows:
225
bitset_unpickle(A._M[i], (versionB, size, limbs, longsize, M[i]))
226
return A
227
228
229
def unpickle_ternary_matrix(version, data):
230
"""
231
Reconstruct a ``TernaryMatrix`` object (internal Sage data structure).
232
233
.. WARNING::
234
235
Users should not call this method directly.
236
237
EXAMPLES::
238
239
sage: from sage.matroids.lean_matrix import *
240
sage: A = TernaryMatrix(2, 5)
241
sage: A == loads(dumps(A)) # indirect doctest
242
True
243
sage: C = TernaryMatrix(2, 2, Matrix(GF(3), [[1, 1], [0, 1]]))
244
sage: C == loads(dumps(C))
245
True
246
"""
247
cdef TernaryMatrix A
248
cdef long i
249
if version != 0:
250
raise TypeError("object was created with newer version of Sage. Please upgrade.")
251
nrows, ncols, versionB, size, limbs, longsize, M0, M1 = data
252
A = TernaryMatrix(nrows, ncols)
253
for i from 0 <= i < nrows:
254
bitset_unpickle(A._M0[i], (versionB, size, limbs, longsize, M0[i]))
255
bitset_unpickle(A._M1[i], (versionB, size, limbs, longsize, M1[i]))
256
return A
257
258
259
def unpickle_quaternary_matrix(version, data):
260
"""
261
Reconstruct a ``QuaternaryMatrix`` object (internal Sage data structure).
262
263
.. WARNING::
264
265
Users should not call this method directly.
266
267
EXAMPLES::
268
269
sage: from sage.matroids.lean_matrix import *
270
sage: A = QuaternaryMatrix(2, 5, ring=GF(4, 'x'))
271
sage: A == loads(dumps(A)) # indirect doctest
272
True
273
sage: C = QuaternaryMatrix(2, 2, Matrix(GF(4, 'x'), [[1, 1], [0, 1]]))
274
sage: C == loads(dumps(C))
275
True
276
"""
277
cdef QuaternaryMatrix A
278
cdef long i
279
if version != 0:
280
raise TypeError("object was created with newer version of Sage. Please upgrade.")
281
nrows, ncols, ring, versionB, size, limbs, longsize, M0, M1 = data
282
A = QuaternaryMatrix(nrows, ncols, ring=ring)
283
for i from 0 <= i < nrows:
284
bitset_unpickle(A._M0[i], (versionB, size, limbs, longsize, M0[i]))
285
bitset_unpickle(A._M1[i], (versionB, size, limbs, longsize, M1[i]))
286
return A
287
288
289
def unpickle_integer_matrix(version, data):
290
"""
291
Reconstruct an ``IntegerMatrix`` object (internal Sage data structure).
292
293
.. WARNING::
294
295
Users should not call this method directly.
296
297
EXAMPLES::
298
299
sage: from sage.matroids.lean_matrix import *
300
sage: A = IntegerMatrix(2, 5)
301
sage: A == loads(dumps(A)) # indirect doctest
302
True
303
"""
304
if version != 0:
305
raise TypeError("object was created with newer version of Sage. Please upgrade.")
306
cdef IntegerMatrix A = IntegerMatrix(data[0], data[1])
307
cdef long i
308
for i from 0 <= i < A._nrows * A._ncols:
309
A._entries[i] = data[2][i]
310
return A
311
312
313
#############################################################################
314
# LinearMatroid and subclasses
315
#############################################################################
316
317
def unpickle_linear_matroid(version, data):
318
"""
319
Unpickle a LinearMatroid.
320
321
*Pickling* is Python's term for the loading and saving of objects.
322
Functions like these serve to reconstruct a saved object. This all happens
323
transparently through the ``load`` and ``save`` commands, and you should
324
never have to call this function directly.
325
326
INPUT:
327
328
- ``version`` -- an integer (currently 0).
329
- ``data`` -- a tuple ``(A, E, reduced, name)`` where ``A`` is the
330
representation matrix, ``E`` is the groundset of the matroid,
331
``reduced`` is a boolean indicating whether ``A`` is a reduced matrix,
332
and ``name`` is a custom name.
333
334
OUTPUT:
335
336
A :class:`LinearMatroid` instance.
337
338
.. WARNING::
339
340
Users should never call this function directly.
341
342
EXAMPLES::
343
344
sage: M = Matroid(Matrix(GF(7), [[1, 0, 0, 1, 1], [0, 1, 0, 1, 2],
345
....: [0, 1, 1, 1, 3]]))
346
sage: M == loads(dumps(M)) # indirect doctest
347
True
348
sage: M.rename("U35")
349
sage: loads(dumps(M))
350
U35
351
"""
352
if version != 0:
353
raise TypeError("object was created with newer version of Sage. Please upgrade.")
354
A, gs, reduced, name = data
355
if not reduced:
356
M = LinearMatroid(groundset=gs, matrix=A, keep_initial_representation=True)
357
else:
358
M = LinearMatroid(groundset=gs, reduced_matrix=A)
359
if name is not None:
360
M.rename(name)
361
return M
362
363
364
def unpickle_binary_matroid(version, data):
365
"""
366
Unpickle a BinaryMatroid.
367
368
*Pickling* is Python's term for the loading and saving of objects.
369
Functions like these serve to reconstruct a saved object. This all happens
370
transparently through the ``load`` and ``save`` commands, and you should
371
never have to call this function directly.
372
373
INPUT:
374
375
- ``version`` -- an integer (currently 0).
376
- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the
377
representation matrix, ``E`` is the groundset of the matroid, ``B`` is
378
the currently displayed basis, and ``name`` is a custom name.
379
380
OUTPUT:
381
382
A :class:`BinaryMatroid` instance.
383
384
.. WARNING::
385
386
Users should never call this function directly.
387
388
EXAMPLES::
389
390
sage: M = Matroid(Matrix(GF(2), [[1, 0, 0, 1], [0, 1, 0, 1],
391
....: [0, 0, 1, 1]]))
392
sage: M == loads(dumps(M)) # indirect doctest
393
True
394
sage: M.rename("U34")
395
sage: loads(dumps(M))
396
U34
397
"""
398
if version != 0:
399
raise TypeError("object was created with newer version of Sage. Please upgrade.")
400
A, gs, basis, name = data
401
if basis is None:
402
M = BinaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True)
403
else:
404
M = BinaryMatroid(groundset=gs, matrix=A, basis=basis)
405
if name is not None:
406
M.rename(name)
407
return M
408
409
410
def unpickle_ternary_matroid(version, data):
411
"""
412
Unpickle a TernaryMatroid.
413
414
*Pickling* is Python's term for the loading and saving of objects.
415
Functions like these serve to reconstruct a saved object. This all happens
416
transparently through the ``load`` and ``save`` commands, and you should
417
never have to call this function directly.
418
419
INPUT:
420
421
- ``version`` -- an integer (currently 0).
422
- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the
423
representation matrix, ``E`` is the groundset of the matroid, ``B`` is
424
the currently displayed basis, and ``name`` is a custom name.
425
426
OUTPUT:
427
428
A :class:`TernaryMatroid` instance.
429
430
.. WARNING::
431
432
Users should never call this function directly.
433
434
EXAMPLES::
435
436
sage: from sage.matroids.advanced import *
437
sage: M = TernaryMatroid(Matrix(GF(3), [[1, 0, 0, 1], [0, 1, 0, 1],
438
....: [0, 0, 1, 1]]))
439
sage: M == loads(dumps(M)) # indirect doctest
440
True
441
sage: M.rename("U34")
442
sage: loads(dumps(M))
443
U34
444
"""
445
if version != 0:
446
raise TypeError("object was created with newer version of Sage. Please upgrade.")
447
A, gs, basis, name = data
448
if basis is None:
449
M = TernaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True)
450
else:
451
M = TernaryMatroid(groundset=gs, matrix=A, basis=basis)
452
if name is not None:
453
M.rename(name)
454
return M
455
456
457
def unpickle_quaternary_matroid(version, data):
458
"""
459
Unpickle a QuaternaryMatroid.
460
461
*Pickling* is Python's term for the loading and saving of objects.
462
Functions like these serve to reconstruct a saved object. This all happens
463
transparently through the ``load`` and ``save`` commands, and you should
464
never have to call this function directly.
465
466
INPUT:
467
468
- ``version`` -- an integer (currently 0).
469
- ``data`` -- a tuple ``(A, E, B, name)`` where ``A`` is the
470
representation matrix, ``E`` is the groundset of the matroid, ``B`` is
471
the currently displayed basis, and ``name`` is a custom name.
472
473
OUTPUT:
474
475
A :class:`TernaryMatroid` instance.
476
477
.. WARNING::
478
479
Users should never call this function directly.
480
481
EXAMPLES::
482
483
sage: from sage.matroids.advanced import *
484
sage: M = QuaternaryMatroid(Matrix(GF(3), [[1, 0, 0, 1], [0, 1, 0, 1],
485
....: [0, 0, 1, 1]]))
486
sage: M == loads(dumps(M)) # indirect doctest
487
True
488
sage: M.rename("U34")
489
sage: loads(dumps(M))
490
U34
491
sage: M = QuaternaryMatroid(Matrix(GF(4, 'x'), [[1, 0, 1],
492
....: [1, 0, 1]]))
493
sage: loads(dumps(M)).representation()
494
[1 0 1]
495
[1 0 1]
496
"""
497
if version != 0:
498
raise TypeError("object was created with newer version of Sage. Please upgrade.")
499
A, gs, basis, name = data
500
if basis is None:
501
M = QuaternaryMatroid(groundset=gs, matrix=A, keep_initial_representation=True)
502
else:
503
M = QuaternaryMatroid(groundset=gs, matrix=A, basis=basis)
504
if name is not None:
505
M.rename(name)
506
return M
507
508
509
def unpickle_regular_matroid(version, data):
510
"""
511
Unpickle a RegularMatroid.
512
513
*Pickling* is Python's term for the loading and saving of objects.
514
Functions like these serve to reconstruct a saved object. This all happens
515
transparently through the ``load`` and ``save`` commands, and you should
516
never have to call this function directly.
517
518
INPUT:
519
520
- ``version`` -- an integer (currently 0).
521
- ``data`` -- a tuple ``(A, E, reduced, name)`` where ``A`` is the
522
representation matrix, ``E`` is the groundset of the matroid,
523
``reduced`` is a boolean indicating whether ``A`` is a reduced matrix,
524
and ``name`` is a custom name.
525
526
OUTPUT:
527
528
A :class:`RegularMatroid` instance.
529
530
.. WARNING::
531
532
Users should never call this function directly.
533
534
EXAMPLES::
535
536
sage: M = matroids.named_matroids.R10()
537
sage: M == loads(dumps(M)) # indirect doctest
538
True
539
sage: M.rename("R_{10}")
540
sage: loads(dumps(M))
541
R_{10}
542
"""
543
if version != 0:
544
raise TypeError("object was created with newer version of Sage. Please upgrade.")
545
A, gs, reduced, name = data
546
if not reduced:
547
M = RegularMatroid(groundset=gs, matrix=A, keep_initial_representation=True)
548
else:
549
M = RegularMatroid(groundset=gs, reduced_matrix=A)
550
if name is not None:
551
M.rename(name)
552
return M
553
554
555
#############################################################################
556
# Minor matroids
557
#############################################################################
558
559
def unpickle_minor_matroid(version, data):
560
"""
561
Unpickle a MinorMatroid.
562
563
*Pickling* is Python's term for the loading and saving of objects.
564
Functions like these serve to reconstruct a saved object. This all happens
565
transparently through the ``load`` and ``save`` commands, and you should
566
never have to call this function directly.
567
568
INPUT:
569
570
- ``version`` -- an integer, currently `0`.
571
- ``data`` -- a tuple ``(M, C, D, name)``, where ``M`` is the original
572
matroid of which the output is a minor, ``C`` is the set of
573
contractions, ``D`` is the set of deletions, and ``name`` is a custom
574
name.
575
576
OUTPUT:
577
578
A :class:`MinorMatroid` instance.
579
580
.. WARNING::
581
582
Users should never call this function directly.
583
584
EXAMPLES::
585
586
sage: M = matroids.named_matroids.Vamos().minor('abc', 'g')
587
sage: M == loads(dumps(M)) # indirect doctest
588
True
589
"""
590
if version != 0:
591
raise TypeError("object was created with newer version of Sage. Please upgrade.")
592
M = MinorMatroid(matroid=data[0], contractions=data[1], deletions=data[2])
593
if data[3] is not None:
594
M.rename(data[3])
595
return M
596
597