On Sun, 20 Sep 2020 at 10:42, David Bailey <d...@dbailey.co.uk> wrote:
>
> On 19/09/2020 23:46, Oscar Benjamin wrote:
>
> On Sat, 19 Sep 2020 at 23:35, David Bailey <d...@dbailey.co.uk> wrote:
>
> I mean suppose I wanted to replace
>
> f(anything) by g(5*anything**2)
>
> Hi David,
>
> You can do this using replace but instead of replacing f with g you
> can ask to replace all instances of f with a function that will
> process the argument(s) that f had. It is convenient to do this in
> Python using a lambda function:
>
> In [7]: f = Function('f')
>
> In [8]: g = Function('g')
>
> In [9]: expr = f(1 + sqrt(2))
>
> In [10]: expr.replace(f, lambda arg: g(5*arg**2))
> Out[10]:
>  ⎛          2⎞
> g⎝5⋅(1 + √2) ⎠
>
> I'm not sure how well that displays in the email. It looks pretty
> screwy in gmail as I write it but I think it does what you want!
>
> Oscar
>
> Thanks Oscar,
>
> Wow - that is ingenious, and it will take me a while to think out how much of 
> Mathematica's pattern functionality can be achieved this way.
>
> However, I am curious, What are the patterns produced by the Wild function 
> actually used for - or are these an attempt to reproduce Mathematica's 
> pattern functionality (which is really neat) that is yet to be finished?

Hi David (CCing the list),

I haven't used Mathematica's pattern functionality myself so I'm
interested to hear how it compares.

The patterns made with Wild are used to extract the components of the
expression that match the Wild symbols so that you can use them for
other things. Suppose that I want to test if an expression is of the
form sin(a*x + b) and if so identify a and b so that I can use them to
build another expression. Then I can do:

In [7]: a = Wild('a')

In [8]: b = Wild('b')

In [9]: x = Symbol('x')

In [10]: expr = sin(sqrt(2)*x - 1)

In [11]: pattern = sin(a*x + b)

In [12]: expr.match(pattern)
Out[12]: {a: √2, b: -1}

In [13]: m = expr.match(pattern)

In [14]: cos(m[a]*x + m[b])
Out[14]: cos(√2⋅x - 1)

Of course you might want to do something more complicated than just
replace sin with cos so it is useful to be able to extract the values
m[a] and m[b]. For example if m[a] was equal to 2 you could apply the
double angle formula or something like that.

You can also use the Wild symbols with replace like:

In [7]: expr.replace(sin(a*x + b), cos(a*x + b))
Out[7]: cos(√2⋅x - 1)

You can also use a Wild pattern with find to extract sub-expressions
matching a pattern:

In [7]: expr = Integral(1 + sin(2*x + 1) + sin(3*x), x)

In [8]: expr
Out[8]:
⌠
⎮ (sin(3⋅x) + sin(2⋅x + 1) + 1) dx
⌡

In [9]: expr.find(sin(a*x + b))
Out[9]: {sin(3⋅x), sin(2⋅x + 1)}

This basically calls match recursively on all subtrees of the
expression so it can be quite inefficient for larger expressions. If
the objective was just to find all examples of sin then atoms is more
efficient:

In [10]: expr.atoms(sin)
Out[10]: {sin(3⋅x), sin(2⋅x + 1)}


Oscar

-- 
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 sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxTU%3DswUw41aky%3DH_GNvHBXx%2BqS%3D9Fb98oC7HHjsm%3DSDtg%40mail.gmail.com.

Reply via email to