从派生命令中提取渐变 [英] Extract the gradient from the deriv command

查看:77
本文介绍了从派生命令中提取渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,一位同事问我以下问题.在下面的代码中,如何提取渐变:

A colleague asked me the following question the other day. In the following piece of code, how do you extract the gradient:

> x=5
> a = eval(deriv(~ x^3, "x"))
> a
[1] 125
attr(,"gradient")
      x
[1,] 75

我的回答是

>  attr(a, "gradient")[1]
[1] 75

这个语法对我来说似乎很笨拙.有没有更好的提取梯度的方法?

This syntax seems clunky to me. Is there a better way of extracting the gradient?

推荐答案

不确定这些算作更好,但是:

Not sure these count as better, but:

with(attributes(a), gradient)

attributes(a)$gradient

是将属性返回为可供选择的列表的替代方法.

are alternatives that return the attributes as a list from which to select.

这篇关于从派生命令中提取渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