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

Generate a random rational expression.

Arguments:

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

Supported operators:

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

See also:

Examples

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.

The default coefficient is 1, therefore in the following example "+" is twice more likely to appear than ".", and "*" is twice less.

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

Weighted Expressions

Weighted expressions can be generated. Use the keys w. and .w to control the probability of left and right product by a weight. Use the key w to pass parameters to the random weight generator.

In [4]:
qrand = vcsn.context('lal(xyz), z').random_expression
for _ in range(3):
    display(qrand('+, w., w="min=-5, max=5"', length=10, identities='none'))
$ \left\langle 4 \right\rangle \,\left(x + \left\langle -2 \right\rangle \,\left(\left(y + z\right) + z\right)\right)$
$x + \left(z + \left( \left\langle -4 \right\rangle \,x + y\right)\right)$
$ \left\langle 2 \right\rangle \,\left( \left\langle -3 \right\rangle \,\left(z + \left\langle -5 \right\rangle \,\left( \left\langle -5 \right\rangle \,z\right)\right) + z\right)$

Note that because of the identities, some weights might escape the specified range.

In [5]:
for _ in range(5):
    display(qrand('+, w., w="min=0, max=5"', length=20))
$ \left\langle 5 \right\rangle \,\left(x + y + z + \left\langle 4 \right\rangle \,\left( \left\langle 3 \right\rangle \,x + z\right)\right)$
$y + \left\langle 15 \right\rangle \,\left(x + \left\langle 3 \right\rangle \,y + \left\langle 4 \right\rangle \,z\right)$
$x + \left\langle 36 \right\rangle \,y$
$ \left\langle 3 \right\rangle \,x + y + \left\langle 4 \right\rangle \,z + \left\langle 15 \right\rangle \,\left(x + y\right)$
$ \left\langle 30 \right\rangle \,z$