automaton.filter(states)

Return a subautomaton such that their states are in the input states set.

Postcondition:

  • The result automaton is subautomaton of input automaton.
  • The states of automaton are in the input states set.

See also:

Examples

In [1]:
import vcsn
In [2]:
%%automaton -s aut
context = "lal_char(a), b"
0 -> 1 a
1 -> 0 a
1 -> $
1 -> 2 a
3 -> 0 a
4 -> 3 a
0 -> 3 a
$ -> 0
3 -> 5 a
%3 I0 0 0 I0->0 F1 1 1 0->1 a 3 3 0->3 a 1->F1 1->0 a 2 2 1->2 a 3->0 a 5 5 3->5 a 4 4 4->3 a

To keep only states $0, 1, 2 , 3$:

In [3]:
aut.filter([0, 1, 2, 3])
Out[3]:
%3 I0 0 0 I0->0 F1 1 1 0->1 a 3 3 0->3 a 1->F1 1->0 a 2 2 1->2 a 3->0 a

"Keeping" a non-existing state is not an error:

In [4]:
aut.filter([0, 1, 7])
Out[4]:
%3 I0 0 0 I0->0 F1 1 1 0->1 a 1->F1 1->0 a

Calls to filter can be filtered:

In [5]:
a1 = aut.filter([0, 1, 2, 3, 4]).filter([0, 1, 3, 5])
a1
Out[5]:
%3 I0 0 0 I0->0 F1 1 1 0->1 a 3 3 0->3 a 1->F1 1->0 a 3->0 a

but it is less efficient than filtering on the intersection. This is on purpose: the outter automaton has a view on the filtered automaton.

In [6]:
a2 = aut.filter(list(set([0, 1, 2, 3, 4]) & set([0, 1, 3, 5])))
print(a1.info()['type'])
print(a2.info()['type'])
print(a1.is_isomorphic(a2))
filter_automaton<filter_automaton<mutable_automaton<letterset<char_letters(a)>, b>>>
filter_automaton<mutable_automaton<letterset<char_letters(a)>, b>>
True

We can filter a transposed automaton:

In [7]:
trans = aut.transpose()
trans
Out[7]:
%3 I1 1 1 I1->1 F0 0 0 0->F0 0->1 a 3 3 0->3 a 1->0 a 2 2 2->1 a 3->0 a 4 4 3->4 a 5 5 5->3 a
In [9]:
trans.filter([0, 1, 2, 3])
Out[9]:
%3 I1 1 1 I1->1 F0 0 0 0->F0 0->1 a 3 3 0->3 a 1->0 a 2 2 2->1 a 3->0 a