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 add_edge() to FastAutomaton and correct some bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mercatp committed Jul 10, 2016
1 parent a45bb65 commit effde91
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 39 deletions.
53 changes: 33 additions & 20 deletions src/sage/combinat/words/automataC.c
Expand Up @@ -98,9 +98,9 @@ Automaton NewAutomaton (int n, int na)
Automaton a;
a.n = n;
a.na = na;
a.i = -1;
if (n == 0)
{
a.i = -1;
a.e = NULL;
return a;
}
Expand All @@ -111,7 +111,7 @@ Automaton NewAutomaton (int n, int na)
printf("Out of memory !");
exit(6);
}
int i;
int i,j;
for (i=0;i<n;i++)
{
a.e[i].f = (int *)malloc(sizeof(int)*na);
Expand All @@ -120,6 +120,10 @@ Automaton NewAutomaton (int n, int na)
printf("Out of memory !");
exit(7);
}
for (j=0;j<na;j++)
{
a.e[i].f[j] = -1;
}
}
return a;
}
Expand Down Expand Up @@ -1432,6 +1436,33 @@ Automaton Determinise (Automaton a, Dict d, bool noempty, bool onlyfinals, bool
{
int i;

Automaton r;
if (a.i == -1)
{
//calcule la taille de l'alphabet
int nv = 0;
for (i=0;i<d.n;i++)
{
if (d.e[i] >= nv)
nv = d.e[i]+1;
}
//
if (verb)
printf("Pas d'état initial !\n");
if (nof)
{
r = NewAutomaton(1, nv);
r.i = 0;
r.e[0].final = true;
for (i=0;i<nv;i++)
{
r.e[0].f[i] = 0;
}
}else
r = NewAutomaton(0, nv);
return r;
}

//increase the stack size
const rlim_t kStackSize = 32 * 1024 * 1024;
struct rlimit rl;
Expand Down Expand Up @@ -1493,24 +1524,6 @@ Automaton Determinise (Automaton a, Dict d, bool noempty, bool onlyfinals, bool
//initialise l'automate résultat avec juste l'état initial
if (verb)
printf("Init r...\n");
Automaton r;
if (a.i == -1)
{
if (verb)
printf("Pas d'état initial !\n");
if (nof)
{
r = NewAutomaton(1, id.n);
r.i = 0;
r.e[0].final = true;
for (i=0;i<id.n;i++)
{
r.e[0].f[i] = 0;
}
}else
r = NewAutomaton(0, id.n);
return r;
}
r.n = 1;
r.na = id.n;
r.i = 0;
Expand Down
12 changes: 11 additions & 1 deletion src/sage/combinat/words/cautomata.pyx
Expand Up @@ -1067,7 +1067,17 @@ cdef class FastAutomaton:
sig_on()
AddEtat(self.a, final)
sig_off()

return self.a.n-1

def add_edge (self, int i, l, int j):
if i >= self.a.n: raise ValueError("L'etat %s n'existe pas."%i)
if j >= self.a.n: raise ValueError("L'etat %s n'existe pas."%j)
try:
k = self.A.index(l)
except:
raise ValueError("La lettre %s n'existe pas."%l)
self.a.e[i].f[k] = j

def n_states (self):
return self.a.n

Expand Down
12 changes: 12 additions & 0 deletions src/sage/monoids/beta_adic_monoid.pyx
Expand Up @@ -1514,6 +1514,13 @@ class BetaAdicMonoid(Monoid_class):
Cd is the set of differences A-B where A and B are the alphabets to compare.
t is the translation of one of the side (initial state of the automaton).
ext : automate des relations à l'infini ou pas.
TESTS::
sage: pi = x^3-x^2-x-1
sage: b = pi.roots(ring=QQbar)[1][0]
sage: m = BetaAdicMonoid(b, [0,1]).relations_automaton3()
"""
if Cd is None:
Cd = Set([c-c2 for c in self.C for c2 in self.C])
Expand Down Expand Up @@ -2626,16 +2633,21 @@ class BetaAdicMonoid(Monoid_class):
#compute the relations automaton with translation t
ar = self.relations_automaton4(t=t, A=A, B=B, couples=True, verb=verb)
#compute the product of a and b
if verb: print("product...")
ap = a.product(b)
#compute the intersections
if verb: print("intersection...")
ai = ar.intersection(ap)
if verb: print("min...")
ai = ai.minimise()
#project on one side
d={}
for c1 in A:
for c2 in B:
d[(c1,c2)] = c2
if verb: print("determinise...")
ai = ai.determinise_proj(d, verb=verb)
if verb: print("min")
return ai.minimise()

#Not well tested !!!!
Expand Down
62 changes: 46 additions & 16 deletions src/sage/monoids/relations.c
Expand Up @@ -22,7 +22,7 @@ Automate NewAutomaton (int n, int na)
printf("Out of memory !");
exit(6);
}
int i;
int i, j;
for (i=0;i<n;i++)
{
a.e[i].f = (int *)malloc(sizeof(int)*na);
Expand All @@ -31,6 +31,10 @@ Automate NewAutomaton (int n, int na)
printf("Out of memory !");
exit(7);
}
for (j=0;j<na;j++)
{
a.e[i].f[j] = -1;
}
}
return a;
}
Expand All @@ -49,8 +53,9 @@ InfoBetaAdic iba; //variable globale contenant les infos sur le développement e

Element NewElement (int n)
{
//printf("NewElement(%d)\n", n);
Element e;
e.c = (int*)malloc(sizeof(int)*n);
e.c = (coeff *)malloc(sizeof(coeff)*n);
return e;
}

Expand Down Expand Up @@ -101,15 +106,27 @@ void initHash ()
int i;
for (i=0;i<nhash;i++)
{
thash[i].e = NULL;
//thash[i].e = NULL;
thash[i].n = 0;
}
}

void freeInfoBetaAdic (InfoBetaAdic iba)
{
int i;
int i,j;
free(pile);
for (i=0;i<nhash;i++)
{
for (j=0;j<thash[i].n;j++)
{
FreeElement(thash[i].e[j]);
}
if (thash[i].n)
{
free(thash[i].e);
free(thash[i].ind);
}
}
free(thash);
free(iba.cM);
for (i=0;i<iba.ncmax;i++)
Expand Down Expand Up @@ -295,9 +312,17 @@ bool inHash (Element e)
}
}
//ajoute l'élément
thash[h].e = (Element *)realloc(thash[h].e, thash[h].n+1);
//thash[h].ne = (int *)realloc(thash[h].ne, thash[h].n+1);
thash[h].ind = (int *)realloc(thash[h].ind, thash[h].n+1);
if (thash[h].n)
{
thash[h].e = (Element *)realloc(thash[h].e, sizeof(Element)*(thash[h].n+1));
//thash[h].ne = (int *)realloc(thash[h].ne, thash[h].n+1);
thash[h].ind = (int *)realloc(thash[h].ind, sizeof(int)*(thash[h].n+1));
}else
{
thash[h].e = (Element *)malloc(sizeof(Element));
//thash[h].ne = (int *)realloc(thash[h].ne, thash[h].n+1);
thash[h].ind = (int *)malloc(sizeof(int));
}
if (!thash[h].e || !thash[h].ind)
{
printf("Out of memory !!!\n");
Expand Down Expand Up @@ -487,6 +512,12 @@ Automate RelationsAutomaton (InfoBetaAdic iba2, bool isvide, bool ext, bool verb
{
FreeElement(thash[i].e[j]);
}
if (thash[i].n)
{
free(thash[i].e);
free(thash[i].ind);
}
thash[i].n = 0;
}
FreeElement(s);
FreeElement(e);
Expand Down Expand Up @@ -584,9 +615,9 @@ Automate RelationsAutomatonT (InfoBetaAdic iba2, Element t, bool isvide, bool ex
pile[n] = NewElement(iba.n);
copy(s, pile[n]);
n++;
if (n > npile)
if (n >= npile)
{
printf("Erreur : dépassement de la pile !!!\n");
printf("*****************************************\n*** Erreur : dépassement de la pile !!! ***\n*****************************************\n");
}
}else
{//on retombe sur un état déjà vu
Expand All @@ -612,13 +643,6 @@ Automate RelationsAutomatonT (InfoBetaAdic iba2, Element t, bool isvide, bool ex

//créé l'automate
Automate r = NewAutomaton(compteur, iba.nc);
for (i=0;i<r.n;i++)
{
for (j=0;j<r.na;j++)
{
r.e[i].f[j] = -1;
}
}
int k, ind;
for (i=0;i<nhash;i++)
{
Expand Down Expand Up @@ -667,6 +691,12 @@ Automate RelationsAutomatonT (InfoBetaAdic iba2, Element t, bool isvide, bool ex
{
FreeElement(thash[i].e[j]);
}
if (thash[i].n)
{
free(thash[i].e);
free(thash[i].ind);
}
thash[i].n = 0;
}
FreeElement(s);
FreeElement(e);
Expand Down
6 changes: 4 additions & 2 deletions src/sage/monoids/relations.h
@@ -1,13 +1,15 @@

typedef long int coeff;

struct Element
{
long int *c; //liste des n coeffs
coeff *c; //liste des n coeffs
};
typedef struct Element Element;

struct PlaceArch
{
Complexe *c; //1, b, b^2, ... pour cette place
Complexe *c; //1, b, b^2, ..., b^(n-1) pour cette place
};
typedef struct PlaceArch PlaceArch;

Expand Down

0 comments on commit effde91

Please sign in to comment.