context.random_expression(specs, length=6, identities="default")

Generate a random rational expression.

Arguments:

  • specs: string list operators of the generated expression, with associated densities (1 by default).
  • length: the maximum length.
  • identities: the identities of the resulting expression

The list of every operators:

  • nullary: \\e, \\z
  • unary: ! (prefix), {c}, *
  • binary: &, &:, :, ., <+, %, +, {/}, {\\}, {T}

See also:

  • Expressions – the documentation about expressions (and identities)

Example

In [1]:
import os
# This trick ensures that we always use the same random seed,
# hence running this documentation always gives the same result.
os.environ['VCSN_SEED'] = '1'
import vcsn
from IPython.display import display
ctx = vcsn.context('lal_char(abc), b')

Densities are expressed with Bernoulli distribution if the operator is the only one, and discrete distribution otherwise.

Here "+" is twice more likely to appear than ".", and "*" twice less.

In [2]:
for _ in range(3):
    display(ctx.random_expression('+=2,.=1,*=0.5'))
$\left(b \, \left(a + b\right)\right)^{*}$
$a + c$
$a + b \, c \, a$
In [3]:
for _ in range(3):
    display(ctx.random_expression('.=2,+=2,&=1,*=0.5', length=20, identities='none'))
$\left(a \& \left(c \, \left(a + a\right) \& c\right)\right) \& \left(\left(b \, c\right) \, a + \left(a \, c\right) \, b\right)^{*} + b$
$\left(\left(c \, c\right)^{*} \& c + \left({b}^{*} \, \left(\left(\left(c + b\right) \& a\right) \, \left(b + {b}^{*}\right)\right)\right)^{*}\right) + \left(c + a\right) \& a$
$\left(b \& \left(c \& c \, a\right)\right) \, \left(\left(\left(c + a\right) \, a\right)^{*} \, \left(c \& c\right)\right) + b \, \left(a \& \left(a \& a\right)\right)$