# What is the best way to redefine a function that has type ambiguity 0/0 at an isolated point?

Consider the following Green Function of wire turn producing magnetic field:

Br[R_, Z_, {r_, z_}]= ((z - Z) (-(((r^2 + R^2 + (z - Z)^2) EllipticE[-((
4 r R)/((r - R)^2 + (z - Z)^2))])/(
r^2 + 2 r R + R^2 + (z - Z)^2)) +
EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r -
R)^2 + (z - Z)^2])


It is evaluated to Indeterminate at R=0. However Limit[Br[R,Z,{r,z}],R->0,Direction->"FromAbove"] gives 0 as it should be.

What is the best way to modify/append the definition of Br[R, Z, {r, z}] to avoid this issue. Simple

Br[0, Z_, {r_, z_}]=0;
Br[0., Z_, {r_, z_}]=0;


does not always work, e.g., in case of Br[R, Z, {r, z}]/.R->0.

In similar cases I also tryied

Br[R_, Z_, {r_, z_}]/;R>0 = ...
Br[0, Z_, {r_, z_}] = 0;
Br[0., Z_, {r_, z_}] = 0;


But I am not sure that this is recommended soluttion.

Clear[Br]
Br[R_ /; R == 0, Z_, {r_, z_}] = 0
Br[R_ /; R != 0, Z_, {r_, z_}] =
Abs[Sign[R]] ((z -
Z) (-(((r^2 +
R^2 + (z -
Z)^2) EllipticE[-((4 r R)/((r - R)^2 + (z -
Z)^2))])/(r^2 + 2 r R + R^2 + (z - Z)^2)) +
EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r -
R)^2 + (z - Z)^2])

Br[0, 3, {1/2, 2}]
Br[0., 3, {1/2, 2}]
Br[R, 3, {1/2, 2}] /. R -> 0
Br[R, 3, {1/2, 2}] /. R -> 0.

(* 0 *)
(* 0 *)
(* 0 *)
(* 0 *)


Try an IF statement like:

Br[R_, Z_, {r_, z_}] =
If[R == 0,
0, ((z -
Z) (-(((r^2 +
R^2 + (z -
Z)^2) EllipticE[-((4 r R)/((r - R)^2 + (z -
Z)^2))])/(r^2 + 2 r R + R^2 + (z - Z)^2)) +
EllipticK[-((4 r R)/((r - R)^2 + (z - Z)^2))]))/(R Sqrt[(r -
R)^2 + (z - Z)^2])]

Br[R, Z, {r, z}] /. R -> 0

0

• Not sure, but If can be slower than /; . Commented Sep 2, 2023 at 7:06

I'd try a series expansion at $$R=0$$ combined with Piecewise:

BBr[R_, Z_, {r_, z_}] =
Piecewise[{{Series[Br[R, Z, {r, z}], {R, 0, 1}] // FullSimplify //
Normal, R < 10^-12}},
Br[R, Z, {r, z}]]


Maybe the condition on $$R$$ should depend on the other parameters and not be picked absolutely: for example, R < 10^-12 * Z.

• Once I tested various methods. Piecewise was the slowest solution if I remember right. Commented Sep 2, 2023 at 7:47
• My main point here was the use of a series expansion instead of only plugging the hole at $R=0$. You can combine this with If or with pattern matching or with a Dispatch however you prefer. Commented Sep 2, 2023 at 8:02
• It really depends on your use case. If is a logical clause and may get evaluated too early; Piecewise is a mathematical clause and will evaluate much later. Commented Sep 2, 2023 at 10:24