MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Ugly expressions. Was: Toghether, Apart ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17142] Re: Ugly expressions. Was: Toghether, Apart ?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 17 Apr 1999 03:35:26 -0400
  • References: <7f1di1$kop@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Tobias,
Some variants on your code, FactorCommon

Clear[FactorCommon2];
FactorCommon2[HoldPattern[Plus[e__Times]]] :=
       (  #(Plus @@ ( {e}/#))) &[Intersection[e]
];
FactorCommon2[HoldPattern[Plus[e__]]] :=
 With[{ d = d1  d2 },
         (  #(Plus @@ ( {e}/#))) &[cc = (Intersection @@ ( d{e}))/d]
];
 FactorCommon2[expr_] := expr

Here id FactorCommon:

Clear[FactorCommon];
 FactorCommon[expr_Plus] :=
 Block[{(*com, dummy1, dummy2*)},
com = Apply[Intersection, dummy1 dummy2 Apply[List, expr]] /.
{dummy1 -> 1, dummy2 -> 1};
com Map[#/com &, expr]
]
 FactorCommon[expr_] := expr

Some timings

a b c d e f
Apply[Plus, Apply[Times, Table[e[i + j*10], {j, 0, 20}, {i, 0, 20}], {1}]]];

Do[FactorCommon2[ex], {100}] // Timing

{3.74 Second, Null}

Do[FactorCommon2[ex + a ], {100}] // Timing

{5.77 Second, Null}

Do[FactorCommon2[ex + 3], {100}] // Timing

{2.48 Second, Null}

Do[FactorCommon[ex], {100}] // Timing

{6.48 Second, Null}

Do[FactorCommon[ex + a], {100}] // Timing

{5.71 Second, Null}

Do[FactorCommon[ex + 3], {100}] // Timing

{2.42 Second, Null}


<tobiasoed at my-dejanews.com> wrote in message news:7f1di1$kop at smc.vnet.net...
> Hi all.
> There was a post about handling long expressions (leafcount > 10^7). I am
> manipulating the same kind of nasty things and am interested in any little
> function that deals with them (without running out of memory) - like the
> following FactorCommon. that pulls out common factors of sums.
>
> FactorCommon[expr_Plus]:=Block[{com,dummy1,dummy2},
> com=Apply[Intersection,dummy1 dummy2 Apply[List,expr]]
> /.                            {dummy1->1,dummy2->1};
> com Map[#/com &,expr]
> ]
>
> FactorCommon[expr_]:=expr
>
> an example use:
>
> In[1]:= FactorCommon[expr_Plus]:=Block[{com,dummy1,dummy2},
>                 com=Apply[Intersection,dummy1 dummy2 Apply[List,expr]] /.
> {dummy1->1,dummy2->1};
>                 com Map[#/com &,expr]
>         ]
>
> In[2]:= FactorCommon[expr_]:=expr
>
> In[3]:= ex = Expand[a b
> Apply[Plus,Apply[Times,Table[e[i+j*10],{j,0,8},{i,0,9}],{1}]]]
>
> Out[3]= a b e[0] e[1] e[2] e[3] e[4] e[5] e[6] e[7] e[8] e[9] +
>
> >    a b e[10] e[11] e[12] e[13] e[14] e[15] e[16] e[17] e[18] e[19] +
>
> >    a b e[20] e[21] e[22] e[23] e[24] e[25] e[26] e[27] e[28] e[29] +
>
> >    a b e[30] e[31] e[32] e[33] e[34] e[35] e[36] e[37] e[38] e[39] +
>
> >    a b e[40] e[41] e[42] e[43] e[44] e[45] e[46] e[47] e[48] e[49] +
>
> >    a b e[50] e[51] e[52] e[53] e[54] e[55] e[56] e[57] e[58] e[59] +
>
> >    a b e[60] e[61] e[62] e[63] e[64] e[65] e[66] e[67] e[68] e[69] +
>
> >    a b e[70] e[71] e[72] e[73] e[74] e[75] e[76] e[77] e[78] e[79] +
>
> >    a b e[80] e[81] e[82] e[83] e[84] e[85] e[86] e[87] e[88] e[89]
>
> In[4]:= Timing[FactorCommon[ex]][[1]]
>
> Out[4]= 0.04 Second
>
> In[5]:= Timing[Factor[ex]][[1]]
>
> Out[5]= 0.73 Second
>
> Sure, FactorCommon is not as powerfull as Factor, but sometimes it's the
only
> way I found to reduce some of my exprssions.
> Hope this can help some others too, Tobias.
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
>



  • Prev by Date: Re: Continued fraction problem
  • Next by Date: How do I save a Mathematica Animations
  • Previous by thread: Ugly expressions. Was: Toghether, Apart ?
  • Next by thread: How do I solve limits in Mathematica?