# Evaluate Numerator and Denominator Separately

I have this function.

SetAttributes[zf,HoldAllComplete];
zf[x_] :=
If[Denominator[Unevaluated[x]] == 0,
Numerator[Unevaluated[x]],
Numerator[Unevaluated[x]]];


It is supposed to just return the numerator anytime the denominator is 0.

zf[1/0]


1

Cos[-(3*Pi)/2]


0

zf[1/Cos[-(3*Pi)/2]]


ComplexInfinity

It works fine when the denominator is literally 0, but it fails when there are functions/variables, such as when Cos[x] == 0.

How would I go about evaluating the denominator separately, then if that equals zero, drop the denominator, and evaluate the numerator only without evaluating the entire fraction?

Basically my question is how do I make zf[1/Cos[-(3*Pi)/2]] output 1?

• This looks like you're doing something mathematically dubious. Maybe you should have a look at Residue before going any further. The point is that the concept of what the numerator even means in the singular limit has to be clarified first. You can do this by taking a limit, for example of 1/Cos[-(3*Pi)/2 + z] or of 1/(Cos[-(3*Pi)/2] + z) for $z\to 0$. But more information is needed to decide what you really need. – Jens Mar 5 '13 at 7:32
• @Nasser I was wondering why all the different combinations of Hold, HoldAll, HoldForm, etcetera were not working. – mystackexchangeusername Mar 5 '13 at 16:59
• @Jens Residue is interesting but it won't work for my purposes. I just want to essentially replace denominators that are 0 with 1. Because this step is 'in the middle' it probably does seem kind of dubious. When you're working with pencil and paper it seems extremely simple, but telling the dang machine how is maddening. – mystackexchangeusername Mar 5 '13 at 17:16

## 1 Answer

Here is something that I think will work. I didn't have time to do a lot of testing, so maybe it's needs more work.

SetAttributes[zf, HoldAllComplete]

zf[expr : Times[n_, Power[d_, k_]]] /; k < 0 := If[d == 0, n, expr]
zf[expr_] := expr

zf[1/2]


1/2

zf[1/Cos[-(3*Pi)/2]]


1

zf[1/0]


1

zf[n^-2]


1/n^2

n = 0; zf[84/2/n]


42

However,

n = 0; zf[84/n/2]


ComplexInfinity

Not sure this last is acceptable.

• @m_goldberg This works for what I had in mind. Thank you. – mystackexchangeusername Mar 5 '13 at 17:25