Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的类表达式。其中元素的使用_R_Expression_Derivative - Fatal编程技术网

R中的类表达式。其中元素的使用

R中的类表达式。其中元素的使用,r,expression,derivative,R,Expression,Derivative,我正在使用Ryacas包的deriv函数。 输出是一个表达式。 我想使用表达式的一部分作为函数,这样我就可以尝试它的值 这是一个例子 s <- expression(2*y*x + 2*y -1*x^2-2*y^2); a<-deriv(s,c("x","y")); 但是输出是一个类调用 (.grad[, "x"] <- .expr1 - .expr10)() (.grad[,“x”]函数deriv返回一个表达式,您可以通过body函数将其设置为函数体。例如: #

我正在使用Ryacas包的deriv函数。 输出是一个表达式。 我想使用表达式的一部分作为函数,这样我就可以尝试它的值

这是一个例子

s <- expression(2*y*x + 2*y -1*x^2-2*y^2);
a<-deriv(s,c("x","y"));
但是输出是一个类调用

(.grad[, "x"] <- .expr1 - .expr10)()

(.grad[,“x”]函数
deriv
返回一个
表达式
,您可以通过
body
函数将其设置为函数体。例如:

     #declare an empty function with the x and y arguments
     myfun<-function(x,y) {}
     #set the body of the function as the object returned by deriv
     body(myfun)<-a
     #call myfun
     myfun(2,3)
     #[1] -4
     #attr(,"gradient")
     #     x  y
     #[1,] 2 -6
#声明一个带有x和y参数的空函数

MyFun问题中的示例根本不使用Ryacas,即使Ryacas已加载,因为
x
y
未定义为Ryacas符号。它在R本身中使用
deriv
函数。请尝试
demo(“Ryacas函数”)
了解如何使用Ryacas进行此操作的示例。还可以尝试
vignette(“Ryacas”)
了解更多信息。谢谢!很抱歉出现此错误。谢谢!如果我想找到使函数最小化的值。无需使用具有不同值的for循环。可以对函数进行右键和反转?请参阅
?optim
函数以搜索最小值或最大值。
(.grad[, "x"] <- .expr1 - .expr10)()
     #declare an empty function with the x and y arguments
     myfun<-function(x,y) {}
     #set the body of the function as the object returned by deriv
     body(myfun)<-a
     #call myfun
     myfun(2,3)
     #[1] -4
     #attr(,"gradient")
     #     x  y
     #[1,] 2 -6