Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add usefull function to FastAutomaton. Correct some bugs of doc. Corr…
Browse files Browse the repository at this point in the history
…ect a bug with wrong order of letters in plot2 and plot3 of BetaAdicMonoid (need more tests).
  • Loading branch information
mercatp committed Aug 18, 2014
1 parent 718704d commit 01f1f97
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 20 deletions.
77 changes: 71 additions & 6 deletions src/sage/combinat/words/automataC.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,12 +1427,6 @@ Automaton emondeI (Automaton a, bool verb)
return r;
}

/////////////////////////
//
// Tout le code qui suit est à tester !!!!!!!!!!!!!!!!!!!
//
/////////////////////////

Automaton SubAutomaton (Automaton a, Dict d, bool verb)
{
if (verb)
Expand Down Expand Up @@ -1492,3 +1486,74 @@ Automaton SubAutomaton (Automaton a, Dict d, bool verb)
free(l);
return r;
}

/////////////////////////
//
// Tout le code qui suit est à tester !!!!!!!!!!!!!!!!!!!
//
/////////////////////////

//permute les labels des arêtes
//l donne les anciens indices à partir des nouveaux
Automaton Permut (Automaton a, int *l, int na, bool verb)
{
if (verb)
{
int i;
printf("l = [ ");
for (i=0;i<na;i++)
{
printf("%d ", l[i]);
}
printf("]\n");
}
Automaton r = NewAutomaton(a.n, na);
int i,j;
for (i=0;i<a.n;i++)
{
for (j=0;j<na;j++)
{
if (l[j] != -1)
r.e[i].f[j] = a.e[i].f[l[j]];
else
r.e[i].f[j] = -1;
}
r.e[i].final = a.e[i].final;
}
r.i = a.i;
return r;
}

//permute les labels des arêtes SUR PLACE
//l donne les anciens indices à partir des nouveaux
void PermutOP (Automaton a, int *l, int na, bool verb)
{
if (verb)
{
int i;
printf("l = [ ");
for (i=0;i<na;i++)
{
printf("%d ", l[i]);
}
printf("]\n");
}
int *lf = (int*)malloc(sizeof(int)*a.na);
int i,j;
for (i=0;i<a.n;i++)
{
//sauvegarde les arêtes
for (j=0;j<a.na;j++)
{
lf[j] = a.e[i].f[j];
a.e[i].f[j] = -1;
}
//met les nouvelles
for (j=0;j<na;j++)
{
if (l[j] != -1)
a.e[i].f[j] = lf[l[j]];
}
}
free(lf);
}
5 changes: 5 additions & 0 deletions src/sage/combinat/words/automataC.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ Automaton emondeI (Automaton a, bool verb);

Automaton SubAutomaton (Automaton a, Dict d, bool verb);

//permute les labels des arêtes
//l donne les anciens indices à partir des nouveaux
Automaton Permut (Automaton a, int *l, int na, bool verb);
//idem mais SUR PLACE
void PermutOP (Automaton a, int *l, int na, bool verb);
74 changes: 74 additions & 0 deletions src/sage/combinat/words/cautomata.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ cdef extern from "automataC.h":
Automaton Transpose (Automaton a)
int StronglyConnectedComponents (Automaton a, int *res)
Automaton SubAutomaton (Automaton a, Dict d, bool verb)
Automaton Permut (Automaton a, int *l, int na, bool verb)
void PermutOP (Automaton a, int *l, int na, bool verb)

#dictionnaire numérotant l'alphabet projeté
cdef imagDict (dict d, list A, list A2=[]):
Expand Down Expand Up @@ -354,6 +356,9 @@ cdef class FastAutomaton:
def Alphabet (self):
return self.A

def setAlphabet (self, list A):
self.A = A

def initial_state (self):
return self.a.i

Expand All @@ -364,6 +369,11 @@ cdef class FastAutomaton:
l.append(i)
return l

def succ (self, int i, int j):
if i<0 or i>=self.a.n or j<0 or j>=self.a.na:
return -1
return self.a.e[i].f[j]

def emonde_inf (self, verb=False):
sig_on()
cdef Automaton a
Expand Down Expand Up @@ -469,6 +479,70 @@ cdef class FastAutomaton:
sig_off()
return r

