Re: Pattern matching "on the fly"
- To: mathgroup at smc.vnet.net
- Subject: [mg30112] Re: Pattern matching "on the fly"
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sat, 28 Jul 2001 22:08:50 -0400 (EDT)
- References: <9jtk77$87r$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Tony, Not a general solution, but for your example we get (1+x)^1000000 +O[x]^3//Normal//Timing {0.27*Second, 1 + 1000000*x + 499999500000*x^2} -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Tony MacKenzie" <mackenzi at usq.edu.au> wrote in message news:9jtk77$87r$1 at smc.vnet.net... > Hello from down under, > > I am trying to use Mathematica's pattern matching abilities to achieve > something I can do using the computer algebra software REDUCE. I want to do > this because I need to take advantage of the powerful numerical algorithms > of Mathematica. > > I want to set all powers of some variable greater than a certain value to > zero, but I want mathematica to apply this pattern matching "on the fly" > (while an expression is being evaluated, not after the expression has been > evaluated). > > I'll give a quick example. I want to set all powers of x greater than 2 to > zero i.e. x^3=>0, x^4=>0 and so on. In REDUCE I can use the following > statement > > let x^3=>0; (* This replaces x^3 with 0, x^4 with 0 and so on*) > Then if I evaluate (1+x)^1000000, I quickly (a few seconds) find this > expression gives 499999500000*x^2 + 1000000*x + 1. The pattern matching is > done as the expression is being expanded. > > Now in Mathematica I have tried x^n_ ^:=0 /; n> 2; This works, but it only > appears to be applied after an expression has been expanded. For example if > I try > > x^n_ ^:=0 /; n> 2; > Expand[(1+x)^1000000]; > > in Mathematica, the evaluation is very slow (which I think is because the > pattern matching is applied after the expansion and not on the fly). > > Any help would be greatly appreciated as I am very new to Mathematica. > > Tony MacKenzie > University of Southern Queensland, Australia > > > > >