# Fastest form of a function

I'm trying to find the fastest form for a function. I have a few approaches with test results.The function I'm working with is the 3rd derivative of the incomplete beta function with respect to its first argument. The second two arguments will always be greater than 1. All arguments are reals.

fun1[x_, alpha_, beta_] = Derivative[3][(Beta[#, alpha, beta] &)][x];


Then I see if FullSimplify will generate a faster form.

fun2[x_, alpha_, beta_] =
FullSimplify[
Derivative[3][(Beta[#, alpha, beta] &)][x]
];


Next, I try using a pure function.

fun3 = Function[{x, alpha, beta},
Derivative[3][(Beta[#, alpha, beta] &)][x]
];


And then I try using FullSimplify and a pure function

fun4 = Function[{x, alpha, beta},
Evaluate[
FullSimplify[
Derivative[3][(Beta[#, alpha, beta] &)][x]
]]];


My tests show fun2, the FullSimplify (but not pure function) approach as the fastest.

testData = {RandomReal[{0, 1}, 50], RandomInteger[{1, 30}, 5],
RandomInteger[{1, 30}, 5]}
Part[RepeatedTiming[Outer[#, Sequence @@ testData]] & /@ {fun1, fun2,
fun3, fun4}, All, 1]
(*{0.0172, 0.00875, 0.0174, 0.010}*)


Without restricting results to machine precision (e.g., Compile), are any other methods faster?

• Even more straightforward is Derivative[3, 0, 0][Beta][x, alpha, beta]. See this as well. – J. M. is away Aug 12 '17 at 3:02

As I noted in the comments, one can use Derivative[] to take the partial derivative of a multi-argument function; thus, for the incomplete beta function, you can do this:

fun1[x_, alpha_, beta_] = Derivative[3, 0, 0][Beta][x, alpha, beta]


Alternatively, you can use one of a number of explicit formulae listed in the Wolfram Special Functions website; in particular, using the formula here, we have

With[{n = 3},
(-1)^(n - 1) (1 - z)^(b - n) z^(a - 1) Gamma[b]
Hypergeometric2F1Regularized[1 - a, 1 - n, 1 + b - n, 1 - 1/z] // FullSimplify]
(1 - z)^(-3 + b) z^(-3 + a) (2 - 3 a + a^2 - 2 (-1 + a) (-3 + a + b) z +
(-3 + a + b) (-2 + a + b) z^2)

• That form of derivative is much cleaner. Thanks for the tip. – FalafelPita Aug 17 '17 at 19:34