# default value for the 2nd argument in Power for pattern matching

I'm trying to match terms in power like a^b, including b=1 case. I know that I can do something like x_^y_., but by looking at the documentation, I didn't find any words saying that the default value of the second argument of power is 1, so how to understand that x_^y_. can match the symbol x when y=1?

• The pattern _^1 is equivalent to _, so this would not make much sense... Commented May 31, 2018 at 18:56
• @HenrikSchumacher I'm sorry, but say I have a list {a, b^2, c^3} and now I want to apply rules to this list. b^2 and c^3 can be matched by powers, and to keep the rules as simple as possible, I want the element a to be matched as well, and if you were me, how would you match this element? From practicing, I know that pattern like x_^y_.  will match all the elements in the list, but I just don't understand why the default value of y is taken to be 1.
– Y.Du
Commented May 31, 2018 at 19:00
• @MarcoB I agree that the pattern matcher will not try to interpret a as Power[a,1], it will instead just take a as a Symbol. What my problem is I want to match a, b^2, c^3 etc consistently with only ONE rule to keep my code simple and then manipulate them afterwards. With that being said, I find patter like x_^y_. works like a charm, but then I have the difficulty in understanding why the default value of y is set to be one since the Power function does not deal with things like Power[a,1] as you have said.
– Y.Du
Commented May 31, 2018 at 19:13
• @MarcoB you can match expression that do not contain explicitly the pattern head. {a, 2 a, 3 a} /. x_. f[a] :> x gives {f[1], f[2], f[3]} and there is no such thing as Times[1, a]. Commented May 31, 2018 at 19:21
• @Batracos Can't reproduce your result. You sure that's what you meant? It returns {a, 2 a, 3 a} on my end, and MatchQ[x_. f[a]] /@ {a, 2 a, 3 a} returns {False, False, False}. Commented May 31, 2018 at 19:30

You're looking for DefaultValues:

DefaultValues[Power]


{HoldPattern[Default[Power, 2]] :> 1}

And, to address some confusion in the comments, notice that the pattern _^_. does match a:

a /. _^_. -> 1


1

You can also query the default value using Default:

Default[Power, 2]


1

• @MarcoB I think unfortunately the *Values functions are rather un(der)documented. Commented May 31, 2018 at 19:41
• Thanks, this is what I'm looking for.
– Y.Du
Commented May 31, 2018 at 19:44
• @CarlWoll That's too bad. Thank you for adding the Default alternative though. Commented May 31, 2018 at 20:33

An interesting gotcha related to the DefaultValue for Power. Suppose you want to eliminate all odd powers from the polynomial

poly = x + x^2 + x^3 + x^4


First consider

poly /. x^n_ /; OddQ[n] :> g[n]


This behaves as expected but, of course, does not match x by itself. Now try

poly /. x^(n_.) /; OddQ[n] :> g[n]


It took me far too long to realise that this output is explained by noting that, with the default value for the second argument of power, the pattern x^2 is equivalent to the pattern (x^1)^2. I'd bet that this will lead to some puzzlement when you try

poly /. x^(n_.) /; OddQ[n] :> 0


(naively) expecting instead to get x^2 + x^4 as the result.

• Thank you for your input and sorry for the late reply. This is actually an interesting point that I did not realize. I tried and found that EvenQ would work as expected. The problem is with OddQ. I think it's worthy of posting a new post on this topic.
– Y.Du
Commented May 22, 2019 at 13:49