The high-level (symbol) interface

Mikkel Meyer Andersen and Søren Højsgaard

2019-08-29

library(Ryacas)

A short summary of often-used yacas commands are found in the section “yacas reference” in the “Getting started” vignette. A short summary of Ryacas’s high-level functions are found in the section “Ryacas high-level reference” at the end of this document.

Introduction

Start with a base symbol what can either be:

Here, we keep it simple. Consider an R matrix and vector:

A <- outer(0:3, 1:4, "-") + diag(2:5)
a <- 1:4
A
##      [,1] [,2] [,3] [,4]
## [1,]    1   -2   -3   -4
## [2,]    0    2   -2   -3
## [3,]    1    0    3   -2
## [4,]    2    1    0    4
a
## [1] 1 2 3 4

They are now considered yacas-enabled:

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}

Notice how they are printed using yacas’s syntax.

We can apply yacas functions using y_fn():

y_fn(B, "Transpose")
## {{ 1,  0,  1,  2},
##  {-2,  2,  0,  1},
##  {-3, -2,  3,  0},
##  {-4, -3, -2,  4}}
y_fn(B, "Inverse")
## {{   37/202,     3/101,    41/202,    31/101},
##  {(-17)/101,    30/101,     3/101,     7/101},
##  {(-19)/202,  (-7)/101,    39/202,  (-5)/101},
##  { (-5)/101,  (-9)/101, (-11)/101,     8/101}}
y_fn(B, "Trace")
## [1] 10

Standard R commands are available (see the section “Ryacas high-level reference” at the end of this document):

A %*% a
##      [,1]
## [1,]  -28
## [2,]  -14
## [3,]    2
## [4,]   20
B %*% b
## [1] {-28,-14,2,20}
t(A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    1    2
## [2,]   -2    2    0    1
## [3,]   -3   -2    3    0
## [4,]   -4   -3   -2    4
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
A[, 2:3]
##      [,1] [,2]
## [1,]   -2   -3
## [2,]    2   -2
## [3,]    0    3
## [4,]    1    0
B[, 2:3]
## {{-2, -3},
##  { 2, -2},
##  { 0,  3},
##  { 1,  0}}
A[upper.tri(A)] <- 1
B[upper.tri(B)] <- 1
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]    0    2    1    1
## [3,]    1    0    3    1
## [4,]    2    1    0    4
B
## {{1, 1, 1, 1},
##  {0, 2, 1, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
2*A - A
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]    0    2    1    1
## [3,]    1    0    3    1
## [4,]    2    1    0    4
2*B - B
## {{1, 1, 1, 1},
##  {0, 2, 1, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
A %*% solve(A)
##               [,1]          [,2]         [,3]         [,4]
## [1,]  1.000000e+00 -1.110223e-16 5.551115e-17 5.551115e-17
## [2,] -1.110223e-16  1.000000e+00 5.551115e-17 5.551115e-17
## [3,]  2.220446e-16 -1.110223e-16 1.000000e+00 0.000000e+00
## [4,]  0.000000e+00 -2.220446e-16 0.000000e+00 1.000000e+00
B %*% solve(B)
## {{1, 0, 0, 0},
##  {0, 1, 0, 0},
##  {0, 0, 1, 0},
##  {0, 0, 0, 1}}
solve(A %*% t(A))
##            [,1]       [,2]       [,3]       [,4]
## [1,]  3.7040816 -1.2551020 -0.8877551 -0.6224490
## [2,] -1.2551020  0.6938776  0.2346939  0.1530612
## [3,] -0.8877551  0.2346939  0.3367347  0.1326531
## [4,] -0.6224490  0.1530612  0.1326531  0.1734694
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}}

We can also assign a yacas variable, but remember that this may be difficult to distinguish:

yac_str("W") # Get variable W if exists, or else just a symbol
## [1] "W"
yac_str("Variables()") # ...or list variables
## [1] "{j,rformBitwiseOps,I}"
B
## {{1, 1, 1, 1},
##  {0, 2, 1, 1},
##  {1, 0, 3, 1},
##  {2, 1, 0, 4}}
yac_assign(B, "W") # assign B in R to W in yacas
yac_str("W") # Get variable W if exists, or else just a symbol
## [1] "{{1,1,1,1},{0,2,1,1},{1,0,3,1},{2,1,0,4}}"
yac_str("Variables()") # ...or list variables
## [1] "{j,rformBitwiseOps,I,W}"
yac_silent("Clear(W)")
yac_str("Variables()") # List variables
## [1] "{j,rformBitwiseOps,I}"
yac_str("W") # Get variable W if exists, or else just a symbol
## [1] "W"

There are additional functions available:

To demonstrate these and some additional benefit, we exploit yacas’s symbolic availabilities.

D <- diag(4) %>% yac_symbol()
D
## {{1, 0, 0, 0},
##  {0, 1, 0, 0},
##  {0, 0, 1, 0},
##  {0, 0, 0, 1}}
D <- D/2
D
## {{1/2,   0,   0,   0},
##  {  0, 1/2,   0,   0},
##  {  0,   0, 1/2,   0},
##  {  0,   0,   0, 1/2}}
D[2:3, 1] <- "d"
D[3,4] <- "2*d + 2"
D
## {{  1/2,     0,     0,     0},
##  {    d,   1/2,     0,     0},
##  {    d,     0,   1/2, 2*d+2},
##  {    0,     0,     0,   1/2}}
D %>% solve()
## {{           2,            0,            0,            0},
##  {      (-4)*d,            2,            0,            0},
##  {      (-4)*d,            0,            2, (-4)*(2*d+2)},
##  {           0,            0,            0,            2}}
D %>% solve() %>% simplify()
## {{         2,          0,          0,          0},
##  {    (-4)*d,          2,          0,          0},
##  {    (-4)*d,          0,          2, (-8)*(d+1)},
##  {         0,          0,          0,          2}}
D %>% solve() %>% simplify() %>% tex()
## [1] "\\left( \\begin{array}{cccc} 2 & 0 & 0 & 0 \\\\ -4 d & 2 & 0 & 0 \\\\ -4 d & 0 & 2 & -8 \\left( d + 1\\right)  \\\\ 0 & 0 & 0 & 2 \\end{array} \\right) "

\[ \left( \begin{array}{cccc} 2 & 0 & 0 & 0 \\ -4 d & 2 & 0 & 0 \\ -4 d & 0 & 2 & -8 \left( d + 1\right) \\ 0 & 0 & 0 & 2 \end{array} \right) \]

yacas has a Simplify() function. This is made available via a simplify() function that also includes a time-out that prevents yacas in making the R session hang, but it requires that the unix package is available. The default timeout value used when unix is available is 2 seconds.

Ryacas high-level reference

Principle:

Reference:

The following functions work with yac_symbols.