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

Commit

Permalink
Add a function zero_complete() for FastAutomaton, a function shift fo…
Browse files Browse the repository at this point in the history
…r BetaAdicMonoid (not well tested) and correct some little things.
  • Loading branch information
mercatp committed Jul 8, 2016
1 parent 8c1bf8a commit a45bb65
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 11 deletions.
58 changes: 57 additions & 1 deletion src/sage/combinat/words/automataC.c
Expand Up @@ -30,6 +30,13 @@ PyMODINIT_FUNC initdautomata(void)

typedef Automate Automaton;

/*
void printAutomaton (Automaton a)
{
printf("Automate ayant %d lettres, %d états, état initial %d.\n", a.na, a.n, a.i);
}
*/

Dict NewDict (int n)
{
Dict r;
Expand Down Expand Up @@ -550,6 +557,12 @@ bool IsCompleteAutomaton (Automaton a)
//return true iff a state was added
bool CompleteAutomaton (Automaton *a)
{
/*
if (a->n == 0)
{
AddEtat(a, false); //ajoute un état
}
*/
int ne = a->n; //nouvel état
int i,j;
bool add_etat = false;
Expand Down Expand Up @@ -772,8 +785,13 @@ void Product_rec(Automaton r, int i1, int i2, Automaton a1, Automaton a2, Dict d
}
}

Automaton Product(Automaton a1, Automaton a2, Dict d)
Automaton Product (Automaton a1, Automaton a2, Dict d, bool verb)
{
if (verb)
{
printAutomaton(a1);
printAutomaton(a2);
}
//printf("a1.na=%d, a2.na=%d\n", a1.na, a2.na);
//compte le nombre de lettres de l'alphabet final
int i, na=0;
Expand Down Expand Up @@ -1708,6 +1726,44 @@ Automaton Duplicate (Automaton a, InvertDict id, int na2, bool verb)
return r;
}

void ZeroComplete_rec (Automaton a, int etat, bool *vu, int l0, bool verb)
{
if (verb)
printf("etat %d ..\n", etat);
vu[etat] = true;
int i, e;
for (i=0;i<a.na;i++)
{
e = a.e[etat].f[i];
if (e != -1)
{
if (!vu[e])
ZeroComplete_rec(a, e, vu, l0, verb);
if (i == l0 && a.e[e].final)
a.e[etat].final = true;
}
}
}

void ZeroComplete (Automaton a, int l0, bool verb)
{
if (verb)
printf("l0 = %d\n", l0);
if (a.i == -1)
return;
bool *vu = (bool *)malloc(sizeof(bool)*a.n); //liste des sommets vus
if (!vu)
{
printf("Out of memory !\n");
exit(25);
}
int i;
for (i=0;i<a.n;i++)
vu[i] = false;
ZeroComplete_rec(a, a.i, vu, l0, verb);
free(vu);
}

int compteur = 0;
bool emonde_inf_rec (Automaton a, int etat)
{
Expand Down
5 changes: 4 additions & 1 deletion src/sage/combinat/words/automataC.h
Expand Up @@ -30,7 +30,7 @@ bool equalsAutomaton (Automaton a1, Automaton a2); //détermine si les automates
inline int contract (int i1, int i2, int n1);
inline int geti1 (int c, int n1);
inline int geti2 (int c, int n1);
Automaton Product(Automaton a1, Automaton a2, Dict d);
Automaton Product (Automaton a1, Automaton a2, Dict d, bool verb);
void AddEtat (Automaton *a, bool final);

struct Etats
Expand Down Expand Up @@ -114,6 +114,9 @@ Automaton DeterminiseN (NAutomaton a, bool puits);
//the result is assumed deterministic !!!!
Automaton Duplicate (Automaton a, InvertDict id, int na2, bool verb);

//ajoute tous les mots qui se complètent en un mot du langage en ajoutant des 0 à la fin
void ZeroComplete (Automaton a, int l0, bool verb);

//retire tous les états à partir desquels il n'y a pas de chemin infini
Automaton emonde_inf (Automaton a, bool verb);

Expand Down
38 changes: 30 additions & 8 deletions src/sage/combinat/words/cautomata.pyx
Expand Up @@ -49,9 +49,10 @@ cdef extern from "automataC.h":
void printAutomaton (Automaton a)
void plotTikZ (Automaton a, const char **labels, const char *graph_name, double sx, double sy)
void NplotTikZ (NAutomaton a, const char **labels, const char *graph_name, double sx, double sy)
Automaton Product(Automaton a1, Automaton a2, Dict d)
Automaton Product (Automaton a1, Automaton a2, Dict d, bool verb)
Automaton Determinise (Automaton a, Dict d, bool noempty, bool onlyfinals, bool nof, bool verb)
Automaton DeterminiseN (NAutomaton a, bool puits)
void ZeroComplete (Automaton a, int l0, bool verb)
Automaton emonde_inf (Automaton a, bool verb)
Automaton emonde (Automaton a, bool verb)
Automaton emondeI (Automaton a, bool verb)
Expand Down Expand Up @@ -205,7 +206,7 @@ def TestProduct (a1, a2, di):
d = getProductDict(di, list(a1.Alphabet()), list(a2.Alphabet()))
print "dictionnaire du produit :"
printDict(d)
c = Product(a, b, d)
c = Product(a, b, d, False)
print "résultat :"
printAutomaton(c)

Expand Down Expand Up @@ -492,6 +493,7 @@ cdef class FastAutomaton:

def setAlphabet (self, list A):
self.A = A
self.a[0].na = len(A)

def initial_state (self):
return self.a.i
Expand Down Expand Up @@ -554,6 +556,15 @@ cdef class FastAutomaton:
raise ValueError("set_succ(%s, %s) : index out of bounds !"%(i,j))
self.a.e[i].f[j] = k

def zero_completeOP (self, verb=False):
sig_on()
for i in range(len(self.A)):
if self.A[i] == 0:
l0 = i
break
ZeroComplete(self.a[0], l0, verb)
sig_off()

def emonde_inf (self, verb=False):
sig_on()
cdef Automaton a
Expand Down Expand Up @@ -594,6 +605,7 @@ cdef class FastAutomaton:
for la in self.A:
for lb in b.A:
d[(la,lb)] = (la,lb)
if verb: print d
sig_on()
cdef Automaton a
cdef Dict dC
Expand All @@ -607,21 +619,25 @@ cdef class FastAutomaton:
if verb:
print "dC="
printDict(dC)
a = Product(self.a[0], b.a[0], dC)
a = Product(self.a[0], b.a[0], dC, verb)
FreeDict(dC)
r.a[0] = a
r.A = Av
sig_off()
return r

def intersection (self, FastAutomaton a, verb=False):
def intersection (self, FastAutomaton a, verb=False, simplify=True):
d = {}
for l in self.A:
if l in a.A:
d[(l,l)] = l
if verb:
print "d=%s"%d
return self.product(a, d, verb=verb)
p = self.product(a, d, verb=verb)
if simplify:
return p.emonde().minimise()
else:
return p

#determine if the automaton is complete (i.e. with his hole state)
def is_complete (self):
Expand Down Expand Up @@ -682,7 +698,7 @@ cdef class FastAutomaton:
print "dC="
printDict(dC)
sig_on()
ap = Product(self.a[0], a.a[0], dC)
ap = Product(self.a[0], a.a[0], dC, verb)
FreeDict(dC)
sig_off()

Expand Down Expand Up @@ -732,7 +748,7 @@ cdef class FastAutomaton:
print "dC="
printDict(dC)
sig_on()
ap = Product(self.a[0], a.a[0], dC)
ap = Product(self.a[0], a.a[0], dC, verb)
FreeDict(dC)
sig_off()

Expand Down Expand Up @@ -786,7 +802,7 @@ cdef class FastAutomaton:
if self.a.i != -1:
self.a.i = self.a.e[self.a.i].f[a]

def unshift1 (self, int a):
def unshift1 (self, int a, final=False):
r = FastAutomaton(None)
sig_on()
cdef Automaton aut
Expand All @@ -796,6 +812,7 @@ cdef class FastAutomaton:
for i in range(aut.na):
aut.e[ne].f[i] = -1
aut.e[ne].f[a] = self.a.i
aut.e[ne].final = final
aut.i = ne
r.a[0] = aut
r.A = self.A
Expand Down Expand Up @@ -1071,6 +1088,11 @@ cdef class FastAutomaton:
cdef i
for i in range(self.a.n):
self.a.e[i].final = not self.a.e[i].final

def complementary (self):
a = self.copy()
a.complementaryOP()
return a

def included (self, FastAutomaton a, bool verb=False, step=None):
d = {}
Expand Down
36 changes: 35 additions & 1 deletion src/sage/monoids/beta_adic_monoid.pyx
Expand Up @@ -2602,7 +2602,15 @@ class BetaAdicMonoid(Monoid_class):
return self.adherence(tss=a, C=C, C2=nA)

#donne l'automate décrivant le translaté de +t de a, avec les chiffres A au départ et B à l'arrivée, le tout dans l'ensemble décrit par b
def move2 (self, t, FastAutomaton a, FastAutomaton b=None, list A=None, list B=None, verb=False):
def move2 (self, t, FastAutomaton a=None, FastAutomaton b=None, list A=None, list B=None, verb=False):
if a is None:
if hasattr(self, 'tss'):
if isinstance(self.tss, FastAutomaton):
a = self.tss
else:
a = FastAutomaton(self.tss)
else:
a = FastAutomaton(self.default_ss())
if b is None:
if hasattr(self, 'tss'):
if isinstance(self.tss, FastAutomaton):
Expand Down Expand Up @@ -2630,6 +2638,32 @@ class BetaAdicMonoid(Monoid_class):
ai = ai.determinise_proj(d, verb=verb)
return ai.minimise()

#Not well tested !!!!
#return the automaton recognizing the division by beta and translation t
def shift (self, FastAutomaton aa, FastAutomaton bb, t=0, verb=False):
#détermine la liste des successeurs de l'état initial
cdef int i
cdef int e
l = set()
for j in range(aa.a.na):
e = aa.a.e[aa.a.i].f[j]
if e != -1:
l.add(j)
l = list(l)
if verb:
print "états à considérer : %s"%l
#calcule l'union des automates translatés
a = FastAutomaton(None)
A = aa.Alphabet()
a.setAlphabet(A)
for i in range(0, len(l)):
a2 = aa.copy()
a2.set_initial_state(aa.a.e[aa.a.i].f[l[i]])
a2 = a2.emonde().minimise()
a2 = self.move2(t=-(A[l[i]]-t)/self.b, a=a2, b=bb)
a = a.union(a2)
return a

#calcule l'intersection des ensembles limites
def intersection2 (self, FastAutomaton a, FastAutomaton b, ext=True):
a2 = self.complete(a, ext=ext)
Expand Down

0 comments on commit a45bb65

Please sign in to comment.