# expression.derived_term(algo="expansion",lazy=False,deterministic=False,breaking=False)¶

Generate the derived-term automaton from an expression.

The arguments are:

• algo:
• "derivation": rely on the expression.derivation.
• "expansion": rely on the expression.expansion.
• lazy: whether to build the result lazily, on the fly
• deterministic: whether to build a deterministic automaton.
• breaking: split the polynomials at each step

Preconditions:

• derivation-based algorithms require a free labelset (e.g., word labels are not supported)
• deterministic might not terminate on some expressions (in which case consider lazy)
• expressions with complement operators might not terminate either

Also known as:

• Antimirov automaton
• equation automaton
• partial-derivatives automaton

References:

## Examples¶

### Classical Expressions¶

In the classical case (labels are letters, and weights are Boolean), this is the construct as described by Antimirov.

In :
import vcsn
bexp = vcsn.B.expression
r = bexp('[ab]*a[ab]{3}')
a = r.derived_term()
a

Out:

The states of the resulting automaton are decorated by their expressions. This can be stripped:

In :
a.strip()

Out:

The result does not depend on the core computation: expansions and derivations compute the same terms:

In :
r.derived_term('derivation')

Out:

### Deterministic Automata¶

Alternatively, one can request a deterministic result:

In :
qexp = vcsn.Q.expression
r = qexp('aba+a(a+<-1>ba)')
nfa = r.derived_term()
nfa

Out:
In :
dfa = r.derived_term(deterministic=True)
dfa

Out:
In :
nfa.determinize()

Out:

### Extended Rational Expressions¶

Extended expressions are supported. For instance, words starting with as and then bs, but not exactly ab:

In :
r = bexp('a*b* & (ab){c}')
r

Out:
${a}^{*} \, {b}^{*} \& \left(a \, b\right)^{c}$
In :
a = r.derived_term()
a

Out:
In :
a.shortest(10)

Out:
$\varepsilon \oplus \mathit{a} \oplus \mathit{b} \oplus \mathit{aa} \oplus \mathit{bb} \oplus \mathit{aaa} \oplus \mathit{aab} \oplus \mathit{abb} \oplus \mathit{bbb} \oplus \mathit{aaaa}$

### Weighted Expressions¶

The following example is taken from lombardy.2005.tcs:

In :
r = qexp('(<1/6>a*+<1/3>b*)*')
r.derived_term()

Out:

### Multitape Expressions¶

Since both expansions and derivatives in Vcsn support the tuple operator, both flavors of derived_term can be used to build a multitape automaton from a multitape expression. For instance:

In :
c = vcsn.context('lat<lan, lan>, q')
c

Out:
$(\{\ldots\})^? \times (\{\ldots\})^?\to\mathbb{Q}$
In :
e = c.expression('(a{+}|x + b{+}|y)*')
e

Out:
$\left( \left. a \, {a}^{*} \middle| x \right. + \left. b \, {b}^{*} \middle| y \right. \right)^{*}$
In :
e.derived_term('expansion')

Out:
In :
e.derived_term('derivation')

Out:

### Broken derived-term automaton¶

"Breaking" variants of derivation and expansion "split" the polynomials at each step. In short, it means that no state will be labeled by an addition: rather the addition is split into several states. As a consequence, the automaton might have several initial states.

In :
r = qexp('[ab]{2}')
r.derived_term()

Out:
In :
r.derived_term(breaking=True)

Out:

### Lazy construction¶

The derived-term automaton can be built on-the-fly, on-demand: states are uncovered on needed (e.g., when traversed by an evaluation). This is especially useful when considering an expression on which the process does not terminate (i.e., would generate an "infinite" automaton).

In :
r = qexp('a*+b*'); r

Out:
${a}^{*} + {b}^{*}$
In :
a = r.derived_term(lazy=True)
a

Out:
In :
print(a('')); a

2

Out:
In :
print(a('a')); a

1

Out:
In :
print(a('b')); a

1

Out:

The following expression does not admit a (finite) deterministic automaton (and determinizing the non-deterministic derived-term automaton would not terminate either). Repeatedly calling the evaluation uncovers portions of an "infinite" deterministic automaton.

In :
r = qexp('a*+(<2>a)*'); r

Out:
${a}^{*} + \left( \left\langle 2 \right\rangle \,a\right)^{*}$
In :
a = r.derived_term(deterministic=True, lazy=True)
a

Out:
In :
print(a('')); a

2

Out:
In :
print(a('a')); a

3

Out:
In :
print(a('aaa')); a

9

Out:

Note that lazy determinization is also available: see automaton.determinize to see the corresponding example.

Since it entails a "local" determinization ("under" the complement operator), the algorithm fails would not terminate.

In :
r = r.complement()
r

Out:
$\left({a}^{*} + \left( \left\langle 2 \right\rangle \,a\right)^{*}\right)^{c}$
In :
a = r.derived_term(lazy=True)
a

Out:
In :
print(a('aa')); a

0

Out:
In :
print(a('b')); a

1

Out: