cos(4*x+6).replace(
lambda expr: expr.is_Add,
lambda expr: ((expr/(2*x+3)).simplify()*u)
)
cos(4*x+6).subs(x, (u-3)/2)(x*cos(4*x+6)).subs(x, (u-3)/2)In [1]: w = Wild('w')
In [2]: cos(4*x+6).replace(w*(2*x+3), u*w)
Out[2]:
⎛ 2 ⎞
⎜ 4⋅u ⋅x 6⋅u ⎟
cos⎜────────── + ───────⎟
⎜ 2 2⋅x + 3⎟
⎝(2⋅x + 3) ⎠
cos(4*x+6).replace((w*2*x+w*3), u*w)(3 + x*cos(4*x+6)).replace(
lambda expr: expr.is_Add and simplify(expr/(2*x+3)).is_Number,
lambda expr: u*simplify(expr/(2*x+3))
)(x*(8*x+12) + x*cos(4*x+6)).replace( lambda expr: (expr.func == cos or expr.func == sin) and simplify(expr.args[0]/(2*x+3)).is_Number, lambda expr: expr.func(u*simplify(expr.args[0]/(2*x+3))))I think we should make it so that cos(4*x + 6).replace(2*x + 3, u) works.
That would be nice. Also, Francesco's wildcard replacement looks buggy. The `4*x` apparently matches twice, once for 4 and once for x:
In [1]: u = symbols('u')
In [2]: w = Wild('w')
In [3]: cos(4*x+6).replace(w*(2*x+3), u*w, map=True)
Out[3]:
⎛ ⎛ 2 ⎞ ⎞
⎜ ⎜ 4⋅u ⋅x 6⋅u ⎟ ⎧ 4⋅u 6⋅u u⋅x ⎫⎟
⎜cos⎜────────── + ───────⎟, ⎨4: ───────, 6: ───────, x: ───────⎬⎟
⎜ ⎜ 2 2⋅x + 3⎟ ⎩ 2⋅x + 3 2⋅x + 3 2⋅x + 3⎭⎟
⎝ ⎝(2⋅x + 3) ⎠ ⎠This seems to contradict the documentation's claims that "changes made are targeted only once" and "changes are not made twice". I thought maybe I just misunderstood the `simultaneous` flag, but that makes things even worse. Or am I misunderstanding that?