Collecting denominators together with Mathematica

  • Mathematica
  • Thread starter anthony2005
  • Start date
  • #1

Main Question or Discussion Point

Hi all,
I have a very simple issue, but can't get round of it.

I have a long sum of rational functions, which is in the form for example

[itex]\frac{n_{1}}{d_{1}}+\frac{n_{2}}{d_{1}}+\frac{n_{3}}{d_{1}}+\frac{n_{4}}{d_{1}}+\frac{n_{5}}{d_{2}}+\frac{n_{6}}{d_{2}}+\frac{n_{7}}{d_{1}\cdot d_{2}}+\frac{n_{8}}{d_{1}\cdot d_{2}}+\frac{n_{9}}{d_{1}\cdot d_{2}} [/itex]

and I would like to put it in the form

[itex]\frac{n_{1}+n_{2}+n_{3}+n_{4}}{d_{1}}+\frac{n_{5}+n_{6}}{d_{2}}+\frac{n_{7}+n_{8}+n_{9}}{d_{1}\cdot d_{2}} [/itex]

If I use Factor or Simplify it would just put all over one single denominator [itex]d_{1}\cdot d_{2} [/itex], taking also so much time since my expression is huge.

Any help?
Thanks
 

Answers and Replies

  • #2
1,069
29
Figuring out how and why this works will be an educational experience.

In[1]:= f=n1/d1+n2/d1+n3/d1+n4/d1+n5/d2+n6/d2+n7/(d1 d2)+n8/(d1 d2)+n9/(d1 d2);

In[2]:= Plus@@Map[Together[Select[f,Function[x, Denominator[x]==#]]]&,Union[Map[ Denominator,List@@f]]]

Out[2]= (n1 + n2 + n3 + n4)/d1 + (n5 + n6)/d2 + (n7 + n8 + n9)/(d1*d2)

Be careful with that, just in case some oddball input breaks it.
 
  • #3
Wow, it worked. Miracles are hidden in mathematica, but I'll figure out why it worked.
Thanks a lot.
 
  • #4
Hepth
Gold Member
448
39
I always find ways to use Collect and get it to look right myself.

Collect[f /. {1/(d1*d2) -> 1/d1d2}, {d1, d2, d1d2}] /. d1d2 -> d1 d2

but the way above with mapping is much better and more robust.
 

Related Threads for: Collecting denominators together with Mathematica

Replies
2
Views
2K
Replies
0
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
1
Views
7K
  • Last Post
Replies
1
Views
1K
Replies
0
Views
2K
Replies
4
Views
9K
Replies
9
Views
7K
Top