Let's say we want a pattern, that matches, when expression contains some terms but it can optionaly contain some other terms. For example:
Cases[
{-x^2 + Log@x + Sin@x, 3.14 x^2 + Log@x, x^2, x^2 + Tan@x},
(((?NumericQ)x^2|x^2) + Log@x + Sin@x)|
(((?NumericQ)x^2|x^2) + Log@x)|
(((?NumericQ)x^2|x^2) + Sin@x)|
(((?NumericQ)x^2|x^2))
]
Out:
{-x^2 + Log[x] + Sin[x], 3.14 x^2 + Log[x], x^2}
In this example expression has to contain $a x^2$, but it can also contain $\sin(x)$ and $\ln(x)$. In my real example, I have $6$ optional terms, meaning, I would have to write $2^6=64$ versions.
Instead I would like to do something like this, where NothingP woluld be equivalent of Nothing for patterns:
Cases[
{-x^2 + Log@x + Sin@x, 3.14 x^2 + Log@x, x^2, x^2 + Tan@x},
(((_?NumericQ)x^2|x^2) + (Log@x|NothingP) + (Sin@x|NothingP))
]
So is there such a command/ another way to do it? I can't find it in a documentation.
Also a side question: is there a short form of ((_?NumericQ)x^2|x^2) ?
((_?NumericQ)x^2|x^2)there is _.x^2. Basically _.*expression means that the expression is multiplied by something or 1. Also _.+expression means expression plus something or 0. You can check OneIdentity and Default for more details. – userrandrand Sep 16 '22 at 12:48(not ' theused when using packages in Mathematica with Needs) – userrandrand Sep 16 '22 at 13:28_.+expression, that you mentioned. How to use it in this example. – GalZoidberg Sep 16 '22 at 13:45_.*x^2 + b : (__?(MemberQ[{Log[x], Sin[x]}, #] &)) : 0– userrandrand Sep 16 '22 at 14:02