#change les lettres
#le dictionnaire est supposé bijectif de A dans le nouvel alphabet
#opération sur place !
def relabel (self, dict d):
self.A = [d[c] for c in self.A]

#permute les lettres
#A = liste des lettres dans le nouvel ordre (il peut y en avoir moins)
def permut (self, list A, verb=False):
if verb:
print "A=%s"%A
sig_on()
cdef Automaton a
r = FastAutomaton(None)
cdef int *l = <int*>malloc(sizeof(int)*len(A))
cdef int i
for i in range(self.a.na):
l[i] = -1
d = {}
for i,c in enumerate(self.A):
d[c] = i
for i,c in enumerate(A):
if d.has_key(c):
l[i] = d[c] #l gives the old index from the new one
if verb:
str = "l=["
for i in range(len(A)):
str+=" %s"%l[i]
str+=" ]"
print str
a = Permut (self.a[0], l, len(A), verb)
free(l)
r.a[0] = a
r.A = A
sig_off()
return r

#permute les lettres SUR PLACE
#A = liste des lettres dans le nouvel ordre (il peut y en avoir moins)
def permut_op (self, list A, verb=False):
if verb:
print "A=%s"%A
sig_on()
cdef int *l = <int*>malloc(sizeof(int)*len(A))
cdef int i
for i in range(self.a.na):
l[i] = -1
d = {}
for i,c in enumerate(self.A):
d[c] = i
for i,c in enumerate(A):
if d.has_key(c):
l[i] = d[c] #l gives the old index from the new one
if verb:
str = "l=["
for i in range(len(A)):
str+=" %s"%l[i]
str+=" ]"
print str
PermutOP (self.a[0], l, len(A), verb)
free(l)
self.A = A
sig_off()

