On Sun, 20 Sep 2020 at 21:13, David Bailey <d...@dbailey.co.uk> wrote:
>
> On 20/09/2020 17:29, Oscar Benjamin wrote:
>
> The main difference as far as I tell compared to replace is the need
> to create the Wild symbols explicitly. Maybe we could add a bunch of
> those to sympy.abc with names like p1_ and p2_ (are those names
> standardised?).
>
> Those examples are a revelation! I think they should be added to the 
> documentation of replace and Wild(). I must admit I thought I'd tried 
> something like that with replace, but without success - maybe I hit a bug?
>
> Of course I realise that SymPy can't start defining operators like /. -> etc. 
> In any case, one irritation with Mathematica, is that there are just too many 
> operator precedences to remember!
>
> The only thing that is standard about the names is the _ on the end, the rest 
> of the name is chosen by the user, if you named the pattern variables with a 
> trailing _ in abc , then I think it would be really friendly - I mean I think 
> a CAS needs to be easy to use.

We could add a bunch of a_, b_ etc. It's common in the sympy codebase
to create a Wild with something like Wild('a', exclude=[x]). The
reason is that you might want to exclude some kinds of matches:

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

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

In [3]: ax = Wild('ax', exclude=[x])

In [4]: sin(x**2).match(sin(a*x))
Out[4]: {a: x}

In [5]: sin(x**2).match(sin(ax*x))

In [6]: print(sin(x**2).match(sin(ax*x)))
None

If we had Wilds in abc then they wouldn't have those kinds of
exclusion patterns.

> Given that functionality, the pattern matching looks pretty good. One other 
> thing is that Mathematica also has a way to define a pattern that only 
> matches if a predicate returns True when applied to it, for example:
>
> p1_?Even

This can also be done with Wild using properties:

In [12]: ae = Wild('ae', properties=[lambda a: a.is_even])

In [13]: print(cos(2*x).match(cos(ae*x)))
{ae_: 2}

In [14]: print(cos(3*x).match(cos(ae*x)))
None

> There are also some more obscure things that can be done with Mathematica 
> patterns, but probably these are of lesser importance. Also patterns are used 
> in some other contexts, for example a user defined function is written with 
> pattern variables as arguments. This allows for overloaded functions.
>
> It sounds as if the real thing that needs doing is documenting these features!

Yes, I guess so. I have actually been writing some docs for the old
assumptions since the last thread but they are not yet complete:
https://github.com/sympy/sympy/pull/20090
https://github.com/sympy/sympy/pull/20090/files

> I haven't used this at all myself but you might be interested by mathics:
> http://mathics.github.io/
> ""
> Mathics is a free, general-purpose online computer algebra system
> featuring Mathematica-compatible syntax and functions. It is backed by
> highly extensible Python code, relying on SymPy for most mathematical
> tasks.
> """
>
> I did consider  this, but it became clear that it isn't being actively 
> supported. For example, it required a specific (out of date) version of 
> Python. I'd certainly not get anything like the support that your group 
> offer! Besides, by now, I have got somewhat under the hood of SymPy.

The reason I know of mathics is because mathics contributors have
raised issues with sympy on github so I think there is some
maintenance going on. I just followed the installation instructions
with Python 3.8 and got an error when I tried to run mathics. With
Python 3.7 it worked though. With that I get:

$ mathics

Mathics 1.0
on CPython 3.7.6 (default, Jan  8 2020, 13:42:34)
using SymPy 1.0, mpmath 1.1.0

Copyright (C) 2011-2016 The Mathics Team.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
See the documentation for the full license.

Quit by pressing CONTROL-D

In[1]:= Sin[a*x + b] /. Sin[x*p1_. + p2_.] -> Cos[x*p1 + p2]
Out[1]= Cos[a x + b]


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/CAHVvXxQf8YtQiAyPw6Fu0wDR1R6f%3Dv35EsuCBW_-gWSEG0MYWw%40mail.gmail.com.

Reply via email to