I agree that wildcards behave very strangely -- especially under
multiplication, because they can basically divide by whatever they're
multiplying. In my experience, their real power comes from repeated uses.
For example, the following *would* work in your example, Francesco:
cos(4*x+6).replace((w*2*x+w*3), u*w)
Also, note that there is an `exclude` flag to the wildcard constructor that
can help in some cases (though wouldn't have helped in your example).
While we're at it, I might add a couple more examples that make your
original suggestion a little more specific, so that they may deal with
Paul's situation more generally. First, assuming there might be another
term in the expression (and, as he mentioned, other `x`s shouldn't be
replaced), this will be do:
(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))
)
That extra condition in the first lambda function is needed to make sure
the whole expression (which is now an `Add`) won't be multiplied by
u/(2*x+3).
Or, if only arguments to `sin` and `cos` functions should be replaced, this
would work:
(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)))
)
On Friday, March 20, 2015 at 9:11:40 AM UTC-4, Francesco Bonazzi wrote:
>
> There are also wildcards to use in the replacement matcher, unfortunately
> in SymPy wild symbols often match too much or in an unexpected way.
>
> Consider an attempt with a wild:
>
> 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) ⎠
>
>
> It would be nice to have assumptions work on wildcards, in order to
> restrict their matching possibilities.
>
> On Friday, March 20, 2015 at 9:01:05 AM UTC+1, Paul Royik wrote:
>>
>>
>> Yes. This version is not for me.
>> x in front of cosine shouldn't be replaced.
>>
>> On Thursday, March 19, 2015 at 4:56:42 PM UTC+2, Mike Boyle wrote:
>>>
>>> It seems to me that the easy way to do this is to solve your replacement
>>> for `x`:
>>>
>>> cos(4*x+6).subs(x, (u-3)/2)
>>>
>>> And if this is just a special case of a more general substitution, you
>>> can of course do that solve step with sympy.
>>>
>>> I can't tell from your question, but this might not be exactly what you
>>> wanted. For example, look at
>>>
>>> (x*cos(4*x+6)).subs(x, (u-3)/2)
>>>
>>> This will also replace the `x` out front. If you don't want that, you'd
>>> need to do an even more complicated version of what Francesco suggests.
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/e76ad3b1-7fe0-4f3e-9afd-2ff19b1d6124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.