def transpose (self):
sig_on()
r = FastAutomaton(None)
Expand Down
6 changes: 3 additions & 3 deletions src/sage/combinat/words/morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -2290,9 +2290,9 @@ def rauzy_fractal_projection_exact(self, eig=None):
sage: t = WordMorphism('1->12,2->3,3->45,4->5,5->6,6->7,7->8,8->1')
sage: E = t.incidence_matrix().eigenvalues()
sage: x = [x for x in E if -0.8 < x < -0.7][0]
sage: t.rauzy_fractal_projection(prec=10)
sage: t.rauzy_fractal_projection_exact()
{'1': 1, '3': b^2 - b, '2': b - 1, '5': b - 1, '4': -b^2 + 2, '7': -b^2 + b + 1, '6': b^2 - b, '8': b^2 - 1}
sage: t.rauzy_fractal_projection(eig=x, prec=10)
sage: t.rauzy_fractal_projection_exact(eig=x)
{'1': 1, '3': b^2 - b, '2': b - 1, '5': b^2 - 2*b + 1, '4': -b^2 + 2*b - 2, '7': -b + 1, '6': -b^2 + b - 1, '8': -b^2 + b}
AUTHOR:
Expand Down Expand Up @@ -3095,7 +3095,7 @@ def rauzy_fractal_beta_adic_monoid (self, I=None, eig=None, tss=True, det=True,
EXAMPLES::
sage: WordMorphism('a->ab,b->ac,c->a').rauzy_fractal_beta_adic_monoid()
Monoid of b-adic expansion with b root of x^3-x^2-x-1 and numerals set {0, 1, b-1}
Monoid of b-adic expansion with b root of x^3 - x^2 - x - 1 and numerals set {0, 1} with subshift of 3 states
"""
alphabet = self.domain().alphabet()

Expand Down
31 changes: 24 additions & 7 deletions src/sage/monoids/beta_adic_monoid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ from sage.combinat.words.automata import Automaton

#from sage.structure.factory import UniqueFactory
#from sage.misc.cachefunc import cached_method
include "sage/ext/interrupt.pxi"

#calcul de la valeur absolue p-adique (car non encore implémenté autrement)
def absp (c, p, d):
Expand Down Expand Up @@ -148,12 +149,15 @@ cdef surface_to_img (Surface s):
img.save("/Users/mercat/Desktop/output.png")
img.save("output.png")

cdef Automate getAutomate (a, d, iss=None, verb=False):
cdef Automate getAutomate (a, d, list C, iss=None, verb=False):
cdef int i
if verb:
print "getAutomate %s..."%a
cdef FastAutomaton fa
if isinstance(a, FastAutomaton):
fa = a
fa.permut_op(C, verb=verb)
#fa = fa.permut(C, verb=verb)
return fa.a[0]
#assume in the following that a is a Automaton
lv = a.vertices()
Expand All @@ -165,7 +169,6 @@ cdef Automate getAutomate (a, d, iss=None, verb=False):
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:
Expand Down Expand Up @@ -230,17 +233,21 @@ cdef BetaAdic getBetaAdic (self, prec=53, ss=None, tss=None, iss=None, transpose
else:
a = ss

C = set(self.C)
if add_letters:
C = set(self.C)
C.update(a.Alphabet())
C = list(C)

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(a, d, iss=iss, verb=verb)
#if isinstance(a, FastAutomaton):
# a = a.permut(C, verb=verb)
b.a = getAutomate(a, d, C=C, iss=iss, verb=verb)
return b

cdef BetaAdic2 getBetaAdic2 (self, la=None, ss=None, tss=None, prec=53, add_letters=True, verb=False):
Expand All @@ -253,10 +260,12 @@ cdef BetaAdic2 getBetaAdic2 (self, la=None, ss=None, tss=None, prec=53, add_lett
if la is None:
la = self.get_la(ss=ss, tss=tss, verb=verb)

C = set(self.C)
if add_letters:
C = set(self.C)
for a in la:
C.update(a.Alphabet())
C = list(C)

b = NewBetaAdic2(len(C), len(la))
b.b = complex(CC(self.b))
d = {}
Expand All @@ -265,7 +274,9 @@ cdef BetaAdic2 getBetaAdic2 (self, la=None, ss=None, tss=None, prec=53, add_lett
d[c] = i
#automata
for i in range(len(la)):
b.a[i] = getAutomate(la[i], d, iss=None, verb=verb);
#if isinstance(la[i], FastAutomaton):
# la[i] = la[i].permut(C, verb=verb)
b.a[i] = getAutomate(la[i], d, C=C, iss=None, verb=verb);
return b

def PrintWord (m, n):
Expand Down Expand Up @@ -680,9 +691,12 @@ class BetaAdicMonoid(Monoid_class):
sage: m.plot2(19) # long time
"""
sig_on()
cdef Surface s = NewSurface (sx, sy)
cdef BetaAdic b
b = getBetaAdic(self, prec=prec, tss=tss, ss=ss, iss=iss, add_letters=add_letters, transpose=True, verb=verb)
#if verb:
# printAutomaton(b.a)
#dessin
cdef Color col
col.r = color[0]
Expand All @@ -708,6 +722,7 @@ class BetaAdicMonoid(Monoid_class):
if not isinstance(tss, FastAutomaton):
FreeAutomate(b.a)
FreeBetaAdic(b)
sig_off()

def plot3 (self, n=None, la=None, ss=None, tss=None, sx=800, sy=600, ajust=True, prec=53, colormap = 'hsv', backcolor=None, opacity = 1., add_letters=True, verb=False):
r"""
Expand Down Expand Up @@ -788,6 +803,7 @@ class BetaAdicMonoid(Monoid_class):
sage: m.plot2(19) # long time
"""
sig_on()
cdef Surface s = NewSurface (sx, sy)
cdef BetaAdic2 b
b = getBetaAdic2(self, la=la, ss=ss, tss=tss, prec=prec, add_letters=add_letters, verb=verb)
Expand Down Expand Up @@ -833,6 +849,7 @@ class BetaAdicMonoid(Monoid_class):
FreeAutomate(b.a[i])
FreeBetaAdic2(b)
FreeColorList(cl)
sig_off()

def plot (self, n=None, place=None, ss=None, iss=None, prec=53, point_size=None, color='blue', verb=False):
r"""
Expand Down Expand Up @@ -1357,7 +1374,7 @@ class BetaAdicMonoid(Monoid_class):
sage: m = BetaAdicMonoid(3, {0,1,3})
sage: m.complexity()
7.06858347...
3.0...
"""
K = self.C[0].parent()
b = self.b
Expand Down
Loading

0 comments on commit 01f1f97

Please sign in to comment.