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.
UPDATE:
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}$
$\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.
UPDATE2:
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:
Could somebody help me out again?
lh = Function[, Function[{n, d}, n/d] @@ D[NumeratorDenominator[Unevaluated[#]], #2], HoldFirst]
? $\endgroup$