It is with great pleasure that I can announce that Ryacas
version 1.1.0
has now been accepted into Journal of Open Source Software and
same version released to CRAN.
(The source code is available at https://github.com/mikldk/ryacas/.)
I already wrote about Ryacas
many times before.
I will refer you to the “Getting started” and “The high-level (symbol) interface” vignettes or one of the others
available at the CRAN page or the package’s website.
It means that you can install the package by (possible after binaries have been built):
install.packages("Ryacas")
Followed by:
library(Ryacas)
A small demo case (with a Rosenbrock function):
x <- yac_symbol("x")
y <- yac_symbol("y")
f <- 100 * (y - x^2)^2 + (1 - x)^2
f # also yac_symbol
## [1] 100*(y-x^2)^2+(1-x)^2
as_r(f) # as R expression
## expression(100 * (y - x^2)^2 + (1 - x)^2)
We can now find the gradient:
g <- deriv(f, c("x", "y"))
g # yac_symbol
## [1] {(-400)*x*(y-x^2)-2*(1-x),200*(y-x^2)}
as_r(g) # as R expression
## expression(c(-400 * x * (y - x^2) - 2 * (1 - x), 200 * (y - x^2)))
eval(as_r(g), list(x = 3, y = 2)) # get value for a point
## [1] 8404 -1400
Or the Hessian:
H <- Hessian(f, c("x", "y"))
H # yac_symbol
## {{(-400)*(y-x^2-2*x^2)+2, (-400)*x},
## { (-400)*x, 200}}
as_r(H) # as R expression
## expression(rbind(c(-400 * (y - x^2 - 2 * x^2) + 2, -400 * x),
## c(-400 * x, 200)))
eval(as_r(H), list(x = 3, y = 2)) # get value for a point
## [,1] [,2]
## [1,] 10002 -1200
## [2,] -1200 200
We can even get these in \(\LaTeX\) format:
tex(g)
## [1] "\\left( -400 x \\left( y - x ^{2}\\right) - 2 \\left( 1 - x\\right) , 200 \\left( y - x ^{2}\\right) \\right) "
\[ \nabla f(x, y) = \left( -400 x \left( y - x ^{2}\right) - 2 \left( 1 - x\right) , 200 \left( y - x ^{2}\right) \right) \]
tex(H)
## [1] "\\left( \\begin{array}{cc} -400 \\left( y - x ^{2} - 2 x ^{2}\\right) + 2 & -400 x \\\\ -400 x & 200 \\end{array} \\right) "
\[ \nabla^2 f(x, y) = \left( \begin{array}{cc} -400 \left( y - x ^{2} - 2 x ^{2}\right) + 2 & -400 x \\ -400 x & 200 \end{array} \right) \]