Re: [sympy] Conditional substitution

2022-01-06 Thread David Bailey
On 06/01/2022 22:19, Oscar Benjamin wrote: On Thu, 6 Jan 2022 at 21:23, David Bailey wrote: On 06/01/2022 16:41, Oscar Benjamin wrote: You can use replace to make arbitrary conditions on substitution. There are different syntaxes so here's how you do it using wild pattern-matching: In [15]:

Re: [sympy] Conditional substitution

2022-01-06 Thread Oscar Benjamin
On Thu, 6 Jan 2022 at 21:23, David Bailey wrote: > > On 06/01/2022 16:41, Oscar Benjamin wrote: > > You can use replace to make arbitrary conditions on substitution. > > There are different syntaxes so here's how you do it using wild > > pattern-matching: > > > > In [15]: expr = (a*x**14 + b*x +

Re: [sympy] Conditional substitution

2022-01-06 Thread David Bailey
On 06/01/2022 16:41, Oscar Benjamin wrote: You can use replace to make arbitrary conditions on substitution. There are different syntaxes so here's how you do it using wild pattern-matching: In [15]: expr = (a*x**14 + b*x + c) In [16]: expr Out[16]: 14 a⋅x + b⋅x + c In [17]: w =

Re: [sympy] Conditional substitution

2022-01-06 Thread Oscar Benjamin
On Thu, 6 Jan 2022 at 14:41, David Bailey wrote: > > Dear group, > > A substitution like this is easy to make with SymPy: > > (a*x**2+b*x+c).subs(x,y) > > However, how can I make a conditional substitution, such as: > > a) One that would replace even powers of x only. > > b) One which would

[sympy] Conditional substitution

2022-01-06 Thread David Bailey
Dear group, A substitution like this is easy to make with SymPy: (a*x**2+b*x+c).subs(x,y) However, how can I make a conditional substitution, such as: a) One that would replace even powers of x only. b) One which would replace even powers of x by y**(n/2) resulting in a*y**7+b*x+c? I.e. one

[sympy] Conditional substitution and on the fly dictionary creation

2013-04-27 Thread Alan Bromborsky
consider the sympy expressions - a = a_1*e_1+...+a_n*e_n b = b_1*e_1+...+b_n*e_n where the a_i's and the b_i's are commutative sympy expressions and the e_i's are non-commutative symbols and you have a dictionary mul_dict = {e_i*e_j: f(e_i,e_j)} if mul_dict is fully populated then a*b =

Re: [sympy] Conditional substitution and on the fly dictionary creation

2013-04-27 Thread Alan Bromborsky
On 04/27/2013 12:40 PM, Alan Bromborsky wrote: consider the sympy expressions - a = a_1*e_1+...+a_n*e_n b = b_1*e_1+...+b_n*e_n where the a_i's and the b_i's are commutative sympy expressions and the e_i's are non-commutative symbols and you have a dictionary mul_dict = {e_i*e_j: f(e_i,e_j)}

Re: [sympy] Conditional substitution and on the fly dictionary creation

2013-04-27 Thread Aaron Meurer
It should work with a custom dictionary. At the very least, it will work with any iterator or (old, new) pairs. You could also make f symbolic and unevaluated by default, then call doit after the substitution. It won't be 100% efficient if the same f will appear more than once, though. Aaron