Is there a way to speed up high order differentiation of a function in R?

  automatic-differentiation, c++, differentiation, r

I have a function in its symbolic form. And R has the package Deriv which works well for what I am looking for but its painfully slow. CppAD in c++ does the automatic differentiation but I am not sure if there is a way it would work with higher order derivatives. This is an example of what I mean

f <- function(x1,x2,x3,m,n,q){
  
  log( (((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*exp(x1) + 
           ((1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*exp(x2) + 
           ((1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*exp(x3) + 
           ((1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*exp(x1+x3) + 
           ((m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*exp(x2+x3) + 
           (((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) / ( ((1-m)*(1-n)/(1-q))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (1 - m - ((1-m)*(1-n)/(1-q)))*(1 - n - ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + (m + n - q - 1 + ((1-m)*(1-n)/(1-q)))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) + ((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q))*((1-m)*(1-n)/(1-q)) ))*1 )
  
}

what I would be looking for is something like this

Deriv(f, c("x1","x2","x3"), nderiv = 4)

or even nderiv = 3 but it really slow. Is there a way I would use CppAD in c++ and connect it to R or is there any other way I would do these derivatives without going through the longer wait?

Source: Windows Questions C++

LEAVE A COMMENT