Re: Re: Re: how to find n in expression x^n using a pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg58536] Re: [mg58512] Re: [mg58487] Re: [mg58426] how to find n in expression x^n using a pattern?
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Wed, 6 Jul 2005 03:11:21 -0400 (EDT)
- References: <200507020806.EAA01589@smc.vnet.net> <200507040624.CAA05758@smc.vnet.net> <26A87A3E-2A81-483F-B880-FD4E30020592@gmail.com> <42C956F0.6090509@umbc.edu> <200507050557.BAA29462@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Andrzej Kozlowski wrote:
> On 5 Jul 2005, at 00:34, Pratik Desai wrote:
>
>
>>Andrzej Kozlowski wrote:
>>
>>
>>
>>>On 4 Jul 2005, at 15:24, Pratik Desai wrote:
>>>
>>>
>>>
>>>>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
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>Why not do something like this , it sounds quite simplistic but
>>>>it works :)
>>>>Clear[p]
>>>>p[x_] = x ^5(x^3 + 2x^2 + 3x - 13)
>>>>sol2 = Solve[p[x] == 0, x]
>>>>y = x /. sol2
>>>>n = Count[y, 0]
>>>>
>>>>Best regards
>>>>
>>>>--
>>>>Pratik Desai
>>>>Graduate Student
>>>>UMBC
>>>>Department of Mechanical Engineering
>>>>Phone: 410 455 8134
>>>>
>>>>
>>>>
>>>>
>>>
>>>In fact after seeing this posting I understood what the original
>>>poster wanted (probably). (He wrote that he was learning patterns
>>>so I thought this was meant just to be an exercise in pattern
>>>matching). However, using algebraic method, like the one above,
>>>could be unnecessarily complicated if the second factor of the
>>>equation is a high degree polynomial or something even more
>>>complicated. It is in fact easier to use patterns. Compare:
>>>
>>>p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13);
>>>
>>>
>>>Timing[Cases[p[x], x^(n_.) -> n, {1}]]
>>>
>>>
>>>{0.0005019999999973379*Second, {5}}
>>>
>>>(as I wrote in my first response, giving the correct level
>>>specification is important).
>>>
>>>
>>>Timing[Count[x /. Solve[p[x] == 0, x], 0]]
>>>
>>>
>>>{0.08699899999999872*Second, 5}
>>>
>>>
>>>
>>>Andrzej Kozlowski
>>>
>>>Chiba, Japan
>>>
>>>
>>>
>>
>>Hi Andrzej
>>
>>I have had occasion to use patterns before and by far I have found
>>it the most difficult feature of mathematica to work with and the
>>help on it leaves a lot to be desired. I agree the poster seems to
>>be trying to learn patterns, I was just trying to point out, as you
>>have on your earlier reply, that there are easier ways to do the
>>same job (i.e finding the highest common exponent in x). Again not
>>to put a finer point on it, it seems to me that the use of
>>patterning as you have shown seems only to work when the expression
>>is in its factored form (which I admit was the posters original
>>query) whereas using Solve and count works irrespective of the
>>form of the expression. Or am I wrong?
>
>
> You are of course right. Mathematica's pattern matching is (with some
> rudimentary exceptions) only syntactic, which means that expressions
> are matched "literally" and not according to their "mathematical
> meaning" (that would be "semantic" pattern matching). A factored
> expression and an unfactored oen are quite different as far as
> pattern matching is concerned. Algebraic methods can do much more
> than pattern matching but they tend to be very slow and suitable
> mostly for "small" problems. So both approaches have their strong
> and weak points and one or the other should be preferred depending on
> what one is trying to do. Obviously there is no panacea for all types
> of situations, which is why posters should try to state exactly what
> they are really trying to do.
>
> Andrzej Kozlowski
>
>
>
>
>
>
>>p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5);
>>Timing[Cases[p[x], x^(n_.) -> n, {1}]]
>>
>>>>{0. Second, {5}}
>>
>>Timing[Count[x /. Solve[p[x] == 0, x], 0]]
>>
>>>>{0.01 Second, 10}
>>
>>Now if you use simplify
>>p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5) // Simplify;
>>Timing[Cases[p[x], x^(n_.) -> n, {1}]]
>>{0. Second, {10}}
>>
>>Best regards
>>Pratik
>>
>>--
>>Pratik Desai
>>Graduate Student
>>UMBC
>>Department of Mechanical Engineering
>>Phone: 410 455 8134
A method that is reasonably fast but (for most purposes) more robust
than syntactic approaches is to use Exponent with a third argument.
In[3]:= Exponent[x^5*(x^32 + 2x^21 + 3x^17 - 13x^5), x, Min]
Out[3]= 10
Since the topic was broached, there was some Mathematica work on
semantic pattern matching a few years back. You might want to look at
Jason Harris. Semantica: semantic pattern matching in Mathematica. The
Mathematica Journal 7(3):329-360.
Further information about the article may be found at
http://library.wolfram.com/infocenter/Articles/3902/
Daniel Lichtblau
Wolfram Research
- References:
- how to find n in expression x^n using a pattern?
- From: "steve" <nma124@hotmail.com>
- Re: how to find n in expression x^n using a pattern?
- From: Pratik Desai <pdesai1@umbc.edu>
- Re: Re: how to find n in expression x^n using a pattern?
- From: Andrzej Kozlowski <akozlowski@gmail.com>
- how to find n in expression x^n using a pattern?