1

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

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • DeleteDuplicates@Cases[expr, (pat1 | pat2) :> D[y[x], x]^n, 2]? – kglr Nov 21 '21 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. – Nasser Nov 21 '21 at 11:00
  • or use just pat1 with level spec 3: DeleteDuplicates@Cases[expr, pat1 :> D[y[x], x]^n, 3] – kglr Nov 21 '21 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. – Nasser Nov 21 '21 at 11:06
  • You could use a default value: pat = y'[x]^(n_ : 0) ; Cases[expr, pat, -1] // DeleteDuplicates – Daniel Huber Nov 21 '21 at 11:12
  • @DanielHuber sure. That is similar to what I wrote also (but your pattern is shorter which is better). But my main question is not to find workaround, but why when combining the patterns using |, it did not give the union of each result when done separately. I did not know it will work differently. But it does. – Nasser Nov 21 '21 at 11:15
  • 1
    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 '21 at 11:25
  • Looks like a bug to me. Maybe you should report it to Wolfram. – Daniel Huber Nov 21 '21 at 11:38

1 Answers1

6

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]}

enter image description here

Cases[expr2, pat1 :> D[y[x], x]^n]

{Derivative[1][y][x]}

Cases[expr2, pat2 :> D[y[x], x]^n]

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896