Group Abstract Group Abstract

Message Boards Message Boards

0
|
957 Views
|
9 Replies
|
8 Total Likes
View groups...
Share
Share this post:

How to divide numerator and denominator by the numerator?

Posted 2 months ago

How to divide numerator and denominator by the numerator?

in = -((8 m^2)/(3 + 10 m^2 + 3 m^4))
Numerator[in]
in /. a_/b_ -> a/Numerator[in]/(b/Numerator[in])

enter image description here

Why is there no change in the result when using the code above?

I aim to derive this expression in fractional form via identical transformation:

-(1/(5/4 + 3/(8 m^2) + (3 m^2)/8))

enter image description here

POSTED BY: Bill Blair
9 Replies

Try 1/Expand[1/frac].

On more general approaches:

In Mathematica, an expression (a b)/(a c) will auto-simplify to b/c, and there's not much that can be done about common factors that are factored out except to work around it in some way, such as HoldForm, Defer, Inactive or as in the OP, boxes. These are used to keep the division that leads to the auto-simplification from evaluating. Another way is to manipulate a b or a c so that a is not factored out. Common things to try are Expand[a b] and Simplify[a b]. Sometimes there is no way to avoid using the first kind of workaround, such as when the desired output is 1/(1/m).

Here's a step-by-step breakdown of how to multiply the top and bottom of a fraction by the same factor:

frac = -((8 m^2)/(3 + 10 m^2 + 3 m^4));

sgn = Simplify[Sign[frac], m \[Element] Reals && m != 0]; (* wrestle Mma over the sign *)
fac = sgn/Numerator[frac]; (* construct factor for multiplying *)
newnd = fac*NumeratorDenominator[frac]; (* mult. num. & den. by fac *)
newnd = Expand /@ newnd; (* un-factor the product(s) *)
Divide @@ newnd (* construct new fraction *)
(*  -(1/(5/4 + 3/(8 m^2) + (3 m^2)/8))  *)

Here's a quick way that goes along with the default treatment of signs:

1/Expand[Denominator[frac]/Numerator[frac]]
(*  1/(-(5/4) - 3/(8 m^2) - (3 m^2)/8)  *)

Note that Sign[] on an algebraic expression is usually difficult to resolve to a 1 or -1. The following, using an undocumented function, always resolves to 1 or -1 and often agrees with the user's idea of what the leading sign is:

