11
$\begingroup$

I am not good in patterns in Mathematica, and so can't figure what I am doing wrong.

This example works, when the exponent is 1

ClearAll[y,x,n]
Cases[{( y[x]/x)^1}, (y[x]/x )^(n_.) :> n]

Mathematica graphics

But when exponent is 2 it does not find it

 Cases[{( y[x]/x)^2}, (y[x]/x )^(n_.) :> n]

Mathematica graphics

Help says

Mathematica graphics

I think this happens, because the font end changes (y[x]/x)^2 to y[x]^2/x^2 before Cases gets hold of it? So the pattern is lost. But when I tried

   Cases[{( y[x]/x)^2}, (y[x]^n_.)/(x^n_.):>n]

It still returned {}

What is the correct way to handle this? I need to find all powers of expressions of form y[x]/x

Tried HoldForm and Verbatim and Inactive but can't get it to work.


Addition This is really an observation and some rambling on my part about this issue. Did not want to put it as comment.

Answer by m_goldberg explains the problem (which comes down to knowing how Mathematica internally layout this expression, i.e. FullForm basically).

But When the exponent is a symbol, then it behaves differently

 Cases[{(y[x]/x)^a},(y[x]/x)^(n_):>n]

Mathematica graphics

This is because of the difference in FullForm between

 FullForm[(y[x]/x)^a]

Mathematica graphics

 FullForm[(y[x]/x)^2]

Mathematica graphics

I would have expected then when the exponent is a, the FullForm should similar to when the exponent is a number, i.e. as follows

 Times[Power[x,-a],Power[y[x],a]]

Or

Mathematica graphics

So my lesson for the day, when using pattern in Mathematica, I need to look at FullForm to get the pattern right because what I see might not be what I think it is.

$\endgroup$
4
  • $\begingroup$ try Cases[{(y[x]/x)^3}, Times[Power[x_, b_], Power[y_, c_]] :> Abs[b]] $\endgroup$
    – Alucard
    Dec 24, 2017 at 21:14
  • 2
    $\begingroup$ @Alucard Yes, using full form like this always works. But the question is, why Cases[{( y[x]/x)^2}, (y[x]/x )^(n_.) :> n] does not? I am writing the pattern as the expression appears. Also help says x^n_. should match. I guess the question becomes, when should one use the FullForm for the pattern, vs. the "form" that appears on the screen that one is looking at. This is the confusing part about patterns in Mathematica. $\endgroup$
    – Nasser
    Dec 24, 2017 at 21:19
  • $\begingroup$ Interesting question. To what part of Help at you referring? $\endgroup$
    – bbgodfrey
    Dec 24, 2017 at 22:35
  • $\begingroup$ @bbgodfrey this is a screen shot I took from some WRI help pages long time ago. I do not know from which document or web page it came from. But I kept this screen shot in my Mathematica cheat sheet here for reference. $\endgroup$
    – Nasser
    Dec 24, 2017 at 22:46

1 Answer 1

10
$\begingroup$

Perhaps

 Cases[{(...}, y[x]^(m_.) x^(n_.) /; m === -n :> m]

is what you are looking for. It returns {2} from

Cases[{(y[x]/x)^2}, y[x]^(m_.) x^(n_.) /; m === -n :> m]
Cases[{y[x]^2/x^2}, y[x]^(m_.) x^(n_.) /; m === -n :> m]

but returns {} from

Cases[{y[x]^2/x^3}, y[x]^(m_.) x^(n_.) /; m === -n :> m]
Cases[{y[x]^3/x^2}, y[x]^(m_.) x^(n_.) /; m === -n :> m]

The problem arises because, internally, Mathematica sees the exponent of y[x] and x as different integers (2 and -2) and it is necessary to inform it that one of the integers must be the negative of the other.

$\endgroup$

Your Answer

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

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