Re: efficient term collection algorithm
- To: mathgroup at smc.vnet.net
- Subject: [mg69177] Re: [mg69155] efficient term collection algorithm
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Fri, 1 Sep 2006 06:40:30 -0400 (EDT)
- References: <200608310839.EAA19589@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Aug 31, 2006, at 4:39 AM, Blake Laing wrote:
> Dear Math Group
>
> I wish to combine terms in a expression which share a denominator,
> such
> as in the following simple case:
>
> In[1]:=
> a/i+b/j+c/i//.Plus[Times[A_.,Power[denom_,-1]],Times[B_.,Power
> [denom_,-1]]]:>
> Factor[Plus[A,B]Power[denom,-1]]//InputForm
> Out[1]//InputForm=
> (a + c)/i + b/j
>
> The actual expression I am working with contains thousands of
> terms, and
> a pairwise algorithm such as this is wholly inadequate. Will one of
> you
> please suggest a more efficient way to combine each additive term in a
> large expression with a shared denominator?
As long as the whole expression is a sum, you can transform it to a
list, sort the terms that have common denominators together, split
into sublists of a common denominator then combine terms with
Together. For example
In[6]:=
Plus @@ Together @ Plus @@@
Split[Sort[List @@ (a/i + b/j + c/i),
OrderedQ[{Denominator[#1], Denominator[#2]}] &],
Denominator[#1] == Denominator[#2] &]
Out[6]=
(a + c)/i + b/j
Regards,
Ssezi