Transformer
Transformer()
Operations that transform an expression.
Methods
Name | Description |
---|---|
apart | Create a transformer that computes the partial fraction decomposition in x . |
cancel | Create a transformer that cancels common factors between numerators and denominators. |
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, |
coefficient | Create a transformer that collects terms involving the literal occurrence of x . |
collect | Create a transformer that collect terms involving the same power of x , |
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 a bound transformer. If the transformer is unbound, |
expand | Create a transformer that expands products and powers. |
factor | Create a transformer that factors the expression over the rationals. |
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. |
map_terms | Map a chain of transformer over the terms of the expression, optionally using multiple cores. |
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 . |
set_coefficient_ring | Create a transformer that sets the coefficient ring to contain the variables in the vars list. |
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. |
together | Create a transformer that writes the expression over a common denominator. |
apart
Transformer.apart(x)
Create a transformer that computes the partial fraction decomposition in x
.
cancel
Transformer.cancel()
Create a transformer that cancels common factors between numerators and denominators. Any non-canceling parts of the expression will not be rewritten.
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()
coefficient
Transformer.coefficient(x)
Create a transformer that collects terms involving the literal occurrence of x
.
collect
Transformer.collect(x, key_map=None, coeff_map=None)
Create a transformer that collect terms involving the same power of x
, where x
is a variable or function name. Return the list of key-coefficient pairs and the remainder that matched no key.
Both the key (the quantity collected in) and its coefficient can be mapped using key_map
and coeff_map
transformers respectively.
Examples
>>> from symbolica import Expression
>>> x, y = Expression.symbol('x', 'y')
>>> e = 5*x + x * y + x**2 + 5
>>>
>>> print(e.transform().collect(x).execute())
yields x^2+x*(y+5)+5
.
>>> from symbolica import Expression
>>> x, y, x_, var, coeff = Expression.symbol('x', 'y', 'x_', 'var', 'coeff')
>>> e = 5*x + x * y + x**2 + 5
>>> print(e.collect(x, key_map=Transformer().replace_all(x_, var(x_)),
=Transformer().replace_all(x_, coeff(x_)))) coeff_map
yields var(1)*coeff(5)+var(x)*coeff(y+5)+var(x^2)*coeff(1)
.
Parameters
Name | Type | Description | Default |
---|---|---|---|
x |
Expression | The variable to collect terms in | required |
key_map |
Optional[Transformer] | A transformer to be applied to the quantity collected in | None |
coeff_map |
Optional[Transformer] | A transformer to be applied to the coefficient | None |
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 a bound transformer. If the transformer is unbound, you can call it with an expression as an argument.
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.symbol('x', 'x_')
>>> f = Expression.symbol('f')
>>> e = f((x+1)**2).replace_all(f(x_), x_.transform().expand())
>>> print(e)
factor
Transformer.factor()
Create a transformer that factors the expression over the rationals.
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.symbol('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)
map_terms
Transformer.map_terms(*transformers, n_cores=1)
Map a chain of transformer over the terms of the expression, optionally using multiple cores.
Examples
>>> from symbolica import *
>>> x, y = S('x', 'y')
>>> t = Transformer().map_terms(Transformer().print(), n_cores=2)
>>> e = t(x + y)
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.symbol('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.symbol('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.symbol('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 | The pattern to match. | required |
rhs |
Transformer | Expression | Callable[[dict[Expression, Expression]], Expression] | int | float | Decimal | The right-hand side to replace the matched subexpression with. Can be a transformer, expression or a function that maps a dictionary of wildcards to an expression. | required |
cond |
Optional[PatternRestriction] | Conditions on the pattern. | None |
non_greedy_wildcards |
Optional[Sequence[Expression]] | Wildcards that try to match as little as possible. | None |
level_range |
Optional[Tuple[int, Optional[int]]] | Specifies the [min,max] level at which the pattern is allowed to match. The first level is 0 and the level is increased when going into a function or one level deeper in the expression tree, depending on level_is_tree_depth . |
None |
level_is_tree_depth |
Optional[bool] | If set to True , the level is increased when going one level deeper in the expression tree. |
False |
allow_new_wildcards_on_rhs |
Optional[bool] | If set to True , allow wildcards that do not appear in the pattern on the right-hand side. |
False |
rhs_cache_size |
Cache the first rhs_cache_size substituted patterns. If set to None , an internally determined cache size is used. Warning: caching should be disabled (rhs_cache_size=0 ) if the right-hand side contains side effects, such as updating a global variable. |
required | |
repeat |
If set to True , the entire operation will be repeated until there are no more matches. |
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.symbol('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.symbol('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)
.
set_coefficient_ring
Transformer.set_coefficient_ring(vars)
Create a transformer that sets the coefficient ring to contain the variables in the vars
list. This will move all variables into a rational polynomial function.
Parameters
Name | Type | Description | Default |
---|---|---|---|
vars |
Sequence[Expression] | A list of variables | required |
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.symbol('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)
together
Transformer.together()
Create a transformer that writes the expression over a common denominator.