1
$\begingroup$

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

$\endgroup$
8
  • $\begingroup$ DeleteDuplicates@Cases[expr, (pat1 | pat2) :> D[y[x], x]^n, 2]? $\endgroup$
    – kglr
    Nov 21, 2021 at 10:51
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    Nov 21, 2021 at 11:00
  • $\begingroup$ or use just pat1 with level spec 3: DeleteDuplicates@Cases[expr, pat1 :> D[y[x], x]^n, 3] $\endgroup$
    – kglr
    Nov 21, 2021 at 11:03
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    Nov 21, 2021 at 11:06
  • 1
    $\begingroup$ 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) $\endgroup$
    – kglr
    Nov 21, 2021 at 11:25

1 Answer 1

6
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.