rxFun: Add user function to RxODE

View source: R/symengine.R

rxFunR Documentation

Add user function to RxODE

Description

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()

Usage

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(s)

Matthew L. Fidler

Examples


## 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)"))

## 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

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

rxToSE("fun(a,b,c)")

## And will take a central difference when calculating derivatives

rxFromSE("Derivative(fun(a,b,c),a)")

## 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)")

# You can also remove the functions by `rxRmFun`

rxRmFun("fun")


RxODE documentation built on March 23, 2022, 9:06 a.m.