# Why does Cases not find a match to the pattern using ^n_. when n is a number other than 1?

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] But when exponent is 2 it does not find it

 Cases[{( y[x]/x)^2}, (y[x]/x )^(n_.) :> n] Help says 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] This is because of the difference in FullForm between

 FullForm[(y[x]/x)^a] FullForm[(y[x]/x)^2] 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 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.

• try Cases[{(y[x]/x)^3}, Times[Power[x_, b_], Power[y_, c_]] :> Abs[b]] Dec 24, 2017 at 21:14
• @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. Dec 24, 2017 at 21:19
• Interesting question. To what part of Help at you referring? Dec 24, 2017 at 22:35
• @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. Dec 24, 2017 at 22:46

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.