Skip to main content
added 470 characters in body
Source Link
Jason B.
  • 70.2k
  • 3
  • 144
  • 298

Here is a more robust version of what I was doing before. ConsiderConsider your expression,

ThisBut as OP points out, this will break up a term intonot combine terms where the largest number of fractionsdenominator is the same except for a constant factor, without repeating denominatorslike a/(2*b) and c/b. Here it is This function should be able to simplify an rational expression like desired in functional formthe OP (if you come up with a way to break the function, please let me know).

fractionExpand[expr_] :=
  Replace[Expand@expr, 
  expr2_Plus :> (Together@*Plus @@@ 
      GatherBy[List @@ Expand[expr]expr2, Denominator]Variables@*Denominator] // 
  Total)]

Here is anotherThis will break up a term into the largest number of fractions, longer testwithout repeating denominators. Here are a few tests,

expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
   a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together
(* (a b + a c + b c + c d + c e + c f g + a h + a i + a j k + l + a c m + 

enter image description here

fractionExpand@expr2
 b c n

enter image description here

You get back the original input.

expr3 += a b o + p q)/(a2 b c) *)

fractionExpand@expr2
(* (d + e + f g)c/(a b) + (h + i + j k)d/(b c) + (1e + m)f/b + (
 1g + nz)/a + (1a +- oc)/c + (l + p q)/(am b c) *)// Together

You get back the original input.enter image description here

fractionExpand@expr3

enter image description here

Here is a more robust version of what I was doing before. Consider your expression,

This will break up a term into the largest number of fractions, without repeating denominators. Here it is in functional form,

fractionExpand[expr_] := 
 Together@*Plus @@@ GatherBy[List @@ Expand[expr], Denominator] // 
  Total

Here is another, longer test,

expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
   a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together
(* (a b + a c + b c + c d + c e + c f g + a h + a i + a j k + l + a c m + 
 b c n + a b o + p q)/(a b c) *)

fractionExpand@expr2
(* (d + e + f g)/(a b) + (h + i + j k)/(b c) + (1 + m)/b + (
 1 + n)/a + (1 + o)/c + (l + p q)/(a b c) *)

You get back the original input.

Consider your expression,

But as OP points out, this will not combine terms where the denominator is the same except for a constant factor, like a/(2*b) and c/b. This function should be able to simplify an rational expression like desired in the OP (if you come up with a way to break the function, please let me know).

fractionExpand[expr_] :=
  Replace[Expand@expr, 
  expr2_Plus :> (Together@*Plus @@@ 
      GatherBy[List @@ expr2, Variables@*Denominator] // Total)]

This will break up a term into the largest number of fractions, without repeating denominators. Here are a few tests,

expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
   a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together

enter image description here

fractionExpand@expr2

enter image description here

You get back the original input.

expr3 = a/(2 b) + c/b + d/e + f/(g + z) + (a - c)/(l m b) // Together

enter image description here

fractionExpand@expr3

enter image description here

added 944 characters in body
Source Link
Jason B.
  • 70.2k
  • 3
  • 144
  • 298

Here is a more robust version of what I was doing before. Consider your expression,

expr = (a + m a + b + n b + c + k c + d + e)/(a b);

This gets us almost where we want to go,

Expand@expr
vars(* =1/a {+ 1/b + c/(a, b};) + d/(a b) + e/(a b) + (c k)/(
 a b) + m/b + n/a *)

Haven't testedBut we have too many terms with the versatilitysame denominator, so we can use GatherBy to group them, then simplify the sums of this method yetterms with the same denominator, then sum it all back up,

Fold[ListTogether@*Plus @@@ GatherBy[List @@ Apart[#1Expand[expr], #2]Denominator] &,// exprTotal
(* (c + d + e + c k)/(a b) + (1 + m)/b + (1 + n)/a *)

This will break up a term into the largest number of fractions, without repeating denominators. Here it is in functional form,

fractionExpand[expr_] := 
 Together@*Plus @@@ GatherBy[List @@ Expand[expr], vars]Denominator] // 
  Total

Here is another, longer test,

expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
   a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together
(* (a b + a c + b c + c d + c e + c f g + a h + a i + a j k + l + a c m + 
 b c n + a b o + p q)/(a b c) *)

fractionExpand@expr2
(* (d + e + f g)/(a b) + (h + i + j k)/(b c) + (1 + m)/b + (
 1 + n)/a + (1 + o)/c + (l + p q)/(a b c) *)

You get back the original input.

expr = (a + m a + b + n b + c + k c + d + e)/(a b);

vars = {a, b};

Haven't tested the versatility of this method yet,

Fold[List @@ Apart[#1, #2] &, expr, vars] // Total
(* (c + d + e + c k)/(a b) + (1 + m)/b + (1 + n)/a *)

Here is a more robust version of what I was doing before. Consider your expression,

expr = (a + m a + b + n b + c + k c + d + e)/(a b);

This gets us almost where we want to go,

Expand@expr
(* 1/a + 1/b + c/(a b) + d/(a b) + e/(a b) + (c k)/(
 a b) + m/b + n/a *)

But we have too many terms with the same denominator, so we can use GatherBy to group them, then simplify the sums of terms with the same denominator, then sum it all back up,

Together@*Plus @@@ GatherBy[List @@ Expand[expr], Denominator] // Total
(* (c + d + e + c k)/(a b) + (1 + m)/b + (1 + n)/a *)

This will break up a term into the largest number of fractions, without repeating denominators. Here it is in functional form,

fractionExpand[expr_] := 
 Together@*Plus @@@ GatherBy[List @@ Expand[expr], Denominator] // 
  Total

Here is another, longer test,

expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
   a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together
(* (a b + a c + b c + c d + c e + c f g + a h + a i + a j k + l + a c m + 
 b c n + a b o + p q)/(a b c) *)

fractionExpand@expr2
(* (d + e + f g)/(a b) + (h + i + j k)/(b c) + (1 + m)/b + (
 1 + n)/a + (1 + o)/c + (l + p q)/(a b c) *)

You get back the original input.

Source Link
Jason B.
  • 70.2k
  • 3
  • 144
  • 298

expr = (a + m a + b + n b + c + k c + d + e)/(a b);

vars = {a, b};

Haven't tested the versatility of this method yet,

Fold[List @@ Apart[#1, #2] &, expr, vars] // Total
(* (c + d + e + c k)/(a b) + (1 + m)/b + (1 + n)/a *)