It's been years since I last programmed in Wolfram & for the first few days, I felt like I've been feeling around the dark; now that I've mostly forgotten how to code it.

Again, I could be forgetting things, or maybe this is one of those nuisances in the language I've never had to find a workaround. What I want to do is define a simple function:

lhospital = D[Numerator[#], #2]/ D[Denominator[#], #2] &

Yet, it has problems retrieving the numerator & denominator (in different situations), since the engine would preemptively evaluate the division or fractional expression to one that would have the denominator as 1. Thereby making the expression all in the numerator & for said function above, have the denominator to evaluate to 0, invoking a division by 0 fault.

For example trying this:

lhospital[x/Sqrt[x], x]

Invokes division by zero, as x/Sqrt[x] is already at the form Sqrt[x]/1

I've tried Hold:

lhospital = 
 ReleaseHold[D[Numerator[Hold[#]], #2]/ D[Denominator[Hold[#]], #2]] &

But, it also doesn't work. I think trivially the answer is in functions like Delayed & Hold & such. I just can't remember.


So I'm using your code snippet Michael, but the function fails when, say, I enter x/Sin[x]. I get the division by zero error from I don't know where. I look at the stack trace & here's what I see:

$Message[Power::infy, 1/0]$


$\frac{1}{0} (Csc[x] - x Cot[x] Csc[x])$

$\frac{(Csc[x] - x Cot[x] Csc[x])}{0}$

I know, x/Sin[x] is a bad example, since Sin[x] does not go to infinity, so L'Hospital doesn't apply. But, why a division by zero error? Seems like an underlying flaw. Something that the Unevaluated & HoldFirst is inadequate to handle.


So here's the actual deal. I have a bunch of large equations/inequalities & expressions that needs to be treated by L'hospital in respect to ao. An example is this expression that & will share:

(-3 ao^2 (r2 r3 rb rc + 
      r1 (r4 ra rc + r3 rb rc + r3 ra (rb + rc))) rgsink vref - 
   r2 (r4 ra rb rgsink vref + r3 rb rc rgsink vth + 
      r3 ra (rb rc vth + rc rgsink vth + rb rgsink (vref + vth))) - 
   r1 (2 r3 rb rc rgsink vth + r4 rb rc rgsink vth + 
      r4 ra (rb rc vth + rc rgsink vth + rb rgsink (vref + vth)) + 
      r3 ra (2 rb rc vth + 2 rc rgsink vth + 
         rb rgsink (vref + 2 vth))) - 
   2 ao (r2 (r3 rb rc + r4 (ra + rb) rc + 
         r3 ra (rb + rc)) rgsink vref + 
      r1 (r4 (rb rc + ra (rb + rc)) rgsink vref + 
         r3 (ra rc rgsink (vref + vth) + rb rc rgsink (vref + vth) + 
            ra rb (rc vth + 
               rgsink (2 vref + vth))))))/(r2 r4 (-ra rb rgsink - 
      2 ao ra rc rgsink - 2 ao rb rc rgsink) + (1 + 
      ao) r1 r3 (-((1 + ao) ra rb rgsink) + (1 - 
         2 ao) ra rc rgsink + (1 - 2 ao) rb rc rgsink + 
      ra rb (rc + rgsink - ao rgsink)) + 
   r2 r3 (-((1 + ao) ra rb rgsink) + (1 - 2 ao) ra rc rgsink - 
      2 (-1 + ao) (1 + ao) rb rc rgsink - (1 + ao)^2 rb rc rgsink + 
      ra rb (rc + rgsink - ao rgsink)) + 
   r1 r3 ((1 + ao - ao^2) ra rc rgsink + (1 + ao - 
         ao^2) rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
         ao rgsink)) + 
   r1 r4 ((1 - 2 ao) rb rc rgsink + (1 + ao) ra (-rb rgsink - 
         2 ao rc rgsink) + 
      ra (-((-1 + ao^2) rc rgsink) + rb (rc + rgsink - ao rgsink))))

The crude first function D[Numerator[#], #2]/ D[Denominator[#], #2] & used to work, but now when apply the current function, I get back "ComplexInfinity". Then I see this on the stack trace: enter image description here

Could somebody help me out again?

  • 1
    $\begingroup$ lh = Function[, Function[{n, d}, n/d] @@ D[NumeratorDenominator[Unevaluated[#]], #2], HoldFirst]? $\endgroup$
    – Michael E2
    Aug 27, 2022 at 22:25
  • $\begingroup$ @Michael Never seen function arguments declared in the inner recursive function definition. That's new to me. I'll try it when I get back at it. $\endgroup$ Aug 27, 2022 at 23:59
  • $\begingroup$ @Nasser I'm not sure I wanna differentiate products of the numerator & reciprocals of the denominator, as technically, that is not applying L'Hospital Rule. Gets rid of the error, though. Thanks. $\endgroup$ Aug 28, 2022 at 0:02
  • $\begingroup$ I'll try it. I see now I'm tinkering with the engine. Thanks Michael, you've been helpful so far. $\endgroup$ Aug 28, 2022 at 17:43
  • $\begingroup$ I can be fine with a language that's bloated in features, as long as I can have a subset of the language that I stick to & hope the compiler/interpreter does not link into the machine code extra junk. But, Mathematica is just so inconsistent & doesn't mesh well here & there. $\endgroup$ Aug 28, 2022 at 17:47

1 Answer 1


Here's one way, using the Function[args, body, attributes] form to prevent automatic simplification of the input:

lhospital = 
  Function[{f, x}, 
    Divide @@ D[
      Block[{Power, Times, Plus}, NumeratorDenominator[f]],

lhospital[x/Sqrt[x], x]
(*  2 Sqrt[x]  *)

lhospital[x/Sin[x], x]
(* Sec[x] *)

The output auto-simplifies, though. If you don't want that, you can hold up evaluation by using Inactive@Divide in place of Divide.

Update in response to Holds, Defers, Unevaluated & Inactive and Preventing the auto simplification of fractional expression by the engine, but posting here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.