Re: Linearization
- To: mathgroup at smc.vnet.net
- Subject: [mg15185] Re: Linearization
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 18 Dec 1998 02:11:01 -0500
- References: <754vm1$jgc@smc.vnet.net> <75a0ll$rtv@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Daniel Lichtblau emailed me >>> Normal[Series[...]] may be more efficient than expansion, especially in cases where the latter might cause memory problems. Below is the code, it is a minor modification of your version. In[1]:= Linearize[poly_, vars_] := Module[{u}, Normal[Series[poly /. Thread[vars -> (vars u)], {u,0,1}]] /. u->1] In[2]:= Linearize[1 + u + (a + b)x + x u + x^2 + u^2, {x, u}] Out[2]= 1 + u + (a + b) x <<< And several other postings used this Series approach So I took another look: If we have to start from a general function (which was the original question) and generate its linearization then the direct use of series is faster and uses less memory then getting the series and then truncating it. However, if we are already working with polynomials in the variables it seems that the Expand form is quicker and uses less memory (at least on the example that I have used): I include two variants using Collect and CoefficientList that appear to be faster again and also to use less memory. Method Using Memory Used Timing/seconds LinearizeE Expand 2744 0.27 LinearizeS Series 3496 0.55 LinearizeC Collect 2648 0.11 LinearizeCL CoefficientList 2648 0.11 LinearizeE[poly_, vars_] := Module[{u}, Expand[poly, Alternatives@@vars] /. Thread[vars -> (vars u)] /. {u^n_ -> 0, u -> 1}] LinearizeS[poly_, vars_] := Module[{u}, Normal[Series[poly /. Thread[vars -> (vars u)], {u, 0, 1}]] /. u -> 1] LinearizeC[poly_, vars_] := Module[{u}, Collect[poly /. Thread[vars -> (vars u)], u] /. {u^n_ -> 0, u -> 1}] LinearizeCL[poly_, vars_] := Module[{u}, Plus@@Take[CoefficientList[poly /. Thread[vars -> (vars u)], u], 2] ] pol = Collect[Normal[Series[f[x, y, z], {x, 0, 6}, {y, 0, 6}, {z, 0, 6}]], {x, y, z}] ; m1 = MemoryInUse[]; (le = LinearizeE[pol, {x, y}]); // Timing // First 0.27 Second m2 = MemoryInUse[]; m2 - m1 2744 (ls = LinearizeS[pol, {x, y}]); // Timing // First 0.55 Second m3 = MemoryInUse[]; m3 - m2 3496 (lc = LinearizeC[pol, {x, y}];) // Timing // First 0.11 Second m4 = MemoryInUse[]; m4 - m3 2648 (lcl = LinearizeCL[pol, {x, y}];) // Timing // First 0.11 Second m5 = MemoryInUse[]; m5 - m4 2648 Check Expand[lc] == Expand[le ] == Expand[ls] == Expand[lcl] True Allan, --------------------- Allan Hayes Mathematica Training and Consulting www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565