sgn = (-1)^Boole[Internal`SyntacticNegativeQ[frac]]
(*  -1  *)
POSTED BY: Michael Rogers
Posted 2 months ago

Thank you for the excellent solution, Michael! Your 1/Expand[1/frac] approach is really elegant. I have a follow-up question: I modified your step-by-step method to divide both numerator and denominator by the parameter part of the numerator (excluding the sign and coefficient), and I came up with the following code:

sgn = Simplify[
  Sign[frac], (var = Variables[Numerator@frac][[1]]) \[Element] 
    Reals && var != 0](*wrestle Mma over the sign*)
Sign@(Cases[Numerator[frac] , _?NumericQ, Infinity][[1]])
fac = sgn/((Numerator[
       frac]/((Sign@(Cases[Numerator[frac] , _?NumericQ, 
             Infinity][[1]]))*(Cases[Numerator[frac] , _?NumericQ, 
           Infinity][[1]]))) // 
    Expand)(*construct factor for multiplying*)
newnd = fac*NumeratorDenominator[frac]; (*mult.num.& den.by fac*)
newnd = Expand /@ newnd; (*un-factor the product(s)*)
Divide @@ (newnd // Expand )(*construct new fraction*)

It works, but the code feels quite verbose and the nesting is a bit hard to follow. I was wondering if there's a more concise or optimized way to achieve the same result.

POSTED BY: Bill Blair

I'm not sure of the range of cases for the fraction that you want to deal with. For instance:

  • Is the numerator always a monomial?
  • Does the numerator ever have more than one numeric factor?
  • Is there ever more than one parameter or variable?

I'm guessing the answers are "yes," "no", and "no," respectively. But the second one is tricky. I don't think the code works, according to my understanding of what is desired, on the following fractions:

frac = -((Sqrt[8] m^2)/(3 + 10 m^2 + 3 m^4))
frac = -((Sqrt[2] m^2)/(3 + 10 m^2 + 3 m^4))
frac = -(((1 + Sqrt[2]) m^2)/(3 + 10 m^2 + 3 m^4)) (* a real disaster! :) *)

These have more than one numeric factor (inspect the FullForm) in the numerator, and your (Cases…)[[1]] code grabs only the first. Perhaps the coefficients are always single numbers, maybe always integers even, and not arbitrary numeric expressions.

Here is another way to separate the coefficient of a monomial numerator:

(* change fraction as desired. I picked a complicated one to show robustness *)
num = Numerator[-((Sqrt[8] m^2 p^3)/(3 + 10 m^2 + 3 m^4))]; 
vars = Variables[num]; (* change vars to {m} to include p in the coefficient *)

{coeff, fac} = Replace[
  CoefficientRules[num, vars],
  {{pow_ -> coeff_} :> {coeff, Times @@ (vars^pow)},
   _ :> (* should really pick another action :) *)
    "Oh, no, num is not a monomial!" }
  ]
(*  {-2 Sqrt[2], m^2 p^3}  *)

Another alternative, which separates the numeric factors from the rest of the numerator:

{coeff, fac} = Times @@@ Lookup[
   GroupBy[Power @@@ FactorList[num], NumericQ],
   {True, False}, 
   {}]
(*  {-2 Sqrt[2], m^2 p^3}  *)

A tip for a slightly neater code: You call NumeratorDenominator[] and Numerator[] several times. One could save the result in variables with one call and use the variables in the code:

{num, den} = NumeratorDenominator[frac];

If you want to test whether an expression is a rational function with integer coefficients, one can use this:

AllTrue[
 NumeratorDenominator@Together@expr, 
 PolynomialExpressionQ[#, {m}, IntegerQ] &]

We don't really need to use Together@in or Together@frac above, because those fractions are already "together."

POSTED BY: Michael Rogers
Posted 2 months ago

Thank you so much for the thorough analysis and the excellent suggestions! You are absolutely right — my original Cases[..., _?NumericQ, Infinity][[1]] approach is fragile and breaks down as soon as the coefficient involves anything more complicated than a single integer.

POSTED BY: Bill Blair

A couple of ways:

in = -((8 m^2)/(3 + 10 m^2 + 3 m^4));
in /. a_/b_ :>
  -DisplayForm[FractionBox[a/Numerator[in],
     Expand[-b/Numerator[in]]]]
in /. a_/b_ :>
  -Inactive[Divide][a/Numerator[in],
    -Expand[b/Numerator[in]]]
POSTED BY: Gianluca Gorni
Posted 2 months ago

Thank you, but this code does not produce the desired result when the leading sign is changed. The code has certain limitations.

POSTED BY: Bill Blair

This is one way to do it, but we must strip the formatting away if we need to reuse the result in further calculations:

in = -((8 m^2)/(3 + 10 m^2 + 3 m^4))
Numerator[in]
newForm = 
 in /. a_/b_ -> 
   DisplayForm[FractionBox[a/Numerator[in], (b/Numerator[in])]]
3*newForm
3*newForm /. {FractionBox -> Divide, 
   DisplayForm -> Identity} // Expand
POSTED BY: Gianluca Gorni
Posted 2 months ago

I aim to derive this expression in fractional form via identical transformation:

-(1/(5/4 + 3/(8 m^2) + (3 m^2)/8))
POSTED BY: Bill Blair
Posted 2 months ago

I think it's equivalent to dividing by 1, which Mathematica simplifies:

(n/n)/(d/n) -> 1/(d/n) -> n/d

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard