Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 45910
Kernel: Python 3

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

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.

for _ in range(3): display(ctx.random_expression('+=2, ., *=0.5'))

(b(a+b))\left(b \, \left(a + b\right)\right)^{*}

a+cca + c \, c

a(b+c)a \, \left(b + c\right)

for _ in range(3): display(ctx.random_expression('.=2, +=2, &=1, *=0.5', length=20, identities='none'))

b&(cc+cc)(ca)+a(cb)b \& \left(c \, c + c \, c\right) \, \left(c \, a\right) + a \, \left(c \, b\right)

(ab&(a+a))&(c+b&((a+a)+a))\left(a \, b \& \left(a + a\right)\right) \& \left(c + b \& \left(\left(a + a\right) + a\right)\right)

((bc)((cc&(b&b+b))c))\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.

qrand = vcsn.context('lal(xyz), z').random_expression for _ in range(3): display(qrand('+, w., w="min=-5, max=5"', length=10, identities='none'))

4(x+2((y+z)+z)) \left\langle 4 \right\rangle \,\left(x + \left\langle -2 \right\rangle \,\left(\left(y + z\right) + z\right)\right)

x+(z+(4x+y))x + \left(z + \left( \left\langle -4 \right\rangle \,x + y\right)\right)

2(3(z+5(5z))+z) \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.

for _ in range(5): display(qrand('+, w., w="min=0, max=5"', length=20))

5(x+y+z+4(3x+z)) \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+15(x+3y+4z)y + \left\langle 15 \right\rangle \,\left(x + \left\langle 3 \right\rangle \,y + \left\langle 4 \right\rangle \,z\right)

x+36yx + \left\langle 36 \right\rangle \,y

3x+y+4z+15(x+y) \left\langle 3 \right\rangle \,x + y + \left\langle 4 \right\rangle \,z + \left\langle 15 \right\rangle \,\left(x + y\right)

30z \left\langle 30 \right\rangle \,z