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 smc.vnet.net
  • Subject: [mg58496] Re: [mg58426] how to find n in expression x^n using a pattern?
  • From: stephen layland <layland at wolfram.com>
  • Date: Mon, 4 Jul 2005 02:24:28 -0400 (EDT)
  • Mail-followup-to: steve <nma124@hotmail.com>, mathgroup@smc.vnet.net
  • References: <200507020806.EAA01589@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

and thus spake steve [2005.07.02 @ 04:08]:
> 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

Here' ssomething that might work for you:

(Exponent[Cases[p, x | x^n_], x] /. {} -> {0})[[1]]

It's kind of ugly, but like someone suggested before, first we use
cases to find any occurance of x or x^n_ by itself in the multiplication
expression. We then use the function Exponent to turn the x^n_ or x
into n or 1.   Lastly, we explicitly set the exponent to 0 if we didn't find
anything, and get rid of the list enclosure.

I'm sure there are other methods, however.

Happy hacking!

--
/*------------------------------*\
|        stephen layland         |
|    Documentation Programmer    |
| http://members.wri.com/layland |
\*------------------------------*/


  • Prev by Date: Re: Re: How to suppress plot output ?
  • Next by Date: Re: how to find n in expression x^n using a pattern?
  • Previous by thread: Re: 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?