Skip to content

Commit fe17a49

Browse files
committed
added rule for numD(f(x) ~ x + x + x)
1 parent 8fd83c2 commit fe17a49

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

R/numD.R

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#'
1212
#' @return a function implementing the derivative as a finite-difference approximation.
1313
#' This has a second argument, `.h`, that allow the finite-difference to be set when evaluating
14-
#' the function. The default values are set for reasonable numerical precision.
14+
#' the function. The default values are set for reasonable numerical precision
15+
#' with the pattern-book functions.
1516
#'
1617
#' @details
1718
#' Uses a simple finite-difference scheme to evaluate the derivative. The function created
@@ -64,11 +65,15 @@ numD <- function(tilde, ..., .h=NULL) {
6465
the_h <- ifelse(is.null(.h), 0.000001, .h)
6566
res = make_dfdx( f, dvars[1], .h = the_h) %>%
6667
bind_params(formals(f))
67-
}else if (length(dvars==2) && dvars[1]==dvars[2]) {
68+
} else if (length(dvars)==2 && dvars[1]==dvars[2]) {
6869
# Second unmixed partial
6970
the_h <- ifelse(is.null(.h), 0.0001, .h)
7071
res = make_d2fd2x( f, dvars[1], .h = the_h) %>%
7172
bind_params(formals(f))
73+
} else if (length(dvars)==3 && length(unique(dvars)) == 1) {
74+
the_h = ifelse(is.null(.h), 0.01, .h)
75+
res = make_d3fd3x( f, dvars[1], .h = the_h) %>%
76+
bind_params(formals(f))
7277
} else if (length(dvars)==2) {
7378
# mixed partial
7479
the_h <- ifelse(is.null(.h), 0.001, .h)
@@ -143,3 +148,27 @@ make_d2fd2x <- function(f, .wrt, .h = 0.000001) {
143148

144149
conventional_argument_order(dfun, ".h")
145150
}
151+
152+
# ==========
153+
#
154+
# @note Helper function for third-order deriv in one variable.
155+
156+
make_d3fd3x <- function(f, .wrt, .h = 0.0001) {
157+
far_right_args <- far_left_args <- right_args <- left_args <- args <- names(formals(f))
158+
args <- paste0(args, collapse=", ")
159+
right_args[right_args==.wrt] <- glue::glue("{.wrt} + .h")
160+
right_args <- paste0(right_args, collapse=", ")
161+
left_args[left_args==.wrt] <- glue::glue("{.wrt} - .h")
162+
left_args <- paste0(left_args, collapse=", ")
163+
far_right_args[far_right_args==.wrt] <- glue::glue("{.wrt} + 2*.h")
164+
far_right_args <- paste0(far_right_args, collapse=", ")
165+
far_left_args[far_left_args==.wrt] <- glue::glue("{.wrt} - 2*.h")
166+
far_left_args <- paste0(far_left_args, collapse=", ")
167+
command <- glue::glue(
168+
"function({.wrt}){{(f({far_right_args}) - 2*(f({right_args}) - f({left_args})) - f({far_left_args}))/(2*.h^3)}}")
169+
dfun <- eval(parse(text=command))
170+
formals(dfun) <- c(formals(f), list(.h=.h))
171+
172+
conventional_argument_order(dfun, ".h")
173+
}
174+

man/numD.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)