Given expression
expr = y'[x]^3 + y'[x]*Log[y[x]*y'[x]^2] + y'[x]^(1/2) + x/y'[x]^7
$$y'(x)^3+\sqrt{y'(x)}+\frac{x}{y'(x)^7}+y'(x) \log \left(y(x) y'(x)^2\right)$$
The goal is pick all $y'(x)^n$ terms out anywhere they show. So the final list should be
$$\left\{\frac{1}{y'(x)^7},\sqrt{y'(x)},y'(x)^2,y'(x),y'(x)^3\right\}$$
The following first pattern finds some subset of them, and the second pattern finds the rest. But when used together using Alternatives
not all pattern are found.
pat1 = _.*D[y[x], x]^n_.;
pat2 = _.*_[_. * D[y[x], x]^n_.];
Cases[expr, pat1 :> D[y[x], x]^n]
$$\left\{\frac{1}{y'(x)^7},\sqrt{y'(x)},y'(x),y'(x)^3\right\}$$
Cases[expr, pat2 :> D[y[x], x]^n]
$$\left\{y'(x)^2\right\}$$
So one would expect that using both in Alternative
then the result will be combined. But it does not
Cases[expr, (pat1 | pat2) :> D[y[x], x]^n]
$$\left\{\frac{1}{y'(x)^7},\sqrt{y'(x)},y'(x)^2,y'(x)^3\right\}$$
You see, the $y'(x)$ does not show up.
Why is that? Should not all the patterns matched when combining the two patterns using |
be the union of each pattern applied separately?
V 12.3.1 on windows 10
DeleteDuplicates@Cases[expr, (pat1 | pat2) :> D[y[x], x]^n, 2]
? $\endgroup$|
not to have any effect on this. But it seems then|
if I understand you, tries one level only (1), compared when there is only one pattern, which will try different levels then? Ok, I think I understand the issue if this is what it is. $\endgroup$pat1
with level spec 3:DeleteDuplicates@Cases[expr, pat1 :> D[y[x], x]^n, 3]
$\endgroup$DeleteDuplicates@Cases[expr, pat1 :> D[y[x], x]^n, Infinity]
also. My main question was I thought using|
will just combine both results. I guess I did not know it will make difference. If you like to make this an answer, will accept it. $\endgroup$Cases[expr, (pat1 | pat2) :> D[y[x], x]^n]
andCases[expr, (pat2 | pat1) :> D[y[x], x]^n]
give different results. (UseTrace
on both to see the source of the difference). (The difference might have to do with this) $\endgroup$