R 中的显式公式与符号导数
我想评估R中某些函数f的高阶导数。 我有两种可能性。
- 要么我确定 f(k) 的通用表达式,即 f 的 k 阶导数(我在我的具体情况下可以做),然后我评估它;
- 或者我利用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.
- 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;
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
符号微分比手工微分更不易出错。
对于低阶,我不认为符号微分会花费太多计算机时间,但您可以轻松地计算您的具体情况,以确定它正在使用proc.time, system.time 或 rbenchmark 包。另请参阅这些示例。
您可能想尝试符号微分和手微分作为检查。
关于 R 的 deriv (以及相关函数例如
D
)与 Ryacas 包相比,后者能够重复执行差异化而不要求用户迭代它们自己(deriv
的第三个参数指定顺序),它有一个Simplify
函数,R 中不存在对应的函数,尽管 Ryacas 应该仔细检查,因为 yacas 可能有点有时有越野车。这是一个示例:
编辑:添加到示例:
另请尝试
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 ofderiv
specifies the order) and it has aSimplify
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:
EDIT: Added to example:
Also try
demo("Ryacas-Function")
.EDIT 2: Fixed an error and added more to the example.