# why combining 2 patterns in Alternatives in Cases do not give same output as when each pattern is used separately?

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]?
– kglr
Nov 21, 2021 at 10:51
• @kglr But each pattern works on its own, so one would expect using | 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. Nov 21, 2021 at 11:00
• or use just pat1 with level spec 3: DeleteDuplicates@Cases[expr, pat1 :> D[y[x], x]^n, 3]
– kglr
Nov 21, 2021 at 11:03
• @kglr sure, I could ofcourse do 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. Nov 21, 2021 at 11:06
• Cases[expr, (pat1 | pat2) :> D[y[x], x]^n]  and Cases[expr, (pat2 | pat1) :> D[y[x], x]^n]  give different results. (Use Trace on both to see the source of the difference). (The difference might have to do with this)
– kglr
Nov 21, 2021 at 11:25

The difference is due to the fact that the second term in expr matches both pat1 and pat2 (pat1 matches the leaf y'[x] and pat2 matches the leaf Log[y[x] (y'[x]^2]) and when pat1|pat2 is used with default level specification (1) the first pattern returns y'[x].

Using a simpler input expression:

expr2 = {y'[x]*Log[y[x]*y'[x]^2]} Cases[expr2, pat1 :> D[y[x], x]^n] Cases[expr2, pat2 :> D[y[x], x]^n] Cases[expr2, pat1 | pat2 :> D[y[x], x]^n] Cases[expr2, pat2 | pat1 :> D[y[x], x]^n] Trace[Cases[expr2, pat1 | pat2 :> D[y[x], x]^n]] // Column Trace[Cases[expr2, pat2 | pat1 :> D[y[x], x]^n]] // Column When we use the third argument to specify a deeper level, both orders give the same result:

Cases[expr2, pat1 | pat2 :> D[y[x], x]^n, Infinity] // DeleteDuplicates Cases[expr2, pat2 | pat1 :> D[y[x], x]^n, Infinity] // DeleteDuplicates 