R 中的显式公式与符号导数

发布于 2024-12-25 17:02:04 字数 339 浏览 2 评论 0原文

我想评估R中某些函数f的高阶导数。 我有两种可能性。

  1. 要么我确定 f(k) 的通用表达式,即 fk 阶导数(我在我的具体情况下可以做),然后我评估它;
  2. 或者我利用R的能力来执行符号导数(函数D())。

1比2有什么优点?假设 f(k) 不是递归公式。如果 f(k) 是递归的怎么办?

任何提示将不胜感激。

I would like to evaluate higher order derivatives of some function f in R.
Two possibilities are available to me.

  1. Either I determine a general expression for f(k), the k-th derivative of f (which I can do in my particular case), and then I evaluate it;
  2. Or I take advantage of the capacities of R to perform symbolic derivative (function D()).

What are the advantages of 1 over 2? Let us say that f(k) is not a recursive formula. What if f(k) is recursive?

Any hint will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么敢 2025-01-01 17:02:04

符号微分比手工微分更不易出错。

对于低阶,我不认为符号微分会花费太多计算机时间,但您可以轻松地计算您的具体情况,以确定它正在使用proc.time, system.timerbenchmark 包。另请参阅这些示例

您可能想尝试符号微分和手微分作为检查。

关于 R 的 deriv (以及相关函数例如 D)与 Ryacas 包相比,后者能够重复执行差异化而不要求用户迭代它们自己(deriv 的第三个参数指定顺序),它有一个 Simplify 函数,R 中不存在对应的函数,尽管 Ryacas 应该仔细检查,因为 yacas 可能有点有时有越野车。

这是一个示例:

> library(Ryacas)
> x <- Sym("x")
> y <- (x^2+x)^2
> dy <- deriv(y, x, 2) # 2nd deriv
> dy
expression(2 * (2 * x + 1)^2 + 4 * (x^2 + x))
> Simplify(dy)
expression(2 * (6 * x^2 + 6 * x + 1))

编辑:添加到示例:

> Eval(dy, list(x = 3))
[1] 146
> Eval(Simplify(dy), list(x = 3))
[1] 146
>
> f <- function(x) {}
> body(f) <- yacas(Simplify(dy))[[1]]
> f
function (x) 
2 * (6 * x^2 + 6 * x + 1)
> f(3)
[1] 146
>
> # double check
> w <- 3
> eval(D( D(expression((w^2+w)^2), "w"), "w"))
[1] 146

另请尝试 demo("Ryacas-Function")

编辑 2:修复了一个错误并向示例添加了更多内容。

Symbolic differentiation is less error prone than doing it by hand.

For low orders I would not think that symbolic differentiation would take much computer time but you can readily time your specific situation to determine what it is using proc.time, system.time or the rbenchmark package. Also see these examples.

You might want to try both symbolic and hand differentiation as a check.

Regarding R's deriv (and associated functions such as D) vs. the Ryacas package, the latter has the facility to do repeated differentiation without requiring that the user iterate themselves (third arg of deriv specifies the order) and it has a Simplify function for which no counterpart exists in R although Ryacas should be carefully checked since yacas can be a bit buggy at times.

Here is an example:

> library(Ryacas)
> x <- Sym("x")
> y <- (x^2+x)^2
> dy <- deriv(y, x, 2) # 2nd deriv
> dy
expression(2 * (2 * x + 1)^2 + 4 * (x^2 + x))
> Simplify(dy)
expression(2 * (6 * x^2 + 6 * x + 1))

EDIT: Added to example:

> Eval(dy, list(x = 3))
[1] 146
> Eval(Simplify(dy), list(x = 3))
[1] 146
>
> f <- function(x) {}
> body(f) <- yacas(Simplify(dy))[[1]]
> f
function (x) 
2 * (6 * x^2 + 6 * x + 1)
> f(3)
[1] 146
>
> # double check
> w <- 3
> eval(D( D(expression((w^2+w)^2), "w"), "w"))
[1] 146

Also try demo("Ryacas-Function") .

EDIT 2: Fixed an error and added more to the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文