On Sun, 20 Sep 2020 at 12:40, David Bailey <[email protected]> wrote:
>
> Hi David (CCing the list),
>
> Yes, I know - I need to remember to press"Reply List" rather than "Reply" in
> Thunderbird!
The reply-to header for the list is set to go back to the list so when
I use reply it always goes to the list.
> I haven't used Mathematica's pattern functionality myself so I'm
> interested to hear how it compares.
>
> Sure - to use your example:
>
>
> In[2]:= Sin[a*x + b] /. Sin[x*p1_. + p2_.] -> Cos[x*p1 + p2]
>
> Out[2]= Cos[b + a x]
This can be done with replace:
In [37]: x, a, b = symbols('x, a, b')
In [38]: p1 = Wild('p1')
In [39]: p2 = Wild('p2')
In [40]: sin(a*x + b).replace(sin(p1*x + p2), cos(p1*x + p2))
Out[40]: cos(a⋅x + b)
> p1_ matches anything and p1_. also matches nothing by default in the context
> of multiplication (i.e. 1). Since p2_. is used in the context of addition,
> it matches zero by default. On the RHS there are referred to as p1 and p2
> (which implies some rather complicated scoping rules that only apply within
> the replacement operation.
>
> Here I make use of both the defaults (p1=1 and p2=0)
>
> In[5]:= Sin[x] /. Sin[x*p1_. + p2_.] -> Cos[x*p1 + p2]unwanted
>
> Out[5]= Cos[x]
It is possible to match with values of 1 or 0 for the wild symbols:
In [47]: sin(x).match(sin(p1*x + p2))
Out[47]: {p₁: 1, p₂: 0}
I'm not sure why that doesn't carry through to replace when p2 should
be zero (possibly a bug):
In [49]: sin(x).replace(sin(p1*x + p2), cos(p1*x + p2))
Out[49]: sin(x)
In [50]: sin(2*x).replace(sin(p1*x + p2), cos(p1*x + p2))
Out[50]: sin(2⋅x)
In [51]: sin(x + 1).replace(sin(p1*x + p2), cos(p1*x + p2))
Out[51]: cos(x + 1)
> These replacements do not have to be at the top level:
>
> In[6]:= Exp[Sin[a*x + b]] /. Sin[x*p1_ + p2_.] -> Cos[x*p1 + p2]
>
> Out[6]= E^Cos[b + a x]
That's also the case with replace:
In [52]: exp(sin(a*x + b)).replace(sin(p1*x + p2), cos(p1*x + p2))
Out[52]:
cos(a⋅x + b)
ℯ
> There is also a variant of ->, :> which suppressed certain possible
> evaluations, but My Mathematica is fairly rusty.
>
> Of course every Mathematica operator - such as '->' and '/.' has a long form
> which avoids remembering all the various operator precedences.
>
> The sheer compactness of these replacements makes replacement operations very
> popular for algebraic manipulations.
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?).
> Because you do actually extract the values taken on by the patterns in those
> examples, I rather wonder if something more like Mathematica replacements
> could be constructed out of what is already available in SymPy?
I think so. What more would you want compared to the replace examples
above? SymPy itself is embedded within the Python programming language
and so direct use of sympy can not invent syntactic operators like /.
because those aren't legal Python syntax. Within the constraints of
Python though what would make the wildcard matching more palatable?
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.
"""
Since mathics is implemented in Python but allows users to write code
in another language it can give users whatever syntax it wants. I
don't know if the /. syntax is supported.
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAHVvXxQcm6N_PQS2AfNrFJWF4%3DLS-jowvXCr%2B9iO%2BpXvJAVRGQ%40mail.gmail.com.