0
$\begingroup$

I was trying to match some polynomials, but this is weird:

Plus[Power[x, 2], Power[y, 2]]~MatchQ~Plus[Power[_, 2] ..]
SetAttributes[f, Attributes@Plus]
f[Power[x, 2], Power[y, 2]]~MatchQ~f[Power[_, 2] ..]

Despite f having all of the same attributes as Plus, the expressions match only when it is used

False True

I found this is the root of the problem:

Plus[Power[_, 2] ..] // FullForm

gives Repeated[Power[Blank[],2]], the Plus entirely disappears, while

f[Power[_, 2] ..] // FullForm

gives f[Repeated[Power[Blank[],2]]] as I would expect.

I understand many built-in symbols escape the standard evaluation procedure anyways, so that not all of their behaviour is readily understandable from their Attributes etc. Obviously, Plus wrongly applies something like OneIdentity in this case, causing it to be removed. A user-defined symbol such as f doesn't do it here.

Is this a bug?

$\endgroup$
  • 3
    $\begingroup$ Isn't only this f[x_] := x enough to replicate Plus in this case? P.s. HoldPattern[Plus[Power[_, 2] ..]] will work btw. $\endgroup$ – Kuba Aug 18 '16 at 11:42
  • 1
    $\begingroup$ To add to Kuba's remark: Is it OneIdentity or simply "Plus[x] is x" as stated in the docs for Plus? $\endgroup$ – Michael E2 Aug 18 '16 at 12:15
2
$\begingroup$

As pointed out in comments, this is caused by

Plus[x] is x

Here are some workarounds

Plus[Power[x, 2], Power[y, 2]]~MatchQ~Verbatim[Plus][Power[_, 2] ..]
Plus[Power[x, 2], Power[y, 2]]~MatchQ~HoldPattern[Plus][Power[_, 2] ..]
Plus[Power[x, 2], Power[y, 2]]~MatchQ~HoldPattern[Plus[Power[_, 2] ..]]

all giving True as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.