I have the following function which is expressed through the numerical integration

F1[\[Nu]_?NumericQ, x_?NumericQ] := 
 Exp[-x/2]/\[Pi] NIntegrate[
   Exp[- (x/2) Cos[t]] Cos[\[Nu]*t], {t, 0, \[Pi]}]

If the first argument is an integer the integral is exressed through the Bessel function:

F1[\[Nu]_?IntegerQ, x_] := (-1)^\[Nu] E^(-x/2) BesselI[\[Nu], x/2]


F1[1, x^2]


-E^(-(x^2/2)) BesselI[1, x^2/2]


F1[1., x^2]

is returned unevaluated:

F1[1., x^2]

How to define F1 so that it would return (-1)^\[Nu] E^(-x/2) BesselI[\[Nu], x/2] if \[Nu] is close to an integer within given accuracy? One solution is evident:

F1[\[Nu]_ /; Abs[Round[\[Nu]] - \[Nu]] < 10^-10, x_] := 
 Cos[\[Pi] \[Nu]] E^(-x/2) BesselI[Round[\[Nu]], x/2]

Is there a better solution?

By the way, the integral can be expressed through the hypergeometric function,

F3[\[Nu]_, x_] := 
 HypergeometricPFQRegularized[{1, 1/2}, {1 - \[Nu], 1 + \[Nu]}, -x]

but Mathematica don't know that.

My question is motivated by the fact that the hypergeometric function for big arguments is evaluated more slow than numerical integration, and numerical integration is slow for integer \[Nu].


1 Answer 1


You could simply put the following before the F1[ν_?NumericQ, ...] definition:

epsilon = 10^-6  (* Replace with whatever tolerance you'd like to use *)

F1[ν_, x_] /; ! IntegerQ@ν && Abs[ν - Round@ν] < epsilon := F1[Round@ν, x]

You might want to make sure to clear out old definitions with ClearAll[F1] first.

Edit -- Added absolute value, which OP's edit reminded me of.

  • $\begingroup$ @Igor Ok, this is obviously fairly similar to your edit, which I didn't see before originally posting. $\endgroup$
    – jjc385
    Commented Oct 20, 2017 at 8:52

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