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
/
cluster_snakegraph.py
855 lines (700 loc) · 27 KB
/
cluster_snakegraph.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
r"""
Snake Graphs
REFERENCES:
.. [CanakciSchiffler] Canakci and Schiffler,
*Snake graph calculus and cluster algebras from surfaces*
:arxiv:`1209.4617`
.. [MSW_Positivity] Musiker - Schiffler - Williams,
*Positivity for Cluster Algebras from Surfaces*,
:arxiv:`0906.0748`
.. [FominShapiroThurston] Fomin - Shapiro - Thurston,
*Cluster algebras and triangulated surfaces. part I: Cluster
complexes*,
:arxiv:`math/0608367`
.. SEEALSO::
Cluster triangulations closely interact with
:class:`~sage.combinat.cluster_algebra_quiver.cluster_seed.ClusterSeed`,
:class:`~sage.combinat.cluster_algebra_quiver.quiver.ClusterQuiver`
"""
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.structure.element import Element
from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass
from sage.structure.list_clone import ClonableArray
class SnakeGraph(ClonableArray):
"""
A snake graph is a connected sequence of square tiles. A square tile is considered
as a graph with four vertices and four edges in the obvious way,
and we use the following ascii art to visualize each tile::
north
--
west | | east
--
south
To build a snake graph, start with one tile, then glue a new tile so that
the new tile is glued to the north or the east of the previous tile.
See [MSW_Positivity]_ or [CanakciSchiffler]_.
Note that the edges of the graph are not labeled. Hence a snake graph is uniquely
determined by a list of positive integers (``shape``) such that their sum is
equal to the number of the snake graph's tiles, i.e. a snake graph is uniquely
determined by a composition of ``d``, where ``d`` is the number of tiles
of the snake graph.
INPUT:
- ``shape`` -- a tuple/list listing the sizes of the rows of the snake graph
EXAMPLES::
sage: SnakeGraph((2,1,3))
-- -- --
| | | |
-- -- --
| |
-- --
| | |
-- --
"""
__metaclass__ = InheritComparisonClasscallMetaclass
@staticmethod
def __classcall_private__(cls, shape):
"""
Create an snake graph.
EXAMPLES::
sage: SnakeGraph([2,1,3])
-- -- --
| | | |
-- -- --
| |
-- --
| | |
-- --
"""
from sage.combinat.composition import Compositions
from sympy import Sum
if not list(shape) in Compositions():
raise ValueError("The input must be a composition of positive integers")
SGs = SnakeGraphs(sum(shape))
return SGs(shape)
def __init__(self, parent, shape):
"""
Initialize ``self``.
TESTS::
sage: G = SnakeGraph((2,1,1))
sage: TestSuite(G).run()
"""
self._shape = list(shape)
ClonableArray.__init__(self, parent, shape)
def check(self):
"""
Check if ``self`` is a valid snake graph.
EXAMPLES::
sage: M = SnakeGraphs(3)
sage: M[0].check()
"""
if self not in self.parent():
raise ValueError("invalid snake graph")
def _repr_(self):
"""
Return a string representation of ``self``.
EXAMPLES::
sage: SnakeGraphs(5).list()[0:6]
[
--
| |
-- -- -- -- --
| | | | | | | | |
-- -- -- -- -- -- -- -- -- -- --
| | | | | | | | | | | | | | | |
-- -- -- -- -- -- -- -- -- -- -- --
| | | | | | | | | | | | | |
-- -- -- -- -- -- -- --
| | | | | | | | | | | |
-- , -- , -- , -- , -- , --
]
sage: SnakeGraphs(5).list()[6:11]
[
--
| |
-- -- -- -- --
| | | | | | | | |
-- -- -- -- -- -- -- -- -- -- -- --
| | | | | | | | | | | | | | | |
-- -- -- -- -- -- -- -- -- -- -- -- -- --
| | | | | | | | | | | | |
-- , -- , -- -- , -- -- , -- --
]
sage: SnakeGraphs(5).list()[12:16]
[
--
| |
-- -- -- --
| | | | | | |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
| | | | | | | | | | | | | | | | | | |
-- -- -- , -- -- -- , -- -- -- -- , -- -- -- -- --
]
"""
sh = self._shape
ret = ''
top_row = sh[-1]
skips = sum(sh[:-1])-(len(sh)-1)
white_sp = ' '
ret += white_sp* skips
ret +=' -- '
for i in range(1,top_row):
ret +='-- '
ret +='\n' + white_sp * skips + '| |'
for i in range(1,top_row):
ret +=' |'
for i in range(len(sh)-2,-1,-1):
r = sh[i]
skips += -(r-1)
ret +='\n' + white_sp * skips
for i in range(0,r+sh[i+1]-1):
ret +=' --'
ret +='\n' + white_sp * skips + '| |'
for i in range(1,r):
ret +=' |'
bottom_row = sh[0]
ret +='\n' + ' --'
for i in range(1,bottom_row):
ret +=' --'
return ret
def shape(self):
"""
Return the shape of ``self``. The shape is a list of positive integers,
which corresponds to the number of tiles on each row of the snake graph.
Recall that the shape of a snake graph uniquely determines a snake graph.
EXAMPLES::
sage: Gs = SnakeGraphs(9)
sage: Gs((3,3,3))
-- -- --
| | | |
-- -- -- -- --
| | | |
-- -- -- -- --
| | | |
-- -- --
sage: Gs((3,3,3)).shape()
[3, 3, 3]
"""
return self._shape
def __eq__(self, other):
"""
Check equality.
EXAMPLES::
sage: Gs = SnakeGraphs(9)
sage: G = Gs((3,2,4))
sage: G == Gs([3,2,4])
True
sage: G == Gs([3,3,3])
False
sage: G == 'I am a string'
False
"""
if isinstance(other, SnakeGraph):
return self._shape == other._shape
return False
def __ne__(self, other):
"""
Check not equals. This is needed because otherwise != gives a wrong result.
EXAMPLES::
sage: SnakeGraph([1,1,1])==SnakeGraph([3])
False
sage: SnakeGraph([1,1,1])!=SnakeGraph([3])
True
"""
return not self.__eq__(other)
def directions(self):
"""
Return the list DIRs of directions (either 'up' or 'right').
This list is of length `len(self)-1` and corresponds to all
the tiles of ``self`` except for the last tile.
Recall that we build a snake graph by starting with one tile, then glue
a new tile so that the new tile is glued to the north or the east of
the previous tile (see [MSW_Positivity]_ or [CanakciSchiffler]_).
The entry in position `k` the list DIRs is `up`
if the tile `k+1` is glued above tile `k`,
and `right` if the tile `k+1` is glued to the right of tile `k`.
EXAMPLES::
sage: G = SnakeGraph([1,3,3,1,2,4,2])
sage: G.directions()
['up',
'right',
'right',
'up',
'right',
'right',
'up',
'up',
'right',
'up',
'right',
'right',
'right',
'up',
'right']
sage: SnakeGraph([1]).directions()
[]
"""
temp_shape = self._shape[:]
temp_shape.reverse()
DIRs = []
for i in range(len(self._shape)):
r = temp_shape.pop()
DIRs.extend((r-1)*['right'])
if i == len(self._shape)-1:
break
DIRs.append('up')
return DIRs
def plot(self, rgb_color=(0,0,0), xy=(0, 0)):
"""
Return a plot of ``self``.
INPUT:
- ``rgb_color`` -- (default:(0,0,0), black) The color as an RGB tuple
- ``xy`` -- (default:(0,0)) snake graph will be plotted at xy=(a,b)
EXAMPLES::
sage: g=SnakeGraph((2,3,1))
sage: print g.plot(rgb_color=(1,0,1)).description()
Line defined by 5 points: [(1.0, 0.0), (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
Line defined by 5 points: [(2.0, 0.0), (1.0, 0.0), (1.0, 1.0), (2.0, 1.0), (2.0, 0.0)]
Line defined by 5 points: [(2.0, 1.0), (1.0, 1.0), (1.0, 2.0), (2.0, 2.0), (2.0, 1.0)]
Line defined by 5 points: [(3.0, 1.0), (2.0, 1.0), (2.0, 2.0), (3.0, 2.0), (3.0, 1.0)]
Line defined by 5 points: [(4.0, 1.0), (3.0, 1.0), (3.0, 2.0), (4.0, 2.0), (4.0, 1.0)]
Line defined by 5 points: [(4.0, 2.0), (3.0, 2.0), (3.0, 3.0), (4.0, 3.0), (4.0, 2.0)]
"""
from sage.plot.graphics import Graphics
from sage.plot.line import line
DIRs = self.directions()[:]
drawing = Graphics()
x, y = 0,0
(x,y)=xy
for pos in range(0,len(DIRs)+1):
tile_drawing = line([(x+1,y+0),(x+0,y+0),(x+0,y+1),(x+1,y+1),(x+1,y+0)],rgbcolor=rgb_color)
if pos < len(DIRs):
DIR = DIRs[pos]
if DIR == 'up':
y=y+1
else:
x=x+1
drawing = drawing + tile_drawing
drawing.axes(False)
drawing.set_aspect_ratio(1)
return drawing
class SnakeGraphs(Parent, UniqueRepresentation):
"""
Class of all snake graphs with `d` square tiles.
A snake graph is a connected sequence of square tiles.
To build a snake graph, start with one tile, then glue a new tile so that
the new tile is glued to the north or the east of the previous tile.
See [MSW_Positivity]_ or [CanakciSchiffler]_.
Note that the edges of the graph are not labeled. Hence snake graphs with `d`
tiles are in bijection with :class:`Compositions` (of positive integers)
with total sum `d`
.. SEEALSO::
:class:`SnakeGraph`
INPUT:
- ``d`` -- the number of tiles
EXAMPLES::
sage: M = SnakeGraphs(2)
sage: list(M)
[
--
| |
-- -- --
| | | | |
-- , -- --
]
"""
def __init__(self, d):
"""
Initialize ``self``.
EXAMPLES::
sage: M = SnakeGraphs(2)
sage: TestSuite(M).run()
"""
self._d = d
Parent.__init__(self, category=FiniteEnumeratedSets())
def _repr_(self):
"""
Return a string representation of ``self``.
EXAMPLES::
sage: SnakeGraphs(2)
Snake graphs with 2 tiles
"""
return "Snake graphs with {} tiles".format(self._d)
def _repr_option(self, key):
"""
Metadata about the ``_repr_()`` output.
See :meth:`sage.structure.parent._repr_option` for details.
EXAMPLES::
sage: Gs = SnakeGraphs(2)
sage: Gs._repr_option('element_ascii_art')
True
"""
if key == 'element_ascii_art':
return True
return Parent._repr_option(self, key)
def _element_constructor_(self, x):
"""
Construct an element of ``self``.
EXAMPLES::
sage: Gs = SnakeGraphs(6)
sage: g = Gs((2,1,3))
sage: g
-- -- --
| | | |
-- -- --
| |
-- --
| | |
-- --
sage: Gs(g)
-- -- --
| | | |
-- -- --
| |
-- --
| | |
-- --
sage: Gs((2,2))
Traceback (most recent call last):
...
ValueError: Input a composition of 6
sage: Gs(matrix([1,1]))
Traceback (most recent call last):
...
ValueError: [1 1] is not a SnakeGraph nor a list of positive integers
"""
if isinstance(x, SnakeGraph):
if x in self.parent():
return x
else:
raise ValueError("Cannot convert between Snake Graphs of different number of tiles")
elif isinstance(x, list) or isinstance(x, tuple) or isinstance(x, set):
if sum(x) == self._d:
return self.element_class(self, x)
else:
raise ValueError("Input a composition of {}".format(self._d))
else:
raise ValueError("{} is not a SnakeGraph nor a list of positive integers".format(x))
Element = SnakeGraph
def __iter__(self):
"""
Iterate through ``self``.
EXAMPLES::
sage: M = SnakeGraphs(2)
sage: list(M)
[
--
| |
-- -- --
| | | | |
-- , -- --
]
"""
from sage.combinat.composition import Compositions
for c in Compositions(self._d):
yield self.element_class(self,c)
def number_of_tiles(self):
"""
Return the number of tiles of the snake graph.
EXAMPLES::
sage: M = SnakeGraphs(4)
sage: M.number_of_tiles()
4
"""
return self._d
class LabeledSnakeGraph(SnakeGraph):
"""
A labeled snake graph is a snake graph in which each edge and each tile carries
a label or weight [CanakciSchiffler]_. For example, for snake graphs arising
from cluster algebras from surfaces [FominShapiroThurston]_, these labels are cluster variables.
See [MSW_Positivity]_.
In some situation we would like to consider the weights of the diagonals of the
snake graph tiles. The diagonal of a tile is defined to be an edge between the
northwest (NW) and southeast (SE) corners of a tile::
NW
--
|\ |
| \|
--
SE
.. SEEALSO::
:class:`SnakeGraph`
Note that :class:`LabeledSnakeGraph` differs from :class:`SnakeGraph` in that user
may specify two optional attributes ``diagonal_weights`` and ``weights``.
INPUT:
- ``shape`` -- a tuple/list listing the sizes of the rows of the snake graph
- ``weights`` -- (default:None) a list/tuple/dictionary giving the weight
of each edge of the snake graph
- ``diagonal_weights`` -- (default: None) a list/tuple/dictionary giving
the weight for the diagonal of each tile
- ``first_tile_orientation`` -- (default: 1) whether the orientation
of the first tile is 1 or -1
EXAMPLES::
sage: LabeledSnakeGraph((2,1,3))
-- -- --
| | | |
-- -- --
| |
-- --
| | |
-- --
with edge labels
sage: LabeledSnakeGraph((2,1),weights={0:('B1','B2','b','d'),\
....: 1:('a','c','B3','a'),2:('B3','B4','d','b')})
--
| |
-- --
| | |
-- --
with edge labels
sage: L = LabeledSnakeGraph([2,1],'my weight')
Traceback (most recent call last):
...
ValueError: weights must be a dictionary of length 3
"""
__metaclass__ = InheritComparisonClasscallMetaclass
def __init__(self, shape, weights={}, diagonal_weights={}, first_tile_orientation=1,\
from_surface=False):
"""
Initialize ``self``.
.. TODO::
For the input weights, we should check that if edge `e` belongs
to two tiles, the weight of `e` is consistent on both tiles.
TESTS::
sage: G = SnakeGraph((2,1,1))
sage: TestSuite(G).run()
"""
self._shape = list(shape)
self._weights = weights
self._diagonal_weights = diagonal_weights
self._first_tile_orientation = 1
self._from_surface = from_surface # TODO
if weights:
num_of_tiles = sum(shape)
if not isinstance(weights,dict) or len(weights) != num_of_tiles:
raise ValueError("weights must be a dictionary of length {}".format(num_of_tiles))
if weights.keys() != range(num_of_tiles) or\
not all(isinstance(v,(list,tuple,dict)) and len(v)==4 for v in weights.values()):
raise ValueError("weights must be in the form ",\
"{0:(S0,E0,N0,W0), 1:(S1,E1,N1,W1),..., d:(Sn,En,Nn,Wn)}, ",\
" where d is {}".format(num_of_tiles-1))
def _repr_(self):
"""
Return a string representation of ``self``.
EXAMPLES::
sage: LabeledSnakeGraph([1,1,2,1], weights={0:(1,2,3,4),1:(5,6,7,8),\
2:(9,8,7,6),3:(1,1,1,1),4:(2,2,2,2)})
--
| |
-- --
| | |
-- --
| |
--
| |
--
with edge labels
"""
sh = self._shape
ret = ''
top_row = sh[-1]
skips = sum(sh[:-1])-(len(sh)-1)
white_sp = ' '
ret += white_sp* skips
ret +=' -- '
for i in range(1,top_row):
ret +='-- '
ret +='\n' + white_sp * skips + '| |'
for i in range(1,top_row):
ret +=' |'
for i in range(len(sh)-2,-1,-1):
r = sh[i]
skips += -(r-1)
ret +='\n' + white_sp * skips
for i in range(0,r+sh[i+1]-1):
ret +=' --'
ret +='\n' + white_sp * skips + '| |'
for i in range(1,r):
ret +=' |'
bottom_row = sh[0]
ret +='\n' + ' --'
for i in range(1,bottom_row):
ret +=' --'
ret += '\nwith edge labels'
return ret
def __eq__(self, other):
"""
Check equality.
EXAMPLES::
sage: Gs = SnakeGraphs(9)
sage: G = Gs((3,2,4))
sage: G == Gs([3,2,4])
True
sage: G == Gs([3,3,3])
False
sage: G == 'I am a string'
False
"""
if isinstance(other, LabeledSnakeGraph):
return self._shape == other._shape and\
self._weights == other._weights and\
self._diagonal_weights == other._diagonal_weights
return False
def __ne__(self, other):
"""
Check not equals. This is needed because otherwise != gives a wrong result.
[TODO] maybe this is not needed here because this inherits __ne__from SnakeGraph
EXAMPLES::
sage: LabeledSnakeGraph([1,1,1]) == LabeledSnakeGraph([3])
False
sage: LabeledSnakeGraph([1,1,1])!= LabeledSnakeGraph([3])
True
sage: LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')}) == LabeledSnakeGraph([2,1])
False
sage: LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')}) == SnakeGraph([2,1])
False
sage: LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')}) != LabeledSnakeGraph([2,1])
True
sage: LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')}) != SnakeGraph([2,1])
True
sage: LabeledSnakeGraph([2,1]) == SnakeGraph([2,1])
False
sage: LabeledSnakeGraph([2,1]) != SnakeGraph([2,1])
True
"""
return not self.__eq__(other)
def weights(self):
"""
Return the weights for the edges of the snake graph.
EXAMPLES::
sage: L = LabeledSnakeGraph([1],{0:('south','east','north','west')})
sage: L.weights()
{0: ('south', 'east', 'north', 'west')}
"""
return self._weights
def plot(self, rgb_color=(0,0,0), xy=(0, 0), draw_weights=True,\
draw_diagonal_weights=True, text_color = (1,0,0)):
"""
Return a plot of ``self``.
INPUT:
- ``rgb_color`` -- (default:(0,0,0), black) The color as an RGB tuple
- ``xy`` -- (default:(0,0)) Snake graph will be plotted at xy=(a,b)
- ``text_color`` -- (default:(1,0,0), red) The color of the edge labels
EXAMPLES::
sage: L=LabeledSnakeGraph([2])
sage: print L.plot().description()
Line defined by 5 points: [(1.0, 0.0), (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
Line defined by 5 points: [(2.0, 0.0), (1.0, 0.0), (1.0, 1.0), (2.0, 1.0), (2.0, 0.0)]
Text '$+$' at the point (0.8,0.8)
Text '$-$' at the point (1.8,0.8)
Text '' at the point (0.0,0.5)
Text '' at the point (0.5,0.0)
Text '' at the point (0.5,0.5)
Text '' at the point (0.5,1.0)
Text '' at the point (1.0,0.5)
Text '' at the point (1.5,0.0)
Text '' at the point (1.5,0.5)
Text '' at the point (1.5,1.0)
Text '' at the point (2.0,0.5)
sage: L=LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')})
sage: print L.plot().description()
Line defined by 5 points: [(1.0, 0.0), (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
Line defined by 5 points: [(2.0, 0.0), (1.0, 0.0), (1.0, 1.0), (2.0, 1.0), (2.0, 0.0)]
Line defined by 5 points: [(2.0, 1.0), (1.0, 1.0), (1.0, 2.0), (2.0, 2.0), (2.0, 1.0)]
Text '$+$' at the point (0.8,0.8)
Text '$+$' at the point (1.8,1.8)
Text '$-$' at the point (1.8,0.8)
Text '' at the point (0.5,0.5)
Text '' at the point (1.5,0.5)
Text '' at the point (1.5,1.5)
Text 'B1' at the point (0.5,0.0)
Text 'B2' at the point (1.0,0.5)
Text 'B3' at the point (1.5,1.0)
Text 'B4' at the point (2.0,1.5)
Text 'a' at the point (1.5,0.0)
Text 'b' at the point (0.5,1.0)
Text 'b' at the point (1.0,1.5)
Text 'c' at the point (2.0,0.5)
Text 'd' at the point (0.0,0.5)
Text 'd' at the point (1.5,2.0)
sage: L=LabeledSnakeGraph([2,1],{0:('B1','B2','b','d'),1:('a','c','B3','a'),
....: 2:('B3','B4','d','b')},{0:'a',1:'b',2:'c'})
sage: print L.plot().description()
Line defined by 5 points: [(1.0, 0.0), (0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
Line defined by 5 points: [(2.0, 0.0), (1.0, 0.0), (1.0, 1.0), (2.0, 1.0), (2.0, 0.0)]
Line defined by 5 points: [(2.0, 1.0), (1.0, 1.0), (1.0, 2.0), (2.0, 2.0), (2.0, 1.0)]
Text '$+$' at the point (0.8,0.8)
Text '$+$' at the point (1.8,1.8)
Text '$-$' at the point (1.8,0.8)
Text 'B1' at the point (0.5,0.0)
Text 'B2' at the point (1.0,0.5)
Text 'B3' at the point (1.5,1.0)
Text 'B4' at the point (2.0,1.5)
Text 'a' at the point (0.5,0.5)
Text 'a' at the point (1.5,0.0)
Text 'b' at the point (0.5,1.0)
Text 'b' at the point (1.0,1.5)
Text 'b' at the point (1.5,0.5)
Text 'c' at the point (1.5,1.5)
Text 'c' at the point (2.0,0.5)
Text 'd' at the point (0.0,0.5)
Text 'd' at the point (1.5,2.0)
"""
if not draw_weights:
return SnakeGraph(self._shape).plot()
from sage.plot.graphics import Graphics
from sage.plot.line import line
from sage.plot.text import text
DIRs = self.directions()[:]
drawing = Graphics()
x, y = 0,0
(x,y)=xy
for pos in range(0,len(DIRs)+1):
tile_drawing = line([(x+1,y+0),(x+0,y+0),(x+0,y+1),(x+1,y+1),(x+1,y+0)],rgbcolor=rgb_color)
if self._weights:
floor = str(self._weights[pos][0])
right_side = str(self._weights[pos][1])
ceiling = str(self._weights[pos][2])
left_side = str(self._weights[pos][3])
else:
floor, right_side, ceiling, left_side = str(),str(),str(),str()
if self._diagonal_weights:
diagonal = str(self._diagonal_weights[pos])
else:
diagonal =str()
if self._first_tile_orientation == 1:
orientation=(-1)**pos
elif self._first_tile_orientation == -1:
orientation=-1*(-1)**pos
if orientation == 1: orientation='$+$'
else: orientation='$-$'
labels = text(diagonal,(x+0.5,y+0.5),vertical_alignment='bottom', rgbcolor=text_color)\
+ text(right_side,(x+1,y+0.5),horizontal_alignment='left', rgbcolor=text_color)\
+ text(ceiling,(x+0.5,y+1),vertical_alignment='bottom', rgbcolor=text_color) \
+ text(orientation,(x+0.8, y+0.8))
if pos>0:
PREVIOUS_DIR = DIRs[pos-1]
if PREVIOUS_DIR == 'right': # Then draw the label of the bottom edge
labels = labels + text(floor,(x+0.5,y+0),vertical_alignment='bottom', rgbcolor=text_color)
elif PREVIOUS_DIR == 'up': # Then draw the label of the left edge
labels = labels + text(left_side,(x+0,y+0.5),horizontal_alignment='left', rgbcolor=text_color)
else: # For the first tile, draw labels for both bottom and left edges
labels = labels + \
text(floor,(x+0.5,y+0),vertical_alignment='bottom', rgbcolor=text_color)\
+ text(left_side,(x+0,y+0.5),horizontal_alignment='left', rgbcolor=text_color)
if pos<len(DIRs):
DIR = DIRs[pos]
if DIR == 'up':
y=y+1
else:
x=x+1
drawing = drawing + tile_drawing + labels
drawing.axes(False)
drawing.set_aspect_ratio(1)
return drawing