This adds a user function to RxODE that can be called. If needed, these functions can be differentiated by numerical differences or by adding the derivatives to RxODE's internal derivative table with rxD()

rxFun(name, args, cCode)

rxRmFun(name)

Arguments

name

This gives the name of the user function

args

This gives the arguments of the user function

cCode

This is the C-code for the new function

Value

nothing

Author

Matthew L. Fidler

Examples

# \donttest{
## Right now RxODE is not aware of the function f
## Therefore it cannot translate it to symengine or
## Compile a model with it.

try(RxODE("a=fun(a,b,c)"))
#>  
#> Error in rxModelVars_(obj) : Evaluation error: syntax errors (see above).

## Note for this approach to work, it cannot interfere with C
## function names or reserved RxODE specical terms.  Therefore
## f(x) would not work since f is an alias for bioaviability.

fun <- "
double fun(double a, double b, double c) {
  return a*a+b*a+c;
}
" ## C-code for function

rxFun("fun", c("a", "b", "c"), fun) ## Added function
#> Error: .onLoad failed in loadNamespace() for 'symengine', details:
#>   call: dyn.load(file, DLLpath = DLLpath, ...)
#>   error: unable to load shared object '/home/runner/work/_temp/Library/symengine/libs/symengine.so':
#>   libmpfr.so.6: cannot open shared object file: No such file or directory

## Now RxODE knows how to translate this function to symengine

rxToSE("fun(a,b,c)")
#> Error: package "symengine" needed for this function to work

## And will take a central difference when calculating derivatives

rxFromSE("Derivative(fun(a,b,c),a)")
#> Error: package "symengine" needed for this function to work

## Of course, you could specify the derivative table manually
rxD("fun", list(
  function(a, b, c) {
    paste0("2*", a, "+", b)
  },
  function(a, b, c) {
    return(a)
  },
  function(a, b, c) {
    return("0.0")
  }
))

rxFromSE("Derivative(fun(a,b,c),a)")
#> Error: package "symengine" needed for this function to work

# You can also remove the functions by `rxRmFun`

rxRmFun("fun")
# }