This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
partition.py
8872 lines (7123 loc) · 279 KB
/
partition.py
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
r"""
Integer partitions
A partition `p` of a nonnegative integer `n` is a
non-increasing list of positive integers (the *parts* of the
partition) with total sum `n`.
A partition can be depicted by a diagram made of rows of cells,
where the number of cells in the `i^{th}` row starting from
the top is the `i^{th}` part of the partition.
The coordinate system related to a partition applies from the top
to the bottom and from left to right. So, the corners of the
partition `[5, 3, 1]` are `[[0,4], [1,2], [2,0]]`.
For display options, see :obj:`Partitions.options`.
.. NOTE::
- Boxes is a synonym for cells. All methods will use 'cell' and 'cells'
instead of 'box' and 'boxes'.
- Partitions are 0 based with coordinates in the form of (row-index,
column-index).
- If given coordinates of the form ``(r, c)``, then use Python's
\*-operator.
- Throughout this documentation, for a partition `\lambda` we will denote
its conjugate partition by `\lambda^{\prime}`. For more on conjugate
partitions, see :meth:`Partition.conjugate()`.
- The comparisons on partitions use lexicographic order.
.. NOTE::
We use the convention that lexicographic ordering is read from
left-to-right. That is to say `[1, 3, 7]` is smaller than `[2, 3, 4]`.
AUTHORS:
- Mike Hansen (2007): initial version
- Dan Drake (2009-03-28): deprecate RestrictedPartitions and implement
Partitions_parts_in
- Travis Scrimshaw (2012-01-12): Implemented latex function to Partition_class
- Travis Scrimshaw (2012-05-09): Fixed Partitions(-1).list() infinite recursion
loop by saying Partitions_n is the empty set.
- Travis Scrimshaw (2012-05-11): Fixed bug in inner where if the length was
longer than the length of the inner partition, it would include 0's.
- Andrew Mathas (2012-06-01): Removed deprecated functions and added
compatibility with the PartitionTuple classes. See :trac:`13072`
- Travis Scrimshaw (2012-10-12): Added options. Made
``Partition_class`` to the element ``Partition``. ``Partitions*`` are now
all in the category framework except ``PartitionsRestricted`` (which will
eventually be removed). Cleaned up documentation.
- Matthew Lancellotti (2018-09-14): Added a bunch of "k" methods to Partition.
EXAMPLES:
There are `5` partitions of the integer `4`::
sage: Partitions(4).cardinality()
5
sage: Partitions(4).list()
[[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]]
We can use the method ``.first()`` to get the 'first' partition of a
number::
sage: Partitions(4).first()
[4]
Using the method ``.next(p)``, we can calculate the 'next' partition
after `p`. When we are at the last partition, ``None`` will be returned::
sage: Partitions(4).next([4])
[3, 1]
sage: Partitions(4).next([1,1,1,1]) is None
True
We can use ``iter`` to get an object which iterates over the partitions
one by one to save memory. Note that when we do something like
``for part in Partitions(4)`` this iterator is used in the background::
sage: g = iter(Partitions(4))
sage: next(g)
[4]
sage: next(g)
[3, 1]
sage: next(g)
[2, 2]
sage: for p in Partitions(4): print(p)
[4]
[3, 1]
[2, 2]
[2, 1, 1]
[1, 1, 1, 1]
We can add constraints to the type of partitions we want. For
example, to get all of the partitions of `4` of length `2`, we'd do the
following::
sage: Partitions(4, length=2).list()
[[3, 1], [2, 2]]
Here is the list of partitions of length at least `2` and the list of
ones with length at most `2`::
sage: Partitions(4, min_length=2).list()
[[3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]]
sage: Partitions(4, max_length=2).list()
[[4], [3, 1], [2, 2]]
The options ``min_part`` and ``max_part`` can be used to set constraints
on the sizes of all parts. Using ``max_part``, we can select
partitions having only 'small' entries. The following is the list
of the partitions of `4` with parts at most `2`::
sage: Partitions(4, max_part=2).list()
[[2, 2], [2, 1, 1], [1, 1, 1, 1]]
The ``min_part`` options is complementary to ``max_part`` and selects
partitions having only 'large' parts. Here is the list of all
partitions of `4` with each part at least `2`::
sage: Partitions(4, min_part=2).list()
[[4], [2, 2]]
The options ``inner`` and ``outer`` can be used to set part-by-part
constraints. This is the list of partitions of `4` with ``[3, 1, 1]`` as
an outer bound (that is, partitions of `4` contained in the partition
``[3, 1, 1]``)::
sage: Partitions(4, outer=[3,1,1]).list()
[[3, 1], [2, 1, 1]]
``outer`` sets ``max_length`` to the length of its argument. Moreover, the
parts of ``outer`` may be infinite to clear constraints on specific
parts. Here is the list of the partitions of `4` of length at most `3`
such that the second and third part are `1` when they exist::
sage: Partitions(4, outer=[oo,1,1]).list()
[[4], [3, 1], [2, 1, 1]]
Finally, here are the partitions of `4` with ``[1,1,1]`` as an inner
bound (i. e., the partitions of `4` containing the partition ``[1,1,1]``).
Note that ``inner`` sets ``min_length`` to the length of its argument::
sage: Partitions(4, inner=[1,1,1]).list()
[[2, 1, 1], [1, 1, 1, 1]]
The options ``min_slope`` and ``max_slope`` can be used to set
constraints on the slope, that is on the difference ``p[i+1]-p[i]`` of
two consecutive parts. Here is the list of the strictly decreasing
partitions of `4`::
sage: Partitions(4, max_slope=-1).list()
[[4], [3, 1]]
The constraints can be combined together in all reasonable ways.
Here are all the partitions of `11` of length between `2` and `4` such
that the difference between two consecutive parts is between `-3` and
`-1`::
sage: Partitions(11,min_slope=-3,max_slope=-1,min_length=2,max_length=4).list()
[[7, 4], [6, 5], [6, 4, 1], [6, 3, 2], [5, 4, 2], [5, 3, 2, 1]]
Partition objects can also be created individually with :class:`Partition`::
sage: Partition([2,1])
[2, 1]
Once we have a partition object, then there are a variety of
methods that we can use. For example, we can get the conjugate of a
partition. Geometrically, the conjugate of a partition is the
reflection of that partition through its main diagonal. Of course,
this operation is an involution::
sage: Partition([4,1]).conjugate()
[2, 1, 1, 1]
sage: Partition([4,1]).conjugate().conjugate()
[4, 1]
If we create a partition with extra zeros at the end, they will be dropped::
sage: Partition([4,1,0,0])
[4, 1]
sage: Partition([0])
[]
sage: Partition([0,0])
[]
The idea of a partition being followed by infinitely many parts of size
`0` is consistent with the ``get_part`` method::
sage: p = Partition([5, 2])
sage: p.get_part(0)
5
sage: p.get_part(10)
0
We can go back and forth between the standard and the exponential
notations of a partition. The exponential notation can be padded with
extra zeros::
sage: Partition([6,4,4,2,1]).to_exp()
[1, 1, 0, 2, 0, 1]
sage: Partition(exp=[1,1,0,2,0,1])
[6, 4, 4, 2, 1]
sage: Partition([6,4,4,2,1]).to_exp(5)
[1, 1, 0, 2, 0, 1]
sage: Partition([6,4,4,2,1]).to_exp(7)
[1, 1, 0, 2, 0, 1, 0]
sage: Partition([6,4,4,2,1]).to_exp(10)
[1, 1, 0, 2, 0, 1, 0, 0, 0, 0]
We can get the (zero-based!) coordinates of the corners of a
partition::
sage: Partition([4,3,1]).corners()
[(0, 3), (1, 2), (2, 0)]
We can compute the core and quotient of a partition and build
the partition back up from them::
sage: Partition([6,3,2,2]).core(3)
[2, 1, 1]
sage: Partition([7,7,5,3,3,3,1]).quotient(3)
([2], [1], [2, 2, 2])
sage: p = Partition([11,5,5,3,2,2,2])
sage: p.core(3)
[]
sage: p.quotient(3)
([2, 1], [4], [1, 1, 1])
sage: Partition(core=[],quotient=([2, 1], [4], [1, 1, 1]))
[11, 5, 5, 3, 2, 2, 2]
We can compute the `0-1` sequence and go back and forth::
sage: Partitions().from_zero_one([1, 1, 1, 1, 0, 1, 0])
[5, 4]
sage: all(Partitions().from_zero_one(mu.zero_one_sequence())
....: == mu for n in range(5) for mu in Partitions(n))
True
We can compute the Frobenius coordinates and go back and forth::
sage: Partition([7,3,1]).frobenius_coordinates()
([6, 1], [2, 0])
sage: Partition(frobenius_coordinates=([6,1],[2,0]))
[7, 3, 1]
sage: all(mu == Partition(frobenius_coordinates=mu.frobenius_coordinates())
....: for n in range(12) for mu in Partitions(n))
True
We use the lexicographic ordering::
sage: pl = Partition([4,1,1])
sage: ql = Partitions()([3,3])
sage: pl > ql
True
sage: PL = Partitions()
sage: pl = PL([4,1,1])
sage: ql = PL([3,3])
sage: pl > ql
True
"""
# ****************************************************************************
# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>,
#
# Distributed under the terms of the GNU General Public License (GPL)
# https://www.gnu.org/licenses/
# ****************************************************************************
from copy import copy
from itertools import accumulate
from sage.libs.pari.all import pari
from sage.libs.flint.arith import number_of_partitions as flint_number_of_partitions
from sage.arith.misc import multinomial
from sage.structure.global_options import GlobalOptions
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.symbolic.ring import var
from sage.misc.lazy_import import lazy_import
lazy_import('sage.combinat.skew_partition', 'SkewPartition')
lazy_import('sage.combinat.partition_tuple', 'PartitionTuple')
from sage.misc.misc_c import prod
from sage.misc.prandom import randrange
from sage.misc.cachefunc import cached_method, cached_function
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.sets.non_negative_integers import NonNegativeIntegers
from sage.rings.finite_rings.integer_mod_ring import IntegerModRing
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.semirings.all import NN
from sage.arith.all import factorial, gcd
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.integer import Integer
from sage.rings.infinity import infinity
from .combinat import CombinatorialElement
from . import tableau
from . import permutation
from . import composition
from sage.combinat.partitions import ZS1_iterator, ZS1_iterator_nk
from sage.combinat.integer_vector import IntegerVectors
from sage.combinat.integer_lists import IntegerListsLex
from sage.combinat.integer_vector_weighted import iterator_fast as weighted_iterator_fast
from sage.combinat.combinat_cython import conjugate
from sage.combinat.root_system.weyl_group import WeylGroup
from sage.combinat.combinatorial_map import combinatorial_map
from sage.groups.perm_gps.permgroup import PermutationGroup
from sage.graphs.dot2tex_utils import have_dot2tex
from sage.arith.all import binomial
class Partition(CombinatorialElement):
r"""
A partition `p` of a nonnegative integer `n` is a
non-increasing list of positive integers (the *parts* of the
partition) with total sum `n`.
A partition is often represented as a diagram consisting of **cells**,
or **boxes**, placed in rows on top of each other such that the number of
cells in the `i^{th}` row, reading from top to bottom, is the `i^{th}`
part of the partition. The rows are left-justified (and become shorter
and shorter the farther down one goes). This diagram is called the
**Young diagram** of the partition, or more precisely its Young diagram
in English notation. (French and Russian notations are variations on this
representation.)
The coordinate system related to a partition applies from the top
to the bottom and from left to right. So, the corners of the
partition ``[5, 3, 1]`` are ``[[0,4], [1,2], [2,0]]``.
For display options, see :meth:`Partitions.options`.
.. NOTE::
Partitions are 0 based with coordinates in the form of (row-index,
column-index). For example consider the partition
``mu=Partition([4,3,2,2])``, the first part is ``mu[0]`` (which is 4),
the second is ``mu[1]``, and so on, and the upper-left cell in English
convention is ``(0, 0)``.
A partition can be specified in one of the following ways:
- a list (the default)
- using exponential notation
- by Frobenius coordinates
- specifying its `0-1` sequence
- specifying the core and the quotient
See the examples below.
EXAMPLES:
Creating partitions though parents::
sage: mu = Partitions(8)([3,2,1,1,1]); mu
[3, 2, 1, 1, 1]
sage: nu = Partition([3,2,1,1,1]); nu
[3, 2, 1, 1, 1]
sage: mu == nu
True
sage: mu is nu
False
sage: mu in Partitions()
True
sage: mu.parent()
Partitions of the integer 8
sage: mu.size()
8
sage: mu.category()
Category of elements of Partitions of the integer 8
sage: nu.parent()
Partitions
sage: nu.category()
Category of elements of Partitions
sage: mu[0]
3
sage: mu[1]
2
sage: mu[2]
1
sage: mu.pp()
***
**
*
*
*
sage: mu.removable_cells()
[(0, 2), (1, 1), (4, 0)]
sage: mu.down_list()
[[2, 2, 1, 1, 1], [3, 1, 1, 1, 1], [3, 2, 1, 1]]
sage: mu.addable_cells()
[(0, 3), (1, 2), (2, 1), (5, 0)]
sage: mu.up_list()
[[4, 2, 1, 1, 1], [3, 3, 1, 1, 1], [3, 2, 2, 1, 1], [3, 2, 1, 1, 1, 1]]
sage: mu.conjugate()
[5, 2, 1]
sage: mu.dominates(nu)
True
sage: nu.dominates(mu)
True
Creating partitions using ``Partition``::
sage: Partition([3,2,1])
[3, 2, 1]
sage: Partition(exp=[2,1,1])
[3, 2, 1, 1]
sage: Partition(core=[2,1], quotient=[[2,1],[3],[1,1,1]])
[11, 5, 5, 3, 2, 2, 2]
sage: Partition(frobenius_coordinates=([3,2],[4,0]))
[4, 4, 1, 1, 1]
sage: Partitions().from_zero_one([1, 1, 1, 1, 0, 1, 0])
[5, 4]
sage: [2,1] in Partitions()
True
sage: [2,1,0] in Partitions()
True
sage: Partition([1,2,3])
Traceback (most recent call last):
...
ValueError: [1, 2, 3] is not an element of Partitions
Sage ignores trailing zeros at the end of partitions::
sage: Partition([3,2,1,0])
[3, 2, 1]
sage: Partitions()([3,2,1,0])
[3, 2, 1]
sage: Partitions(6)([3,2,1,0])
[3, 2, 1]
TESTS:
Check that only trailing zeros are stripped::
sage: TestSuite( Partition([]) ).run()
sage: TestSuite( Partition([4,3,2,2,2,1]) ).run()
sage: Partition([3,2,2,2,1,0,0,0])
[3, 2, 2, 2, 1]
sage: Partition([3,0,2,2,2,1,0])
Traceback (most recent call last):
...
ValueError: [3, 0, 2, 2, 2, 1, 0] is not an element of Partitions
sage: Partition([0,7,3])
Traceback (most recent call last):
...
ValueError: [0, 7, 3] is not an element of Partitions
"""
@staticmethod
def __classcall_private__(cls, mu=None, **keyword):
"""
This constructs a list from optional arguments and delegates the
construction of a :class:`Partition` to the ``element_class()`` call
of the appropriate parent.
EXAMPLES::
sage: Partition([3,2,1])
[3, 2, 1]
sage: Partition(exp=[2,1,1])
[3, 2, 1, 1]
sage: Partition(core=[2,1], quotient=[[2,1],[3],[1,1,1]])
[11, 5, 5, 3, 2, 2, 2]
"""
l = len(keyword)
if l == 0:
if mu is not None:
if isinstance(mu, Partition):
return mu
return _Partitions(list(mu))
if l == 1:
if 'beta_numbers' in keyword:
return _Partitions.from_beta_numbers(keyword['beta_numbers'])
elif 'exp' in keyword:
return _Partitions.from_exp(keyword['exp'])
elif 'frobenius_coordinates' in keyword:
return _Partitions.from_frobenius_coordinates(keyword['frobenius_coordinates'])
elif 'zero_one' in keyword:
return _Partitions.from_zero_one(keyword['zero_one'])
if l == 2 and 'core' in keyword and 'quotient' in keyword:
return _Partitions.from_core_and_quotient(keyword['core'], keyword['quotient'])
raise ValueError('incorrect syntax for Partition()')
def __setstate__(self, state):
r"""
In order to maintain backwards compatibility and be able to unpickle a
old pickle from ``Partition_class`` we have to override the default
``__setstate__``.
EXAMPLES::
sage: loads(b'x\x9ck`J.NLO\xd5K\xce\xcfM\xca\xccK,\xd1+H,*\xc9,\xc9\xcc\xcf\xe3\n\x80\xb1\xe2\x93s\x12\x8b\x8b\xb9\n\x195\x1b\x0b\x99j\x0b\x995BY\xe33\x12\x8b3\nY\xfc\x80\xac\x9c\xcc\xe2\x92B\xd6\xd8B6\r\x88IE\x99y\xe9\xc5z\x99y%\xa9\xe9\xa9E\\\xb9\x89\xd9\xa9\xf10N!{(\xa3qkP!G\x06\x90a\x04dp\x82\x18\x86@\x06Wji\x92\x1e\x00x0.\xb5')
[3, 2, 1]
sage: loads(dumps( Partition([3,2,1]) )) # indirect doctest
[3, 2, 1]
"""
if isinstance(state, dict): # for old pickles from Partition_class
self._set_parent(_Partitions)
self.__dict__ = state
else:
self._set_parent(state[0])
self.__dict__ = state[1]
def __init__(self, parent, mu):
"""
Initialize ``self``.
We assume that ``mu`` is a weakly decreasing list of
non-negative elements in ``ZZ``.
EXAMPLES::
sage: p = Partition([3,1])
sage: TestSuite(p).run()
TESTS:
Fix that tuples raise the correct error::
sage: Partition((3,1,7))
Traceback (most recent call last):
...
ValueError: [3, 1, 7] is not an element of Partitions
"""
if isinstance(mu, Partition):
# since we are (suppose to be) immutable, we can share the underlying data
CombinatorialElement.__init__(self, parent, mu._list)
else:
if mu and not mu[-1]:
# direct callers might assume that mu is not modified
mu = mu[:-1]
while mu and not mu[-1]:
mu.pop()
CombinatorialElement.__init__(self, parent, mu)
@cached_method
def __hash__(self):
r"""
Return the hash of ``self``.
TESTS::
sage: P = Partition([4,2,2,1])
sage: hash(P) == hash(P)
True
"""
return hash(tuple(self._list))
def _repr_(self):
r"""
Return a string representation of ``self`` depending on
:meth:`Partitions.options`.
EXAMPLES::
sage: mu=Partition([7,7,7,3,3,2,1,1,1,1,1,1,1]); mu # indirect doctest
[7, 7, 7, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1]
sage: Partitions.options.display="diagram"; mu
*******
*******
*******
***
***
**
*
*
*
*
*
*
*
sage: Partitions.options.display="list"; mu
[7, 7, 7, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1]
sage: Partitions.options.display="compact_low"; mu
1^7,2,3^2,7^3
sage: Partitions.options.display="compact_high"; mu
7^3,3^2,2,1^7
sage: Partitions.options.display="exp_low"; mu
1^7, 2, 3^2, 7^3
sage: Partitions.options.display="exp_high"; mu
7^3, 3^2, 2, 1^7
sage: Partitions.options.convention="French"
sage: mu = Partition([7,7,7,3,3,2,1,1,1,1,1,1,1]); mu # indirect doctest
7^3, 3^2, 2, 1^7
sage: Partitions.options.display="diagram"; mu
*
*
*
*
*
*
*
**
***
***
*******
*******
*******
sage: Partitions.options.display="list"; mu
[7, 7, 7, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1]
sage: Partitions.options.display="compact_low"; mu
1^7,2,3^2,7^3
sage: Partitions.options.display="compact_high"; mu
7^3,3^2,2,1^7
sage: Partitions.options.display="exp_low"; mu
1^7, 2, 3^2, 7^3
sage: Partitions.options.display="exp_high"; mu
7^3, 3^2, 2, 1^7
sage: Partitions.options._reset()
"""
return self.parent().options._dispatch(self, '_repr_', 'display')
def _ascii_art_(self):
"""
TESTS::
sage: ascii_art(Partitions(5).list())
[ * ]
[ ** * ]
[ *** ** * * ]
[ **** *** * ** * * ]
[ *****, * , ** , * , * , * , * ]
"""
from sage.typeset.ascii_art import AsciiArt
return AsciiArt(self._repr_diagram().splitlines(), baseline=0)
def _unicode_art_(self):
"""
TESTS::
sage: unicode_art(Partitions(5).list())
⎡ ┌┐ ⎤
⎢ ┌┬┐ ├┤ ⎥
⎢ ┌┬┬┐ ┌┬┐ ├┼┘ ├┤ ⎥
⎢ ┌┬┬┬┐ ┌┬┬┐ ├┼┴┘ ├┼┤ ├┤ ├┤ ⎥
⎢ ┌┬┬┬┬┐ ├┼┴┴┘ ├┼┼┘ ├┤ ├┼┘ ├┤ ├┤ ⎥
⎣ └┴┴┴┴┘, └┘ , └┴┘ , └┘ , └┘ , └┘ , └┘ ⎦
sage: Partitions.options.convention = "French"
sage: unicode_art(Partitions(5).list())
⎡ ┌┐ ⎤
⎢ ┌┐ ├┤ ⎥
⎢ ┌┐ ┌┐ ├┤ ├┤ ⎥
⎢ ┌┐ ┌┬┐ ├┤ ├┼┐ ├┤ ├┤ ⎥
⎢ ┌┬┬┬┬┐ ├┼┬┬┐ ├┼┼┐ ├┼┬┐ ├┼┤ ├┼┐ ├┤ ⎥
⎣ └┴┴┴┴┘, └┴┴┴┘, └┴┴┘, └┴┴┘, └┴┘, └┴┘, └┘ ⎦
sage: Partitions.options._reset()
"""
from sage.typeset.unicode_art import UnicodeArt
if not self._list:
return UnicodeArt(u'∅', baseline=0)
if self.parent().options.convention == "English":
data = list(self)
else:
data = list(reversed(self))
txt = [u'┌' + u'┬' * (data[0] - 1) + u'┐']
for i in range(len(data) - 1):
p = data[i]
q = data[i + 1]
if p < q:
txt += [u'├' + u'┼' * p + u'┬' * (q - p - 1) + u'┐']
elif p == q:
txt += [u'├' + u'┼' * (p - 1) + u'┤']
else:
txt += [u'├' + u'┼' * q + u'┴' * (p - q - 1) + u'┘']
txt += [u'└' + u'┴' * (data[-1] - 1) + u'┘']
return UnicodeArt(txt, baseline=0)
def _repr_list(self):
"""
Return a string representation of ``self`` as a list.
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_list())
[7, 7, 7, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1]
"""
return '[%s]' % ', '.join('%s' % m for m in self)
def _repr_exp_low(self):
"""
Return a string representation of ``self`` in exponential form (lowest
first).
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_exp_low())
1^7, 2, 3^2, 7^3
sage: print(Partition([])._repr_exp_low())
-
"""
if not self._list:
return '-'
exp = self.to_exp()
return '%s' % ', '.join('%s%s' % (m+1, '' if e==1 else '^%s'%e)
for (m,e) in enumerate(exp) if e > 0)
def _repr_exp_high(self):
"""
Return a string representation of ``self`` in exponential form (highest
first).
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_exp_high())
7^3, 3^2, 2, 1^7
sage: print(Partition([])._repr_exp_high())
-
"""
if not self._list:
return '-'
exp = self.to_exp()[::-1] # reversed list of exponents
M=max(self)
return '%s' % ', '.join('%s%s' % (M-m, '' if e==1 else '^%s'%e)
for (m,e) in enumerate(exp) if e>0)
def _repr_compact_low(self):
"""
Return a string representation of ``self`` in compact form (exponential
form with lowest first).
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_compact_low())
1^7,2,3^2,7^3
sage: print(Partition([])._repr_compact_low())
-
"""
if not self._list:
return '-'
exp = self.to_exp()
return '%s' % ','.join('%s%s' % (m+1, '' if e==1 else '^%s'%e)
for (m,e) in enumerate(exp) if e > 0)
def _repr_compact_high(self):
"""
Return a string representation of ``self`` in compact form (exponential
form with highest first).
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_compact_high())
7^3,3^2,2,1^7
sage: print(Partition([])._repr_compact_low())
-
"""
if not self._list:
return '-'
exp = self.to_exp()[::-1] # reversed list of exponents
M=max(self)
return '%s' % ','.join('%s%s' % (M-m, '' if e==1 else '^%s'%e)
for (m,e) in enumerate(exp) if e>0)
def _repr_diagram(self):
r"""
Return a representation of ``self`` as a Ferrers diagram.
EXAMPLES::
sage: print(Partition([7,7,7,3,3,2,1,1,1,1,1,1,1])._repr_diagram())
*******
*******
*******
***
***
**
*
*
*
*
*
*
*
"""
return self.ferrers_diagram()
def level(self):
"""
Return the level of ``self``, which is always 1.
This method exists only for compatibility with
:class:`PartitionTuples`.
EXAMPLES::
sage: Partition([4,3,2]).level()
1
"""
return 1
def components(self):
"""
Return a list containing the shape of ``self``.
This method exists only for compatibility with
:class:`PartitionTuples`.
EXAMPLES::
sage: Partition([3,2]).components()
[[3, 2]]
"""
return [ self ]
def _latex_(self):
r"""
Return a LaTeX version of ``self``.
For more on the latex options, see :meth:`Partitions.options`.
EXAMPLES::
sage: mu = Partition([2, 1])
sage: Partitions.options.latex='diagram'; latex(mu) # indirect doctest
{\def\lr#1{\multicolumn{1}{@{\hspace{.6ex}}c@{\hspace{.6ex}}}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[b]{*{2}c}\\
\lr{\ast}&\lr{\ast}\\
\lr{\ast}\\
\end{array}$}
}
sage: Partitions.options.latex='exp_high'; latex(mu) # indirect doctest
2,1
sage: Partitions.options.latex='exp_low'; latex(mu) # indirect doctest
1,2
sage: Partitions.options.latex='list'; latex(mu) # indirect doctest
[2, 1]
sage: Partitions.options.latex='young_diagram'; latex(mu) # indirect doctest
{\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[b]{*{2}c}\cline{1-2}
\lr{\phantom{x}}&\lr{\phantom{x}}\\\cline{1-2}
\lr{\phantom{x}}\\\cline{1-1}
\end{array}$}
}
sage: Partitions.options(latex="young_diagram", convention="french")
sage: Partitions.options.latex='exp_high'; latex(mu) # indirect doctest
2,1
sage: Partitions.options.latex='exp_low'; latex(mu) # indirect doctest
1,2
sage: Partitions.options.latex='list'; latex(mu) # indirect doctest
[2, 1]
sage: Partitions.options.latex='young_diagram'; latex(mu) # indirect doctest
{\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[t]{*{2}c}\cline{1-1}
\lr{\phantom{x}}\\\cline{1-2}
\lr{\phantom{x}}&\lr{\phantom{x}}\\\cline{1-2}
\end{array}$}
}
sage: Partitions.options._reset()
"""
return self.parent().options._dispatch(self, '_latex_', 'latex')
def _latex_young_diagram(self):
r"""
LaTeX output as a Young diagram.
EXAMPLES::
sage: print(Partition([2, 1])._latex_young_diagram())
{\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[b]{*{2}c}\cline{1-2}
\lr{\phantom{x}}&\lr{\phantom{x}}\\\cline{1-2}
\lr{\phantom{x}}\\\cline{1-1}
\end{array}$}
}
sage: print(Partition([])._latex_young_diagram())
{\emptyset}
"""
if not self._list:
return "{\\emptyset}"
from sage.combinat.output import tex_from_array
return tex_from_array([ ["\\phantom{x}"]*row_size for row_size in self._list ])
def _latex_diagram(self):
r"""
LaTeX output as a Ferrers' diagram.
EXAMPLES::
sage: print(Partition([2, 1])._latex_diagram())
{\def\lr#1{\multicolumn{1}{@{\hspace{.6ex}}c@{\hspace{.6ex}}}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[b]{*{2}c}\\
\lr{\ast}&\lr{\ast}\\
\lr{\ast}\\
\end{array}$}
}
sage: print(Partition([])._latex_diagram())
{\emptyset}
"""
if not self._list:
return "{\\emptyset}"
entry = self.parent().options("latex_diagram_str")
from sage.combinat.output import tex_from_array
return tex_from_array([ [entry]*row_size for row_size in self._list ], False)
def _latex_list(self):
r"""
LaTeX output as a list.
EXAMPLES::
sage: print(Partition([2, 1])._latex_list())
[2, 1]
sage: print(Partition([])._latex_list())
[]
"""
return repr(self._list)
def _latex_exp_low(self):
r"""
LaTeX output in exponential notation (lowest first).
EXAMPLES::
sage: print(Partition([2,2,1])._latex_exp_low())
1,2^{2}
sage: print(Partition([])._latex_exp_low())
{\emptyset}
"""
if not self._list:
return "{\\emptyset}"
exp = self.to_exp()
return '%s' % ','.join('%s%s' % (m+1, '' if e==1 else '^{%s}'%e)
for (m,e) in enumerate(exp) if e > 0)
def _latex_exp_high(self):
r"""
LaTeX output in exponential notation (highest first).
EXAMPLES::
sage: print(Partition([2,2,1])._latex_exp_high())
2^{2},1
sage: print(Partition([])._latex_exp_high())
{\emptyset}
"""
if not self._list:
return "{\\emptyset}"
exp = self.to_exp()[::-1] # reversed list of exponents
M = max(self)
return '%s' % ','.join('%s%s' % (M-m, '' if e==1 else '^{%s}'%e)
for (m,e) in enumerate(exp) if e>0)
def ferrers_diagram(self):
r"""
Return the Ferrers diagram of ``self``.
EXAMPLES::
sage: mu = Partition([5,5,2,1])
sage: Partitions.options(diagram_str='*', convention="english")
sage: print(mu.ferrers_diagram())
*****
*****
**
*
sage: Partitions.options(diagram_str='#')
sage: print(mu.ferrers_diagram())
#####
#####
##
#
sage: Partitions.options.convention="french"
sage: print(mu.ferrers_diagram())
#
##
#####
#####
sage: print(Partition([]).ferrers_diagram())
-