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
/
beta_adic_monoid.pyx
1757 lines (1419 loc) · 61.3 KB
/
beta_adic_monoid.pyx
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=utf8
r"""
Beta-adic Monoids
AUTHORS:
- Paul Mercat (2013)
Beta-adic monoids are finitely generated monoids with generators of the form
x -> beta*x + c
where beta is a element of a field (for example a complex number),
and c is varying in a finite set of numerals.
It permits to describe beta-adic expansions, that is writing of numbers of the form
x = c_0 + c_1*beta + c_2*beta^2 + ...
for c_i's in a finite set of numerals.
"""
#*****************************************************************************
# Copyright (C) 2013 Paul Mercat <mercatp@icloud.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU General Public License for more details; the full text
# is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.rings.integer import Integer
from sage.rings.number_field.all import *
#from sage.structure.parent_gens import normalize_names
#from free_monoid_element import FreeMonoidElement
from sage.sets.set import Set
from monoid import Monoid_class
from sage.rings.qqbar import QQbar
from sage.rings.padics.all import *
from sage.combinat.words.automata import Automaton
#from sage.structure.factory import UniqueFactory
#from sage.misc.cachefunc import cached_method
#calcul de la valeur absolue p-adique (car non encore implémenté autrement)
def absp (c, p, d):
return ((c.polynomial())(p).norm().abs())**(1/d)
#garde la composante fortement connexe de 0
def emonde (a, K):
for s in a.strongly_connected_components_subgraphs():
if K.zero() in s:
return s
cdef extern from "draw.c":
cdef cppclass Etat:
int* f
int final
cdef cppclass Automate:
int n
int na
Etat* e
int i
ctypedef unsigned char uint8
cdef cppclass Color:
uint8 r
uint8 g
uint8 b
uint8 a
cdef cppclass Surface:
Color **pix
int sx, sy
cdef cppclass Complexe:
double x
double y
cdef cppclass BetaAdic:
Complexe b
Complexe *t #liste des translations
int n #nombre de translations
Automate a
Surface NewSurface (int sx, int sy)
void FreeSurface (Surface s)
Automate NewAutomate (int n, int na)
void FreeAutomate(Automate a)
BetaAdic NewBetaAdic (int n)
void FreeBetaAdic (BetaAdic b)
void Draw (BetaAdic b, Surface s, int n, int ajust, Color col, int verb)
void Draw2 (BetaAdic b, Surface s, int n, int ajust, Color col, int verb)
void print_word (BetaAdic b, int n, int etat)
cdef Complexe complex (c):
cdef Complexe r
r.x = c.real()
r.y = c.imag()
return r
cdef surface_to_img (Surface s):
import numpy as np
from PIL import Image
arr = np.zeros([s.sy, s.sx], dtype = [('r', 'uint8'), ('g', 'uint8'), ('b', 'uint8'), ('a', 'uint8')])
cdef int x, y
cdef Color c
for x in range(s.sx):
for y in range(s.sy):
c = s.pix[x][s.sy -y-1]
arr[y,x][0] = c.r
arr[y,x][1] = c.g
arr[y,x][2] = c.b
arr[y,x][3] = c.a
img = Image.fromarray(arr, 'RGBA')
img.save("/Users/mercat/Desktop/output.png")
img.save("output.png")
cdef Automate getAutomate (a, d, iss=None, verb=False):
if verb:
print "getAutomate %s..."%a
lv = a.vertices()
if hasattr(a, 'F'):
F = a.F
else:
F = lv
#alloue l'automate
cdef Automate r = NewAutomate(a.num_verts(), len(a.Alphabet()))
#réindice les sommets
dv = {}
cdef int i
for u,i in zip(lv, range(len(lv))):
dv[u] = i
if u in F:
r.e[i].final = 1
if verb:
print len(lv)
#copie l'automate en C
le = a.edges()
if verb:
print "len(le)=%s"%len(le)
for u,v,l in le:
if d.has_key(l):
#if dv.has_key(u) and dv.has_key(v):
r.e[dv[u]].f[d[l]] = dv[v]
#else:
# print "Erreur : pas de clef %s ou %s !"%(u,v)
else:
print "Erreur : pas de clef %s !"%l
if verb:
print "I..."
if iss is not None:
r.i = iss
else:
if hasattr(a, 'I') and len(a.I) > 0:
r.i = dv[list(a.I)[0]]
else:
raise ValueError("The initial state must be defined !")
if verb:
print "...getAutomate"
return r
cdef BetaAdic getBetaAdic (self, prec=53, ss=None, iss=None, add_letters=True, verb=False):
from sage.rings.complex_field import ComplexField
CC = ComplexField(prec)
cdef BetaAdic b
if ss is None:
if hasattr(self, 'ss'):
ss = self.ss
else:
ss = self.default_ss()
C = set(self.C)
if add_letters:
C.update(ss.Alphabet())
b = NewBetaAdic(len(C))
b.b = complex(CC(self.b))
d = {}
for i,c in zip(range(b.n), C):
b.t[i] = complex(CC(c))
d[c] = i
#automaton
b.a = getAutomate(ss, d, iss=iss, verb=verb)
return b
def PrintWord (m, n):
b = getBetaAdic(m)
print_word(b, n, b.a.i)
class BetaAdicMonoid(Monoid_class):
r"""
``b``-adic monoid with numerals set ``C``.
It is the beta-adic monoid generated by the set of affine transformations ``{x -> b*x + c | c in C}``.
EXAMPLES::
sage: m1 = BetaAdicMonoid(3, {0,1,3})
sage: m2 = BetaAdicMonoid((1+sqrt(5))/2, {0,1})
sage: b = (x^3-x-1).roots(ring=QQbar)[0][0]
sage: m3 = BetaAdicMonoid(b, {0,1})
"""
def __init__ (self, b, C):
r"""
Construction of the b-adic monoid generated by the set of affine transformations ``{x -> b*x + c | c in C}``.
EXAMPLES::
sage: m1 = BetaAdicMonoid(3, {0,1,3})
sage: m2 = BetaAdicMonoid((1+sqrt(5))/2, {0,1})
sage: b = (x^3-x-1).roots(ring=QQbar)[0][0]
sage: m3 = BetaAdicMonoid(b, {0,1})
"""
#print "init BAM with (%s,%s)"%(b,C)
if b in QQbar:
# print b
pi = QQbar(b).minpoly()
K = NumberField(pi, 'b', embedding = QQbar(b))
else:
K = b.parent()
try:
K.places()
except:
print "b=%s must be a algebraic number, ring %s not accepted."%(b,K)
# print K
self.b = K.gen() #beta (element of an NumberField)
# print "b="; print self.b
self.C = Set([K(c) for c in C]) #set of numerals
# print "C="; print self.C
def gen (self, i):
r"""
Return the element of C of index i.
"""
# g(x) = self.b*x+self.C[i]
return self.C[i]
def ngens (self):
r"""
Return the number of elements of C.
"""
return len(self.C)
def _repr_ (self):
r"""
Returns the string representation of the beta-adic monoid.
EXAMPLES::
sage: BetaAdicMonoid((1+sqrt(5))/2, {0,1})
Monoid of b-adic expansion with b root of x^2 - x - 1 and numerals set {0, 1}
sage: BetaAdicMonoid(3, {0,1,3})
Monoid of 3-adic expansion with numerals set {0, 1, 3}
TESTS::
sage: m=BetaAdicMonoid(3/sqrt(2), {0,1})
sage: repr(m)
'Monoid of b-adic expansion with b root of x^2 - 9/2 and numerals set {0, 1}'
"""
str = ""
if hasattr(self, 'ss'):
if self.ss is not None:
str=" with subshift of %s states"%self.ss.num_verts()
from sage.rings.rational_field import QQ
if self.b in QQ:
return "Monoid of %s-adic expansion with numerals set %s"%(self.b,self.C) + str
else:
K = self.b.parent()
if K.base_field() == QQ:
return "Monoid of b-adic expansion with b root of %s and numerals set %s"%(self.b.minpoly(),self.C) + str
else:
if K.characteristic() != 0:
return "Monoid of b-adic expansion with b root of %s and numerals set %s, in characteristic %s"%(self.b.minpoly(), self.C, K.characteristic()) + str
else:
return "Monoid of b-adic expansion with b root of %s and numerals set %s"%(K.modulus(),self.C) + str
def default_ss (self):
r"""
Returns the full subshift (given by an Automaton) corresponding to the beta-adic monoid.
EXAMPLES::
sage: m=BetaAdicMonoid((1+sqrt(5))/2, {0,1})
sage: m.default_ss()
Finite automaton with 1 states
"""
C = self.C
ss = Automaton()
ss.allow_multiple_edges(True)
ss.allow_loops(True)
ss.add_vertex(0)
for c in C:
ss.add_edge(0, 0, c)
ss.I = [0]
ss.F = [0]
ss.A = C
return ss
def points_exact (self, n=None, ss=None, iss=None):
r"""
Returns a set of exacts values (in the number field of beta) corresponding to the drawing of the limit set of the beta-adic monoid.
INPUT:
- ``n`` - integer (default: ``None``)
The number of iterations used to plot the fractal.
Default values: between ``5`` and ``16`` depending on the number of generators.
- ``ss`` - Automaton (default: ``None``)
The subshift to associate to the beta-adic monoid for this drawing.
- ``iss`` - set of initial states of the automaton ss (default: ``None``)
OUTPUT:
list of exact values
EXAMPLES:
#. The dragon fractal::
sage: m=BetaAdicMonoid(1/(1+i), {0,1})
sage: P = m.points_exact()
sage: len(P)
65536
"""
#global co
C = self.C
K = self.b.parent()
b = self.b
ng = C.cardinality()
if ss is None:
if hasattr(self, 'ss'):
ss = self.ss
else:
ss = self.default_ss()
if iss is None:
if hasattr(ss, 'I'):
iss = [i for i in ss.I][0]
if iss is None:
print "Donner l'état de départ iss de l'automate ss !"
return
if n is None:
if ng == 2:
n = 16
elif ng == 3:
n = 9
else:
n = 5
if n == 0:
#donne un point au hasard dans l'ensemble limite
#co = co+1
return [0]
else:
orbit_points = set()
V = set([v for c in C for v in [ss.succ(iss, c)] if v is not None])
orbit_points0 = dict()
for v in V:
orbit_points0[v] = self.points_exact(n=n-1, ss=ss, iss=v)
for c in C:
v = ss.succ(iss, c)
if v is not None:
orbit_points.update([b*p+c for p in orbit_points0[v]])
#orbit_points = orbit_points.union([b*p+c for p in self.points_exact(n=n-1, ss=ss, iss=v)])
#orbit_points0 = self.points_exact(n-1)
#orbit_points = Set([])
#for c in C:
# v = self.succ(i, c)
# if v is not None:
# orbit_points = orbit_points.union(Set([b*p+c for p in orbit_points0]))
#print "no=%s"%orbit_points.cardinality()
return orbit_points
def points (self, n=None, place=None, ss=None, iss=None, prec=53):
r"""
Returns a set of values (real or complex) corresponding to the drawing of the limit set of the beta-adic monoid.
INPUT:
- ``n`` - integer (default: ``None``)
The number of iterations used to plot the fractal.
Default values: between ``5`` and ``16`` depending on the number of generators.
- ``place`` - place of the number field of beta (default: ``None``)
The place we should use to evaluate elements of the number field given by points_exact()
- ``ss`` - Automaton (default: ``None``)
The subshift to associate to the beta-adic monoid for this drawing.
- ``iss`` - set of initial states of the automaton ss (default: ``None``)
- ``prec`` - precision of returned values (default: ``53``)
OUTPUT:
list of real or complex numbers
EXAMPLES:
#. The dragon fractal::
sage: m=BetaAdicMonoid(1/(1+I), {0,1})
sage: P = m.points()
sage: len(P)
32768
"""
C = self.C
K = self.b.parent()
b = self.b
ng = C.cardinality()
if n is None:
if ng == 2:
n = 18
elif ng == 3:
n = 14
elif ng == 4:
n = 10
elif ng == 5:
n = 7
else:
n = 5
from sage.functions.log import log
n = int(5.2/-log(abs(self.b.N(prec=prec))))
from sage.rings.complex_field import ComplexField
CC = ComplexField(prec)
if place is None:
if abs(b) < 1:
#garde la place courante
#place = lambda x: CC(x.n())
return [CC(c).N(prec) for c in self.points_exact(n=n, ss=ss, iss=iss)]
else:
#choisis une place
places = K.places()
place = places[0]
for p in places:
if abs(p(b)) < 1:
place = p
#break
#from sage.rings.qqbar import QQbar
#from sage.rings.qqbar import QQbar, AA
#if QQbar(self.b) not in AA:
# #print "not in AA !"
# return [(place(c).conjugate().N().real(), place(c).conjugate().N().imag()) for c in self.points_exact(n=n, ss=ss, iss=iss)]
#else:
# #print "in AA !"
# return [place(c).conjugate().N() for c in self.points_exact(n=n, ss=ss, iss=iss)]
return [place(c).N(prec) for c in self.points_exact(n=n, ss=ss, iss=iss)]
# if n == 0:
# #donne un point au hasard dans l'ensemble limite
# return [0]
# else:
# orbit_points0 = self.points(n-1)
# orbit_points = Set([])
# for c in C:
# orbit_points = orbit_points.union(Set([place(b)*p+place(c) for p in orbit_points0]))
# return orbit_points
def plot2 (self, n=None, ss=None, iss=None, sx=800, sy=600, ajust=True, prec=53, color=(0,0,0,255), method=0, add_letters=True, verb=False):
r"""
Draw the limit set of the beta-adic monoid (with or without subshift).
INPUT:
- ``n`` - integer (default: ``None``)
The number of iterations used to plot the fractal.
Default values: between ``5`` and ``16`` depending on the number of generators.
- ``place`` - place of the number field of beta (default: ``None``)
The place we should use to evaluate elements of the number field.
- ``ss`` - Automaton (default: ``None``)
The subshift to associate to the beta-adic monoid for this drawing.
- ``iss`` - set of initial states of the automaton ss (default: ``None``)
- ``sx, sy`` - dimensions of the resulting image (default : ``800, 600``)
- ``ajust`` - adapt the drawing to fill all the image, with ratio 1 (default: ``True``)
- ``prec`` - precision of returned values (default: ``53``)
- ``color`` - list of three integer between 0 and 255 (default: ``(0,0,255,255)``)
Color of the drawing.
- ``verb`` - bool (default: ``False``)
Print informations for debugging.
OUTPUT:
A Graphics object.
EXAMPLES:
#. The dragon fractal::
sage: m=BetaAdicMonoid(1/(1+I), {0,1})
sage: m.plot2() # long time
#. The Rauzy fractal of the Tribonacci substitution::
sage: s = WordMorphism('1->12,2->13,3->1')
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: m.plot2() # long time
#. A non-Pisot Rauzy fractal::
sage: s = WordMorphism({1:[3,2], 2:[3,3], 3:[4], 4:[1]})
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: m.b = 1/m.b
sage: m.ss = m.ss.transpose().determinize().minimize()
sage: m.plot2() # long time
#. The dragon fractal and its boundary::
sage: m = BetaAdicMonoid(1/(1+I), {0,1})
sage: p1 = m.plot() # long time
sage: ssi = m.intersection_words(w1=[0], w2=[1]) # long time
sage: p2 = m.plot2(ss = ssi, n=18) # long time
sage: p1+p2 # long time
#. The "Hokkaido" fractal and its boundary::
sage: s = WordMorphism('a->ab,b->c,c->d,d->e,e->a')
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: p1 = m.plot() # long time
sage: ssi = m.intersection_words(w1=[0], w2=[1]) # long time
sage: p2 = m.plot2(ss=ssi, n=40) # long time
sage: p1+p2 # long time
#. A limit set that look like a tiling::
sage: P=x^4 + x^3 - x + 1
sage: b = P.roots(ring=QQbar)[2][0]
sage: m = BetaAdicMonoid(b, {0,1})
sage: m.plot2(18) # long time
"""
cdef Surface s = NewSurface (sx, sy)
cdef BetaAdic b
b = getBetaAdic(self, prec=prec, ss=ss, iss=iss, add_letters=add_letters, verb=verb)
#dessin
cdef Color col
col.r = color[0]
col.g = color[1]
col.b = color[2]
col.a = color[3]
if n is None:
n = -1
if method == 0:
Draw(b, s, n, ajust, col, verb)
elif method == 1:
print "Not implemented !"
return
#lv = s.rauzy_fractal_projection_exact().values()
#for i,v in zip(range(len(lv)),lv):
# b.t[i] = complex(CC(v))
#Draw2(b, s, n, ajust, col, verb)
#enregistrement du résultat
surface_to_img(s)
FreeSurface(s)
FreeAutomate(b.a)
FreeBetaAdic(b)
def plot (self, n=None, place=None, ss=None, iss=None, prec=53, point_size=None, color='blue', verb=False):
r"""
Draw the limit set of the beta-adic monoid (with or without subshift).
INPUT:
- ``n`` - integer (default: ``None``)
The number of iterations used to plot the fractal.
Default values: between ``5`` and ``16`` depending on the number of generators.
- ``place`` - place of the number field of beta (default: ``None``)
The place we should use to evaluate elements of the number field given by points_exact()
- ``ss`` - Automaton (default: ``None``)
The subshift to associate to the beta-adic monoid for this drawing.
- ``iss`` - set of initial states of the automaton ss (default: ``None``)
- ``prec`` - precision of returned values (default: ``53``)
- ``point_size`` - real (default: ``None``)
Size of the plotted points.
- ``verb`` - bool (default: ``False``)
Print informations for debugging.
OUTPUT:
A Graphics object.
EXAMPLES:
#. The dragon fractal::
sage: m=BetaAdicMonoid(1/(1+I), {0,1})
sage: m.plot() # long time
#. The Rauzy fractal of the Tribonacci substitution::
sage: s = WordMorphism('1->12,2->13,3->1')
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: m.plot() # long time
#. A non-Pisot Rauzy fractal::
sage: s = WordMorphism({1:[3,2], 2:[3,3], 3:[4], 4:[1]})
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: m.b = 1/m.b
sage: m.ss = m.ss.transpose().determinize().minimize()
sage: m.plot() # long time
#. The dragon fractal and its boundary::
sage: m = BetaAdicMonoid(1/(1+I), {0,1})
sage: p1 = m.plot() # long time
sage: ssi = m.intersection_words(w1=[0], w2=[1]) # long time
sage: p2 = m.plot(ss = ssi, n=18) # long time
sage: p1+p2 # long time
#. The "Hokkaido" fractal and its boundary::
sage: s = WordMorphism('a->ab,b->c,c->d,d->e,e->a')
sage: m = s.rauzy_fractal_beta_adic_monoid()
sage: p1 = m.plot() # long time
sage: ssi = m.intersection_words(w1=[0], w2=[1]) # long time
sage: p2 = m.plot(ss=ssi, n=40) # long time
sage: p1+p2 # long time
#. A limit set that look like a tiling::
sage: P=x^4 + x^3 - x + 1
sage: b = P.roots(ring=QQbar)[2][0]
sage: m = BetaAdicMonoid(b, {0,1})
sage: m.plot(18) # long time
"""
global co
co = 0
orbit_points = self.points(n=n, place=place, ss=ss, iss=iss, prec=prec)
if verb: print "co=%s"%co
# Plot points size
if point_size is None:
point_size = 1
# Make graphics
from sage.plot.plot import Graphics
G = Graphics()
#dim = self.b.minpoly().degree()
from sage.rings.qqbar import QQbar, AA
if QQbar(self.b) not in AA: #2D plots
from sage.all import points
G = points(orbit_points, size=point_size, color=color)
else: # 1D plots
from sage.all import plot
G += plot(orbit_points, thickness=point_size, color=color)
# if plotbasis:
# from matplotlib import cm
# from sage.plot.arrow import arrow
# canonicalbasis_proj = self.rauzy_fractal_projection(eig=eig, prec=prec)
# for i,a in enumerate(alphabet):
# x = canonicalbasis_proj[a]
# G += arrow((-1.1,0), (-1.1,x[0]), color=cm.__dict__["gist_gray"](0.75*float(i)/float(size_alphabet))[:3])
# else:
# print "dimension too large !"
G.set_aspect_ratio(1)
return G
def _relations_automaton_rec (self, current_state, di, parch, pultra, m, Cd, ext, verb=False, niter=-1):
r"""
Used by relations_automaton()
"""
if niter == 0:
return di
global count
if verb and count%10000 == 0: print count
if count == 0:
return di
count -= 1
b = self.b
if not di.has_key(current_state):
di[current_state] = dict([])
for c in Cd: #parcours les transitions partant de current_state
#e = b*current_state + c #calcule l'état obtenu en suivant la transition c
e = (current_state + c)/b #calcule l'état obtenu en suivant la transition c
#if verb: print "b=%s, e=%s, cur=%s, c=%s, di=%s"%(b, e, current_state, c, di)
if not di.has_key(e): #détermine si l'état est déjà dans le dictionnaire
ok = True
#calcule les valeurs abolues pour déterminer si l'état n'est pas trop grand
for p in parch:
if not ext:
if p(e).abs() >= m[p]:
ok = False
break
else:
if p(e).abs() > m[p]+.000000001:
ok = False
break
if not ok:
continue #cesse de considérer cette transition
for p, d in pultra:
if absp(e, p, d) > m[p]:
#if verb: print "abs(%s)=%s trop grand !"%(e, absp(e, p, d))
ok = False
break
if ok:
#on ajoute l'état et la transition à l'automate
di[current_state][e] = c
di = self._relations_automaton_rec (current_state=e, di=di, parch=parch, pultra=pultra, m=m, Cd=Cd, ext=ext, verb=verb, niter=niter-1)
else:
#ajoute la transition
di[current_state][e] = c
return di
def relations_automaton (self, ext=False, ss=None, noss=False, verb=False, step=100, limit=None, niter=None):
r"""
Compute the relations automaton of the beta-adic monoid (with or without subshift).
See http://www.latp.univ-mrs.fr/~paul.mercat/Publis/Semi-groupes%20fortement%20automatiques.pdf for a definition of such automaton (without subshift).
INPUT:
- ``ext`` - bool (default: ``False``)
If True, compute the extended relations automaton (which permit to describe infinite words in the monoid).
- ``ss`` - Automaton (default: ``None``)
The subshift to associate to the beta-adic monoid for this operation.
- ``noss`` - bool (default: ``False``)
- ``verb`` - bool (default: ``False``)
If True, print informations for debugging.
- ``step`` - int (default: ``100``)
Stop to an intermediate state of the computing to verify that all is right.
- ``limit``- int (default: None)
Stop the computing after a number of states limit.
OUTPUT:
A Automaton.
EXAMPLES::
sage: m = BetaAdicMonoid(3, {0,1,3})
sage: m.relations_automaton()
Finite automaton with 3 states
sage: b = (x^3-x-1).roots(ring=QQbar)[0][0]
sage: m = BetaAdicMonoid(b, {0,1})
sage: m.relations_automaton()
Finite automaton with 179 states
REFERENCES:
.. [Me13] Mercat P.
Bull. SMF 141, fascicule 3, 2013.
"""
if not noss:
a = self.relations_automaton(ext=ext, ss=None, noss=True, verb=verb, step=step, limit=limit)
if not step:
return a
step = step-1
if ss is None:
if hasattr(self, 'ss'):
ss = self.ss
else:
return a #pas de sous-shift
if not step:
return ss
step = step-1
d=dict()
for u in self.C:
for v in self.C:
if not d.has_key(u-v):
d[u-v] = []
d[u - v] += [(u,v)]
if not step:
return d
step = step-1
ss = ss.emonde0_simplify()
P = ss.product(A=ss)
#P = P.emonde0_simplify()
if not step:
return P
step = step-1
a.relabel2(d)
if not step:
return a
step = step-1
a = a.intersection(A=P)
if not step:
return a
step = step-1
a = a.emonde0_simplify()
if not step:
return a
step = step-1
if not ext:
a.emondeF()
if not step:
return a
step = step-1
#a = a.determinize(A=a.A, noempty=True)
#if not step:
# return a
#step = step-1
#return a
return a.minimize()
K = self.C[0].parent()
b = self.b
if verb: print K
#détermine les places qu'il faut considérer
parch = []
for p in K.places(): #places archimédiennes
if p(b).abs() < 1:
parch += [p]
pi = K.defining_polynomial()
from sage.rings.arith import gcd
pi = pi/gcd(pi.list()) #rend le polynôme à coefficients entiers et de contenu 1
# den = pi.constant_coefficient().denominator()
# lp = (pi.list()[pi.degree()].numerator()*den).prime_divisors() #liste des nombres premiers concernés
lp = (Integer(pi.list()[0])).prime_divisors() #liste des nombres premiers concernés
pultra = [] #liste des places ultramétriques considérées
for p in lp:
#détermine toutes les places au dessus de p dans le corps de nombres K
k = Qp(p)
Kp = k['a']
a = Kp.gen()
for f in pi(a).factor():
kp = f[0].root_field('e')
# c = kp.gen()
if kp == k:
c = f[0].roots(kp)[0][0]
else:
c = kp.gen()
if verb: print "c=%s (abs=%s)"%(c, (c.norm().abs())**(1/f[0].degree()))
if (c.norm().abs())**(1/f[0].degree()) < 1: #absp(c, c, f[0].degree()) > 1:
pultra += [(c, f[0].degree())]
if verb: print "places: "; print parch; print pultra
#calcule les bornes max pour chaque valeur absolue
Cd = Set([c-c2 for c in self.C for c2 in self.C])
if verb: print "Cd = %s"%Cd
m = dict([])
for p in parch:
m[p] = max([p(c).abs() for c in Cd])/abs(1-p(p.domain().gen()).abs())
for p, d in pultra:
m[p] = max([absp(c, p, d) for c in Cd])
if verb: print "m = %s"%m
if verb: print K.zero().parent()
global count
#print limit
if limit is None:
count = -1
else:
count = limit
if niter is None:
niter = -1
#print count
if verb: print "Parcours..."
di = self._relations_automaton_rec (current_state=K.zero(), di=dict([]), parch=parch, pultra=pultra, m=m, Cd=Cd, ext=ext, verb=verb, niter=niter)
if count == 0:
print "Nombre max d'états atteint."
else:
if verb:
if limit is None:
print "%s états parcourus."%(-1-count)
else:
print "%s états parcourus."%(limit-count)
#a = Automaton([K.zero()], [K.zero()], di)
#if verb: print "di = %s"%di
res = Automaton(di, loops=True) #, multiedges=True)
if verb: print "Avant emondation : %s"%res
res.I = [K.zero()]
res.A = Set([c-c2 for c in self.C for c2 in self.C])
if verb: print "Emondation..."
if not ext:
res.F = [K.zero()]
res.emonde()
else:
#res = res.emonde0_simplify() #pour retirer les états puits
res.emonde0()
res.F = res.vertices()
return res
def relations_automaton2 (self, verb=False, step=100, limit=None, niter=None):
r"""
Do the same as relations_automaton, but avoid recursivity in order to avoid the crash of sage.
INPUT:
- ``verb`` - bool (default: ``False``)
If True, print informations for debugging.
- ``step`` - int (default: ``100``)
Stop to an intermediate state of the computing to verify that all is right.
- ``limit``- int (default: None)
Stop the computing after a number of states limit.
OUTPUT:
A Automaton.
"""
K = self.C[0].parent()
b = self.b
if verb: print K
#détermine les places qu'il faut considérer
parch = []
for p in K.places(): #places archimédiennes
if p(b).abs() < 1:
parch += [p]
pi = K.defining_polynomial()
from sage.rings.arith import gcd
pi = pi/gcd(pi.list()) #rend le polynôme à coefficients entiers et de contenu 1
lp = (Integer(pi.list()[0])).prime_divisors() #liste des nombres premiers concernés
pultra = [] #liste des places ultramétriques considérées
for p in lp:
#détermine toutes les places au dessus de p dans le corps de nombres K
k = Qp(p)
Kp = k['a']
a = Kp.gen()
for f in pi(a).factor():
kp = f[0].root_field('e')
if kp == k:
c = f[0].roots(kp)[0][0]
else:
c = kp.gen()
if verb: print "c=%s (abs=%s)"%(c, (c.norm().abs())**(1/f[0].degree()))
if (c.norm().abs())**(1/f[0].degree()) < 1: #absp(c, c, f[0].degree()) > 1:
pultra += [(c, f[0].degree())]
if verb: print "places: "; print parch; print pultra
#calcule les bornes max pour chaque valeur absolue
Cd = Set([c-c2 for c in self.C for c2 in self.C])
if verb: print "Cd = %s"%Cd
m = dict([])
for p in parch:
m[p] = max([p(c).abs() for c in Cd])/abs(1-p(p.domain().gen()).abs())
for p, d in pultra:
m[p] = max([absp(c, p, d) for c in Cd])
if verb: print "m = %s"%m
if verb: print K.zero().parent()
if limit is None:
count = -1
else:
count = limit
if niter is None:
niter = -1
if verb: print "Parcours..."
di=dict([])
S = [K.zero()] #set of states to look at
iter = 0
while len(S) != 0: