Skip to content

Commit 7fa775d

Browse files
author
Matthias Koeppe
committed
sage -fixdoctests --distribution sagemath-graphs --only-tags --probe all src/sage/misc/c3_controlled.pyx
1 parent 0acb7c0 commit 7fa775d

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

src/sage/misc/c3_controlled.pyx

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,23 @@ key.
196196
We consider the smallest poset describing a class hierarchy admitting
197197
no MRO whatsoever::
198198
199-
sage: P = Poset({10: [9,8,7], 9: [6,1], 8: [5,2], 7: [4,3], # needs sage.combinat sage.graphs
199+
sage: P = Poset({10: [9,8,7], 9: [6,1], 8: [5,2], 7: [4,3], # needs sage.graphs
200200
....: 6: [3,2], 5: [3,1], 4: [2,1]},
201201
....: linear_extension=True, facade=True)
202202
203203
And build a :class:`HierarchyElement` from it::
204204
205205
sage: from sage.misc.c3_controlled import HierarchyElement
206-
sage: x = HierarchyElement(10, P) # needs sage.combinat sage.graphs
206+
sage: x = HierarchyElement(10, P) # needs sage.graphs
207207
208208
Here are its bases::
209209
210-
sage: HierarchyElement(10, P)._bases # needs sage.combinat sage.graphs
210+
sage: HierarchyElement(10, P)._bases # needs sage.graphs
211211
[9, 8, 7]
212212
213213
Using the standard ``C3`` algorithm fails::
214214
215-
sage: x.mro_standard # needs sage.combinat sage.graphs
215+
sage: x.mro_standard # needs sage.graphs
216216
Traceback (most recent call last):
217217
...
218218
ValueError: Cannot merge the items 3, 3, 2.
@@ -222,13 +222,13 @@ extension. For easy relabelling, we first need to set an appropriate
222222
default linear extension for `P`::
223223
224224
sage: linear_extension = list(reversed(IntegerRange(1, 11)))
225-
sage: P = P.with_linear_extension(linear_extension) # needs sage.combinat sage.graphs
226-
sage: list(P) # needs sage.combinat sage.graphs
225+
sage: P = P.with_linear_extension(linear_extension) # needs sage.graphs
226+
sage: list(P) # needs sage.graphs
227227
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
228228
229229
Now we play with a specific linear extension of `P`::
230230
231-
sage: # needs sage.combinat sage.graphs
231+
sage: # needs sage.graphs
232232
sage: Q = P.linear_extension([10, 9, 8, 7, 6, 5, 4, 1, 2, 3]).to_poset()
233233
sage: Q.cover_relations()
234234
[[10, 9], [10, 8], [10, 7], [9, 6], [9, 3], [8, 5], [8, 2], [7, 4],
@@ -242,43 +242,43 @@ Now we play with a specific linear extension of `P`::
242242
On the other hand, both the instrumented ``C3`` algorithm, and the
243243
controlled ``C3`` algorithm give the desired MRO::
244244
245-
sage: x.mro # needs sage.combinat sage.graphs
245+
sage: x.mro # needs sage.graphs
246246
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
247-
sage: x.mro_controlled # needs sage.combinat sage.graphs
247+
sage: x.mro_controlled # needs sage.graphs
248248
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
249249
250250
The above checks, and more, can be run with::
251251
252-
sage: x._test_mro() # needs sage.combinat sage.graphs
252+
sage: x._test_mro() # needs sage.graphs
253253
254254
In practice, the control was achieved by adding the following bases::
255255
256-
sage: x._bases # needs sage.combinat sage.graphs
256+
sage: x._bases # needs sage.graphs
257257
[9, 8, 7]
258-
sage: x._bases_controlled # needs sage.combinat sage.graphs
258+
sage: x._bases_controlled # needs sage.graphs
259259
[9, 8, 7, 6, 5]
260260
261261
Altogether, four bases were added for control::
262262
263-
sage: sum(len(HierarchyElement(q, Q)._bases) for q in Q) # needs sage.combinat sage.graphs
263+
sage: sum(len(HierarchyElement(q, Q)._bases) for q in Q) # needs sage.graphs
264264
15
265-
sage: sum(len(HierarchyElement(q, Q)._bases_controlled) for q in Q) # needs sage.combinat sage.graphs
265+
sage: sum(len(HierarchyElement(q, Q)._bases_controlled) for q in Q) # needs sage.graphs
266266
19
267267
268268
This information can also be recovered with::
269269
270-
sage: x.all_bases_len() # needs sage.combinat sage.graphs
270+
sage: x.all_bases_len() # needs sage.graphs
271271
15
272-
sage: x.all_bases_controlled_len() # needs sage.combinat sage.graphs
272+
sage: x.all_bases_controlled_len() # needs sage.graphs
273273
19
274274
275275
We now check that the ``C3`` algorithm fails for all linear extensions
276276
`l` of this poset, whereas both the instrumented and controlled ``C3``
277277
algorithms succeed; along the way, we collect some statistics::
278278
279-
sage: L = P.linear_extensions() # needs sage.combinat sage.graphs
279+
sage: L = P.linear_extensions() # needs sage.graphs
280280
sage: stats = []
281-
sage: for l in L: # needs sage.combinat sage.graphs sage.modules sage.ring.finite_rings
281+
sage: for l in L: # needs sage.graphs sage.modules
282282
....: x = HierarchyElement(10, l.to_poset())
283283
....: try: # Check that x.mro_standard always fails with a ValueError
284284
....: x.mro_standard
@@ -295,7 +295,7 @@ Depending on the linear extension `l` it was necessary to add between
295295
one and five bases for control; for example, `216` linear extensions
296296
required the addition of four bases::
297297
298-
sage: sorted(Word(stats).evaluation_sparse()) # needs sage.combinat sage.graphs sage.modules sage.ring.finite_rings
298+
sage: sorted(Word(stats).evaluation_sparse()) # needs sage.graphs sage.modules
299299
[(1, 36), (2, 108), (3, 180), (4, 216), (5, 180)]
300300
301301
We now consider a hierarchy of categories::
@@ -1118,9 +1118,9 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
11181118
The ``_from_value`` attribute is a function that can be used
11191119
to reconstruct an element of the hierarchy from its value::
11201120
1121-
sage: x._from_value # needs sage.combinat sage.graphs
1121+
sage: x._from_value # needs sage.graphs
11221122
Cached version of <cyfunction HierarchyElement.__classcall__.<locals>.f at ...>
1123-
sage: x._from_value(x.value) is x # needs sage.combinat sage.graphs
1123+
sage: x._from_value(x.value) is x # needs sage.graphs
11241124
True
11251125
"""
11261126
self.value = value
@@ -1135,9 +1135,9 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
11351135
EXAMPLES::
11361136
11371137
sage: from sage.misc.c3_controlled import HierarchyElement
1138-
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.combinat sage.graphs
1139-
sage: x = HierarchyElement(10, P) # needs sage.combinat sage.graphs
1140-
sage: x # needs sage.combinat sage.graphs
1138+
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.graphs
1139+
sage: x = HierarchyElement(10, P) # needs sage.graphs
1140+
sage: x # needs sage.graphs
11411141
10
11421142
"""
11431143
return repr(self.value)
@@ -1175,7 +1175,7 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
11751175
11761176
EXAMPLES::
11771177
1178-
sage: # needs sage.combinat sage.graphs
1178+
sage: # needs sage.graphs
11791179
sage: from sage.misc.c3_controlled import HierarchyElement, C3_sorted_merge, identity
11801180
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True)
11811181
sage: x = HierarchyElement(5, P)
@@ -1188,12 +1188,12 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
11881188
sage: x.mro
11891189
[7, 6, 5, 4, 3, 2, 1]
11901190
1191-
sage: C3_sorted_merge([[6, 4, 3], [5, 2, 1], [6, 5]], identity) # needs sage.combinat sage.graphs
1191+
sage: C3_sorted_merge([[6, 4, 3], [5, 2, 1], [6, 5]], identity) # needs sage.graphs
11921192
([6, 5, 4, 3, 2, 1], [6, 5, 4])
11931193
11941194
TESTS::
11951195
1196-
sage: assert all(isinstance(v, Integer) for v in x.mro) # needs sage.combinat sage.graphs
1196+
sage: assert all(isinstance(v, Integer) for v in x.mro) # needs sage.graphs
11971197
"""
11981198
bases = self._bases
11991199
result, suggestion = C3_sorted_merge([base.mro for base in bases]+[[base.value for base in bases]], key=self._key)
@@ -1212,7 +1212,7 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
12121212
12131213
EXAMPLES::
12141214
1215-
sage: # needs sage.combinat sage.graphs
1215+
sage: # needs sage.graphs
12161216
sage: from sage.misc.c3_controlled import HierarchyElement
12171217
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True)
12181218
sage: x = HierarchyElement(7, P)
@@ -1232,22 +1232,25 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
12321232
EXAMPLES::
12331233
12341234
sage: from sage.misc.c3_controlled import HierarchyElement, C3_merge
1235-
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True) # needs sage.combinat sage.graphs
1236-
sage: x = HierarchyElement(5, P) # needs sage.combinat sage.graphs
1237-
sage: x.mro_standard # needs sage.combinat sage.graphs
1235+
1236+
sage: # needs sage.graphs
1237+
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True)
1238+
sage: x = HierarchyElement(5, P)
1239+
sage: x.mro_standard
12381240
[5, 2, 1]
1239-
sage: x = HierarchyElement(6, P) # needs sage.combinat sage.graphs
1240-
sage: x.mro_standard # needs sage.combinat sage.graphs
1241+
sage: x = HierarchyElement(6, P)
1242+
sage: x.mro_standard
12411243
[6, 4, 3]
1242-
sage: x = HierarchyElement(7, P) # needs sage.combinat sage.graphs
1243-
sage: x.mro_standard # needs sage.combinat sage.graphs
1244+
sage: x = HierarchyElement(7, P)
1245+
sage: x.mro_standard
12441246
[7, 6, 4, 3, 5, 2, 1]
1247+
12451248
sage: C3_merge([[6, 4, 3], [5, 2, 1], [6, 5]])
12461249
[6, 4, 3, 5, 2, 1]
12471250
12481251
TESTS::
12491252
1250-
sage: assert all(isinstance(v, Integer) for v in x.mro_standard) # needs sage.combinat sage.graphs
1253+
sage: assert all(isinstance(v, Integer) for v in x.mro_standard) # needs sage.graphs
12511254
"""
12521255
bases = self._bases
12531256
return [self.value] + C3_merge([base.mro_standard for base in bases]+[[base.value for base in bases]])
@@ -1261,28 +1264,31 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
12611264
EXAMPLES::
12621265
12631266
sage: from sage.misc.c3_controlled import HierarchyElement, C3_merge
1264-
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True) # needs sage.combinat sage.graphs
1265-
sage: x = HierarchyElement(5, P) # needs sage.combinat sage.graphs
1266-
sage: x.mro_controlled # needs sage.combinat sage.graphs
1267+
1268+
sage: # needs sage.graphs
1269+
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True)
1270+
sage: x = HierarchyElement(5, P)
1271+
sage: x.mro_controlled
12671272
[5, 2, 1]
1268-
sage: x = HierarchyElement(6, P) # needs sage.combinat sage.graphs
1269-
sage: x.mro_controlled # needs sage.combinat sage.graphs
1273+
sage: x = HierarchyElement(6, P)
1274+
sage: x.mro_controlled
12701275
[6, 4, 3]
1271-
sage: x = HierarchyElement(7, P) # needs sage.combinat sage.graphs
1272-
sage: x.mro_controlled # needs sage.combinat sage.graphs
1276+
sage: x = HierarchyElement(7, P)
1277+
sage: x.mro_controlled
12731278
[7, 6, 5, 4, 3, 2, 1]
1274-
sage: x._bases # needs sage.combinat sage.graphs
1279+
sage: x._bases
12751280
[6, 5]
1276-
sage: x._bases_controlled # needs sage.combinat sage.graphs
1281+
sage: x._bases_controlled
12771282
[6, 5, 4]
1283+
12781284
sage: C3_merge([[6, 4, 3], [5, 2, 1], [6, 5]])
12791285
[6, 4, 3, 5, 2, 1]
12801286
sage: C3_merge([[6, 4, 3], [5, 2, 1], [6, 5, 4]])
12811287
[6, 5, 4, 3, 2, 1]
12821288
12831289
TESTS::
12841290
1285-
sage: assert all(isinstance(v, Integer) for v in x.mro_controlled) # needs sage.combinat sage.graphs
1291+
sage: assert all(isinstance(v, Integer) for v in x.mro_controlled) # needs sage.graphs
12861292
"""
12871293
return [self.value] + C3_merge([base.mro_controlled for base in self._bases]+[self._bases_controlled])
12881294

@@ -1304,9 +1310,9 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
13041310
EXAMPLES::
13051311
13061312
sage: from sage.misc.c3_controlled import HierarchyElement
1307-
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True) # needs sage.combinat sage.graphs
1308-
sage: x = HierarchyElement(7, P) # needs sage.combinat sage.graphs
1309-
sage: x._test_mro() # needs sage.combinat sage.graphs
1313+
sage: P = Poset({7: [5, 6], 5: [1, 2], 6: [3, 4]}, facade=True) # needs sage.graphs
1314+
sage: x = HierarchyElement(7, P) # needs sage.graphs
1315+
sage: x._test_mro() # needs sage.graphs
13101316
"""
13111317
for b in self._bases:
13121318
b._test_mro()
@@ -1328,7 +1334,7 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
13281334
13291335
EXAMPLES::
13301336
1331-
sage: # needs sage.combinat sage.graphs
1337+
sage: # needs sage.graphs
13321338
sage: from sage.misc.c3_controlled import HierarchyElement
13331339
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True)
13341340
sage: x = HierarchyElement(1, P)
@@ -1356,7 +1362,7 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
13561362
13571363
EXAMPLES::
13581364
1359-
sage: # needs sage.combinat sage.graphs
1365+
sage: # needs sage.graphs
13601366
sage: from sage.misc.c3_controlled import HierarchyElement
13611367
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True)
13621368
sage: HierarchyElement(1, P).all_bases()
@@ -1375,8 +1381,8 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
13751381
EXAMPLES::
13761382
13771383
sage: from sage.misc.c3_controlled import HierarchyElement
1378-
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.combinat sage.graphs
1379-
sage: HierarchyElement(30, P).all_bases_len() # needs sage.combinat sage.graphs
1384+
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.graphs
1385+
sage: HierarchyElement(30, P).all_bases_len() # needs sage.graphs
13801386
12
13811387
"""
13821388
return sum( len(x._bases) for x in self.all_bases())
@@ -1388,8 +1394,8 @@ class HierarchyElement(object, metaclass=ClasscallMetaclass):
13881394
EXAMPLES::
13891395
13901396
sage: from sage.misc.c3_controlled import HierarchyElement
1391-
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.combinat sage.graphs
1392-
sage: HierarchyElement(30, P).all_bases_controlled_len() # needs sage.combinat sage.graphs
1397+
sage: P = Poset((divisors(30), lambda x, y: y.divides(x)), facade=True) # needs sage.graphs
1398+
sage: HierarchyElement(30, P).all_bases_controlled_len() # needs sage.graphs
13931399
13
13941400
"""
13951401
return sum( len(x._bases_controlled) for x in self.all_bases())

0 commit comments

Comments
 (0)