Ugly expressions. Was: Toghether, Apart ?
- To: mathgroup at smc.vnet.net
- Subject: [mg17036] Ugly expressions. Was: Toghether, Apart ?
- From: tobiasoed at my-dejanews.com
- Date: Wed, 14 Apr 1999 02:11:52 -0400
- Organization: Deja News - The Leader in Internet Discussion
- Sender: owner-wri-mathgroup at wolfram.com
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