Ryacas version 1.0.0 released!

[This article was first published on R-bloggers on Mikkel Meyer Andersen, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

It is with great pleasure that I can announce that Ryacas version 1.0.0 is
now released to CRAN (https://cran.r-project.org/package=Ryacas).
I wish to thank all co-authors:
Rob Goedman, Gabor Grothendieck, Søren Højsgaard, Grzegorz Mazur, Ayal Pinkus.

It means that you can install the package by (possible after binaries have been built):

install.packages("Ryacas")

Followed by:

library(Ryacas)

(The source code is available at https://github.com/mikldk/ryacas/.)

Now you have the yacas computer algebra system fully available!
See documentation at https://yacas.readthedocs.io/en/latest/.
Ryacas allows one to send unprocessed yacas strings and certain other R objects to yacas process from R and get back the result. It also has facilities for manipulating yacas strings and R expressions destined for yacas processing.

It can be used for arbitrary-precision arithmetic, symbolic math, ASCII
pretty printing and translating R to TeX.

Below I will demonstrate a few ways to use Ryacas, but I also refer to a number of blog posts that I have made already:

Examples of usage

I will just give a few examples and then refer to the package webpage especially the vignettes:

There are two main functions in the low-level interface:

  • yac_str(x): Evaluate yacas command x (a string) and get result as string/character.
  • yac_expr(x): Evaluate yacas command x (a string) and get result as an R expression.

And one main function in the high-level interface:

  • yac_symbol(x): Takes a yacas command (e.g. x, 2*a), or an R vector or matrix and creates a yac_symbol

Simple examples of the low-level interface:

yac_str("x+x+x+x")
## [1] "4*x"
yac_str("Factor(x^2+x-6)")
## [1] "(x-2)*(x+3)"
yac_str("D(x) x^2 + 4*x")
## [1] "2*x+4"
ex <- yac_expr("D(x) x^2 + 4*x")
ex
## expression(2 * x + 4)
eval(ex, list(x = 2))
## [1] 8
yac_str("Limit(x, 1) (x^2 - 1)/(x - 1)")
## [1] "2"
yac_str("Sum(n, 1, Infinity, (1/2)^n)")
## [1] "1"
yac_str("Fibonacci(10)")
## [1] "55"
yac_str("Sum(n, 1, 10, Fibonacci(n))")
## [1] "143"
yac_str("TeXForm(x^2 - 1)")
## [1] "x ^{2} - 1"
yac_str("Solve(a*x^2 + b*x + c == 0, x)")
## [1] "{x==(Sqrt(b^2-4*a*c)-b)/(2*a),x==(-(b+Sqrt(b^2-4*a*c)))/(2*a)}"

Simple examples of the high-level interface:

x <- yac_symbol("x")
2*x^2 - 5
## [1] 2*x^2-5
c(-2, 5)*x
## [1] {(-2)*x,5*x}
as_r(c(-2, 5)*x)
## expression(c(-2 * x, 5 * x))
A <- outer(0:3, 1:4, "-") + diag(2:5)
a <- 1:4
B <- yac_symbol(A)
B
## {{ 1, -2, -3, -4},
##  { 0,  2, -2, -3},
##  { 1,  0,  3, -2},
##  { 2,  1,  0,  4}}
b <- yac_symbol(a)
b
## [1] {1,2,3,4}
B %*% b
## [1] {-28,-14,2,20}
t(B)
## {{ 1,  0,  1,  2},
##  {-2,  2,  0,  1},
##  {-3, -2,  3,  0},
##  {-4, -3, -2,  4}}
exp(B)
## {{ Exp(1), Exp(-2), Exp(-3), Exp(-4)},
##  {      1,  Exp(2), Exp(-2), Exp(-3)},
##  { Exp(1),       1,  Exp(3), Exp(-2)},
##  { Exp(2),  Exp(1),       1,  Exp(4)}}
as_r(exp(B))
##          [,1]      [,2]        [,3]        [,4]
## [1,] 2.718282 0.1353353  0.04978707  0.01831564
## [2,] 1.000000 7.3890561  0.13533528  0.04978707
## [3,] 2.718282 1.0000000 20.08553692  0.13533528
## [4,] 7.389056 2.7182818  1.00000000 54.59815003
B[, 2:3]
## {{-2, -3},
##  { 2, -2},
##  { 0,  3},
##  { 1,  0}}
B[upper.tri(B)] <- 1
B
## {{1, 1, 1, 1},
##  {0, 2, 1, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
2*B - B
## {{1, 1, 1, 1},
##  {0, 2, 1, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
B %*% solve(B)
## {{1, 0, 0, 0},
##  {0, 1, 0, 0},
##  {0, 0, 1, 0},
##  {0, 0, 0, 1}}
solve(B %*% t(B))
## {{   363/98, (-123)/98,  (-87)/98,  (-61)/98},
##  {(-123)/98,     34/49,     23/98,     15/98},
##  { (-87)/98,     23/98,     33/98,     13/98},
##  { (-61)/98,     15/98,     13/98,     17/98}}

And it even works with variables:

D <- B
D[2, 3] <- "x"
D
## {{1, 1, 1, 1},
##  {0, 2, x, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
d <- b
d[2] <- "x"
d
## [1] {1,x,3,4}
D %*% d
## [1] {x+8,5*x+4,14,x+18}
ex <- as_r(D %*% d)
ex
## expression(c(x + 8, 5 * x + 4, 14, x + 18))
eval(ex, list(x = 2))
## [1] 10 14 14 20

To leave a comment for the author, please follow the link and comment on their blog: R-bloggers on Mikkel Meyer Andersen.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)