Transformer
Transformer()
Operations that transform an expression.
Methods
Name | Description |
---|---|
chain | Chain several transformers. chain(A,B,C) is the same as A.B.C , |
check_interrupt | Create a transformer that checks for a Python interrupt, |
cycle_symmetrize | Create a transformer that cycle-symmetrizes a function. |
deduplicate | Create a transformer that removes elements from a list if they occur |
derivative | Create a transformer that derives self w.r.t the variable x . |
execute | Execute the transformer. |
expand | Create a transformer that expands products and powers. |
for_each | Create a transformer that applies a transformer chain to every argument of the arg() function. |
from_coeff | Create a transformer that extracts a rational polynomial from a coefficient. |
linearize | Create a transformer that linearizes a function, optionally extracting symbols |
map | Create a transformer that applies a Python function. |
nargs | Create a transformer that returns the number of arguments. |
partitions | Create a transformer that partitions a list of arguments into named bins of a given length, |
permutations | Create a transformer that generates all permutations of a list of arguments. |
Create a transformer that prints the expression. | |
prod | Create a transformer that computes the product of a list of arguments. |
repeat | Create a transformer that repeatedly executes the arguments in order |
replace_all | Create a transformer that replaces all subexpressions matching the pattern pat by the right-hand side rhs . |
replace_all_multiple | Create a transformer that replaces all atoms matching the patterns. See replace_all for more information. |
series | Create a transformer that series expands in x around expansion_point to depth depth . |
sort | Create a transformer that sorts a list of arguments. |
split | Create a transformer that split a sum or product into a list of arguments. |
stats | Print statistics of a transformer, tagging it with tag . |
sum | Create a transformer that computes the sum of a list of arguments. |
chain
Transformer.chain(*transformers)
Chain several transformers. chain(A,B,C)
is the same as A.B.C
, where A
, B
, C
are transformers.
Examples
>>> from symbolica import Expression
>>> x_ = Expression.symbol('x_')
>>> f = Expression.symbol('f')
>>> e = Expression.parse("f(5)")
>>> e = e.transform().chain(
>>> Transformer().expand(),
>>> Transformer().replace_all(f(x_), f(5))
>>> ).execute()
check_interrupt
Transformer.check_interrupt()
Create a transformer that checks for a Python interrupt, such as ctrl-c and aborts the current transformer.
Examples
>>> from symbolica import *
>>> x_ = Expression.symbol('x_')
>>> f = Expression.symbol('f')
>>> f(10).transform().repeat(Transformer().replace_all(
>>> f(x_), f(x_+1)).check_interrupt()).execute()
cycle_symmetrize
Transformer.cycle_symmetrize()
Create a transformer that cycle-symmetrizes a function.
Examples
>>> from symbolica import Expression, Transformer
>>> x_ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(1,2,4,1,2,3).replace_all(f(x__), x_.transform().cycle_symmetrize())
>>> print(e)
Yields f(1,2,3,1,2,4)
.
deduplicate
Transformer.deduplicate()
Create a transformer that removes elements from a list if they occur earlier in the list as well.
Examples
>>> from symbolica import Expression, Transformer
>>> x__ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(1,2,1,2).replace_all(f(x__), x__.transform().deduplicate())
>>> print(e)
Yields f(1,2)
.
derivative
Transformer.derivative(x)
Create a transformer that derives self
w.r.t the variable x
.
execute
Transformer.execute()
Execute the transformer.
Examples
>>> from symbolica import Expression
>>> x = Expression.symbol('x')
>>> e = (x+1)**5
>>> e = e.transform().expand().execute()
>>> print(e)
expand
Transformer.expand(var=None)
Create a transformer that expands products and powers.
Examples
>>> from symbolica import Expression, Transformer
>>> x, x_ = Expression.symbols('x', 'x_')
>>> f = Expression.symbol('f')
>>> e = f((x+1)**2).replace_all(f(x_), x_.transform().expand())
>>> print(e)
for_each
Transformer.for_each(*transformers)
Create a transformer that applies a transformer chain to every argument of the arg()
function. If the input is not arg()
, the transformer is applied to the input.
Examples
>>> from symbolica import Expression
>>> x = Expression.symbol('x')
>>> f = Expression.symbol('f')
>>> e = (1+x).transform().split().for_each(Transformer().map(f)).execute()
from_coeff
Transformer.from_coeff()
Create a transformer that extracts a rational polynomial from a coefficient.
Examples
>>> from symbolica import Expression, Function
>>> e = Function.COEFF((x^2+1)/y^2).transform().from_coeff()
>>> print(e)
linearize
Transformer.linearize(symbols)
Create a transformer that linearizes a function, optionally extracting symbols
as well.
Examples
>>> from symbolica import Expression, Transformer
>>> x, y, z, w, f, x__ = Expression.symbols('x', 'y', 'z', 'w', 'f', 'x__')
>>> e = f(x+y, 4*z*w+3).replace_all(f(x__), f(x__).transform().linearize([z]))
>>> print(e)
yields f(x,3)+f(y,3)+4*z*f(x,w)+4*z*f(y,w)
.
map
Transformer.map(f)
Create a transformer that applies a Python function.
Examples
>>> from symbolica import Expression, Transformer
>>> x_ = Expression.symbol('x_')
>>> f = Expression.symbol('f')
>>> e = f(2).replace_all(f(x_), x_.transform().map(lambda r: r**2))
>>> print(e)
nargs
Transformer.nargs(only_for_arg_fun=False)
Create a transformer that returns the number of arguments. If the argument is not a function, return 0.
If only_for_arg_fun
is True
, only count the number of arguments in the arg()
function and return 1 if the input is not arg
. This is useful for obtaining the length of a range during pattern matching.
Examples
>>> from symbolica import Expression, Transformer
>>> x__ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(2,3,4).replace_all(f(x__), x__.transform().nargs())
>>> print(e)
partitions
Transformer.partitions(bins, fill_last=False, repeat=False)
Create a transformer that partitions a list of arguments into named bins of a given length, returning all partitions and their multiplicity.
If the unordered list elements
is larger than the bins, setting the flag fill_last
will add all remaining elements to the last bin.
Setting the flag repeat
means that the bins will be repeated to exactly fit all elements, if possible.
Note that the functions names to be provided for the bin names must be generated through Expression.var
.
Examples
>>> from symbolica import Expression, Transformer
>>> x_, f_id, g_id = Expression.symbols('x__', 'f', 'g')
>>> f = Expression.symbol('f')
>>> e = f(1,2,1,3).replace_all(f(x_), x_.transform().partitions([(f_id, 2), (g_id, 1), (f_id, 1)]))
>>> print(e)
yields:
2*f(1)*f(1,2)*g(3)+2*f(1)*f(1,3)*g(2)+2*f(1)*f(2,3)*g(1)+f(2)*f(1,1)*g(3)+2*f(2)*f(1,3)*g(1)+f(3)*f(1,1)*g(2)+2*f(3)*f(1,2)*g(1)
permutations
Transformer.permutations(function_name)
Create a transformer that generates all permutations of a list of arguments.
Examples
>>> from symbolica import Expression, Transformer
>>> x_, f_id = Expression.symbols('x__', 'f')
>>> f = Expression.symbol('f')
>>> e = f(1,2,1,2).replace_all(f(x_), x_.transform().permutations(f_id)
>>> print(e)
yields:
4*f(1,1,2,2)+4*f(1,2,1,2)+4*f(1,2,2,1)+4*f(2,1,1,2)+4*f(2,1,2,1)+4*f(2,2,1,1)
Transformer.print(terms_on_new_line=False, color_top_level_sum=True, color_builtin_symbols=True, print_finite_field=True, symmetric_representation_for_finite_field=False, explicit_rational_polynomial=False, number_thousands_separator=None, multiplication_operator='*', double_star_for_exponentiation=False, square_brackets_for_function=False, num_exp_as_superscript=True, latex=False)
Create a transformer that prints the expression.
Examples
>>> Expression.parse('f(10)').transform().print(terms_on_new_line = True).execute()
prod
Transformer.prod()
Create a transformer that computes the product of a list of arguments.
Examples
>>> from symbolica import Expression, Transformer
>>> x__ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(2,3).replace_all(f(x__), x__.transform().prod())
>>> print(e)
repeat
Transformer.repeat(*transformers)
Create a transformer that repeatedly executes the arguments in order until there are no more changes. The output from one transformer is inserted into the next.
Examples
>>> from symbolica import Expression
>>> x_ = Expression.symbol('x_')
>>> f = Expression.symbol('f')
>>> e = Expression.parse("f(5)")
>>> e = e.transform().repeat(
>>> Transformer().expand(),
>>> Transformer().replace_all(f(x_), f(x_ - 1) + f(x_ - 2), x_.req_gt(1))
>>> ).execute()
replace_all
Transformer.replace_all(pat, rhs, cond=None, non_greedy_wildcards=None, level_range=None, level_is_tree_depth=False, allow_new_wildcards_on_rhs=False)
Create a transformer that replaces all subexpressions matching the pattern pat
by the right-hand side rhs
.
Examples
>>> x, w1_, w2_ = Expression.symbols('x','w1_','w2_')
>>> f = Expression.symbol('f')
>>> e = f(3,x)
>>> r = e.transform().replace_all(f(w1_,w2_), f(w1_ - 1, w2_**2), (w1_ >= 1) & w2_.is_var())
>>> print(r)
Parameters
Name | Type | Description | Default |
---|---|---|---|
pat |
Transformer | Expression | int | float | Decimal | required | |
rhs |
Transformer | Expression | int | float | Decimal | required | |
cond |
Optional[PatternRestriction] | None |
|
non_greedy_wildcards |
Optional[Sequence[Expression]] | None |
|
level_range |
Optional[Tuple[int, Optional[int]]] | None |
|
level_is_tree_depth |
Optional[bool] | False |
|
allow_new_wildcards_on_rhs |
Optional[bool] | False |
|
repeat |
required |
replace_all_multiple
Transformer.replace_all_multiple(replacements)
Create a transformer that replaces all atoms matching the patterns. See replace_all
for more information.
Examples
>>> x, y, f = Expression.symbols('x', 'y', 'f')
>>> e = f(x,y)
>>> r = e.transform().replace_all_multiple(Replacement(x, y), Replacement(y, x))
>>> print(r)
series
Transformer.series(x, expansion_point, depth, depth_denom=1, depth_is_absolute=True)
Create a transformer that series expands in x
around expansion_point
to depth depth
.
Examples
>>> from symbolica import Expression
>>> x, y = Expression.symbols('x', 'y')
>>> f = Expression.symbol('f')
>>>
>>> e = 2* x**2 * y + f(x)
>>> e = e.series(x, 0, 2)
>>>
>>> print(e)
yields f(0)+x*der(1,f(0))+1/2*x^2*(der(2,f(0))+4*y)
.
sort
Transformer.sort()
Create a transformer that sorts a list of arguments.
Examples
>>> from symbolica import Expression, Transformer
>>> x__ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(3,2,1).replace_all(f(x__), x__.transform().sort())
>>> print(e)
split
Transformer.split()
Create a transformer that split a sum or product into a list of arguments.
Examples
>>> from symbolica import Expression, Transformer
>>> x, x__ = Expression.symbols('x', 'x__')
>>> f = Expression.symbol('f')
>>> e = (x + 1).replace_all(x__, f(x_.transform().split()))
>>> print(e)
stats
Transformer.stats(tag, transformer, color_medium_change_threshold=10.0, color_large_change_threshold=100.0)
Print statistics of a transformer, tagging it with tag
.
Examples
>>> from symbolica import Expression
>>> x_ = Expression.symbol('x_')
>>> f = Expression.symbol('f')
>>> e = Expression.parse("f(5)")
>>> e = e.transform().stats('replace', Transformer().replace_all(f(x_), 1)).execute()
yields
Stats for replace:
In │ 1 │ 10.00 B │
Out │ 1 │ 3.00 B │ ⧗ 40.15µs
sum
Transformer.sum()
Create a transformer that computes the sum of a list of arguments.
Examples
>>> from symbolica import Expression, Transformer
>>> x__ = Expression.symbol('x__')
>>> f = Expression.symbol('f')
>>> e = f(2,3).replace_all(f(x__), x__.transform().sum())
>>> print(e)