Quantcast
Channel: Equivalent of Nothing for patterns - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 3

Equivalent of Nothing for patterns

$
0
0

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) ?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images