MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to find n in expression x^n using a pattern?

  • To: mathgroup at
  • Subject: [mg58456] Re: [mg58426] how to find n in expression x^n using a pattern?
  • From: Andrzej Kozlowski <akozlowski at>
  • Date: Sun, 3 Jul 2005 03:57:08 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

On 2 Jul 2005, at 17:06, steve wrote:

> I am learning patterns in Mathemtica, and I thought this will be easy.
> suppose you are given a factored expression, such as x^n(x^2+2)(x^3+4)
> where 'n' above can be any number, including zero (i.e. there is no
> (x) as a separate factor). I need to find 'n', which can be
> 0,1,2,3,..etc..
> i.e I need to find the power of the factor x by itself if it exist in
> the expression. not (x^n+anything), just (x^n) byitself.
> For example
> p= x (x^2+2)  ---> here n=1
> I tried this
> p /. x^n_(any_) -> n
> This did not work since x^1 does not match pattern x^n_.  I would have
> worked if I had x^2 (x^3+2) for example.
> I know that I need to use something like  x^_:1  to make it match x,
> which
> is really x^1, but I do not know how to use it in a replacement rule,
> becuase
> when I write
> p /. (x^n_:1)(any_) -> n
> it is an error.
> If I write
> p /. (x^_:1)(any_) -> _
> I do not get back anything.
> Next I tried the Cases command, and again had no luck.
> The main problem is that Mathematica makes a difference between
> x, x^1, and x^2, etc... when it comes to matching with pattern x^n_
> any idea how to do this?
> thanks,
> steve

Actually there are many problems with your approach.  First of all,  
your Attempt to use optional fails because of syntax. You shoudl have  
x^(n_:1) or x^Optional[Pattern[n, Blank[]], 1]. But in any case the  
result what have been quite different form what I think you expected:

p= x (x^2+2);



Of course what happened was that x was replaced by 1 and x^2 by 2.  
This is not the way to "find the power of the factor x by itself".  
Perhaps what you would have preferred is something like:

Cases[x (x^2+2) , x^(n_:1)->n,{1,2}]


Note however that you have to be careful about the levels here. There  
is a x on Level 1 of the expression and x^2 at level 2.
In fact level 1 looks like this:

Level[p, {1}]

{x, x^2 + 2}

and level 2 like:

Level[p, {2}]

{2, x^2}

But there is also x at level 3:

Level[p, {3}]


Which means that if you uses Cases with the commonly used level  
setting Infinity you would get a misleading answer:

Cases[x (x^2+2) , x^(n_:1)->n,Infinity]


You do not tell us whether you are simply learning to use patterns or  
trying t solve some problem  in polynomial algebra. for the latter  
task patterns are generally not the right tool to use, instead one  
should use GroebnerBasis and PolynomialReduce. But there is not much  
point in trying to answer questions nobody asked asked ;-)

Andrzej Kozlowski

Chiba, JAPAN

  • Prev by Date: Re: How to suppress plot output ?
  • Next by Date: Re: Re: Can't assign value to symbols
  • Previous by thread: how to find n in expression x^n using a pattern?
  • Next by thread: Re: how to find n in expression x^n using a pattern?