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: [mg58462] Re: [mg58426] how to find n in expression x^n using a pattern?
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sun, 3 Jul 2005 03:57:17 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Steve,

You will probably get a better answer, but I wouldn't use a pattern. I would
use CoefficientList and the Position of the first nonzero entry. You don't
really have to Factor.

expr1 = x^5(x^2 + 2)(x^3 + 4);
expr2 = (x^2 + 2)(x^3 + 4);

minexponent[poly_, var_:x] :=
  First@First@Position[CoefficientList[poly, var], _?(# != 0 &), {1}, 1] - 1

minexponent[expr1]
5

minexponent[expr2]
0

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/




From: steve [mailto:nma124 at hotmail.com]
To: mathgroup at smc.vnet.net


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



  • Prev by Date: Re: how to find n in expression x^n using a pattern?
  • 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?