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.
Start with a base symbol what can either be:
yacas
command, e.g. x
, 2*a
or something similarR
matrix or vector.Here, we keep it simple.
## y: 2*x^2-5
## {(-2)*x, 5*x}
## {2*x, -x^3}
## expression(c(-2 * x, 5 * x))
Then consider an R
matrix and vector:
## [,1] [,2] [,3] [,4]
## [1,] 1 -2 -3 -4
## [2,] 0 2 -2 -3
## [3,] 1 0 3 -2
## [4,] 2 1 0 4
## [1] 1 2 3 4
They are now considered yacas
-enabled:
## {{ 1, -2, -3, -4},
## { 0, 2, -2, -3},
## { 1, 0, 3, -2},
## { 2, 1, 0, 4}}
## [,1] [,2] [,3] [,4]
## [1,] 1 -2 -3 -4
## [2,] 0 2 -2 -3
## [3,] 1 0 3 -2
## [4,] 2 1 0 4
## {1, 2, 3, 4}
## [1] 1 2 3 4
Notice how they are printed using yacas
’s syntax.
We can apply yacas
functions using
y_fn()
:
## {{ 1, 0, 1, 2},
## {-2, 2, 0, 1},
## {-3, -2, 3, 0},
## {-4, -3, -2, 4}}
## {{ 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: 10
Standard R
commands are available (see the section
“Ryacas
high-level reference” at the end of this
document):
## [,1]
## [1,] -28
## [2,] -14
## [3,] 2
## [4,] 20
## {-28, -14, 2, 20}
## [,1] [,2] [,3] [,4]
## [1,] 1 0 1 2
## [2,] -2 2 0 1
## [3,] -3 -2 3 0
## [4,] -4 -3 -2 4
## {{ 1, 0, 1, 2},
## {-2, 2, 0, 1},
## {-3, -2, 3, 0},
## {-4, -3, -2, 4}}
## {{ 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)}}
## [,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
## [,1] [,2]
## [1,] -2 -3
## [2,] 2 -2
## [3,] 0 3
## [4,] 1 0
## {{-2, -3},
## { 2, -2},
## { 0, 3},
## { 1, 0}}
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 0 2 1 1
## [3,] 1 0 3 1
## [4,] 2 1 0 4
## {{1, 1, 1, 1},
## {0, 2, 1, 1},
## {1, 0, 3, 1},
## {2, 1, 0, 4}}
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 0 2 1 1
## [3,] 1 0 3 1
## [4,] 2 1 0 4
## {{1, 1, 1, 1},
## {0, 2, 1, 1},
## {1, 0, 3, 1},
## {2, 1, 0, 4}}
## [,1] [,2] [,3] [,4]
## [1,] 1 1.110223e-16 0 1.110223e-16
## [2,] 0 1.000000e+00 0 0.000000e+00
## [3,] 0 1.110223e-16 1 0.000000e+00
## [4,] 0 0.000000e+00 0 1.000000e+00
## {{1, 0, 0, 0},
## {0, 1, 0, 0},
## {0, 0, 1, 0},
## {0, 0, 0, 1}}
## [,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
## {{ 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}}
## [1] -1.2857143 -0.2857143 0.8571429 1.7142857
## {(-9)/7, (-2)/7, 6/7, 12/7}
We can also assign a yacas
variable, but remember that
this may be difficult to distinguish:
## [1] "W"
## [1] "{j,I,rformBitwiseOps,t,TeXForm'FuncPrec,Arow}"
## {{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}}"
## [1] "{W,j,I,rformBitwiseOps,t,TeXForm'FuncPrec,Arow}"
## [1] "{j,I,rformBitwiseOps,t,TeXForm'FuncPrec,Arow}"
## [1] "W"
There are additional functions available:
simplify()
tex()
To demonstrate these and some additional benefit, we exploit
yacas
’s symbolic availabilities.
## {{1, 0, 0, 0},
## {0, 1, 0, 0},
## {0, 0, 1, 0},
## {0, 0, 0, 1}}
## {{1/2, 0, 0, 0},
## { 0, 1/2, 0, 0},
## { 0, 0, 1/2, 0},
## { 0, 0, 0, 1/2}}
## {{ 1/2, 0, 0, 0},
## { d, 1/2, 0, 0},
## { d, 0, 1/2, 2*d+2},
## { 0, 0, 0, 1/2}}
## {{ 2, 0, 0, 0},
## { (-4)*d, 2, 0, 0},
## { (-4)*d, 0, 2, (-4)*(2*d+2)},
## { 0, 0, 0, 2}}
## {{ 2, 0, 0, 0},
## { (-4)*d, 2, 0, 0},
## { (-4)*d, 0, 2, (-8)*(d+1)},
## { 0, 0, 0, 2}}
## [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.
We illustrate using the example in https://mikl.dk/post/2019-pizza-frozen-yogurt-goebner/:
## y: (x^2*y)/4-a*(3*x+(3*y)/2-45)
We can consider one variable only:
## y: (x*y)/2-3*a
## {{y/2}}
Or multiple variables:
## {(x*y)/2-3*a, x^2/4-(3*a)/2, 45-(3*x+(3*y)/2)}
## {{ y/2, x/2, -3},
## { x/2, 0, (-3)/2},
## { -3, (-3)/2, 0}}
## expression(rbind(c(y/2, x/2, -3), c(x/2, 0, -3/2), c(-3, -3/2,
## 0)))
## [,1] [,2] [,3]
## [1,] 1 1.0 -3.0
## [2,] 1 0.0 -1.5
## [3,] -3 -1.5 0.0
The Jacobian is taken on a vector function denoted by many functions:
## {(x^2*y)/4-a*(3*x+(3*y)/2-45), x^3+4*a^2}
## {{(x*y)/2-3*a},
## { 3*x^2}}
## {{ (x*y)/2-3*a, x^2/4-(3*a)/2, 45-(3*x+(3*y)/2)},
## { 3*x^2, 0, 8*a}}
Say we want to find roots of a polynomial. We use the generic
solve(a, b, ...)
function.
Note the conventions are as follows:
R
’s
solve()
as demonstrated above):
a
is a matrix and b
not provided,
this finds the inverse of a
.a
is a matrix and a vector b
is
provided, the linear system of equations is solved.solve(a, b)
: find roots of a
for variable
b
, i.e. yacas Solve(a == 0, b)
solve(a, b, v)
: find solutions to a == b
for variable v
, i.e. yacas
Solve(a == b, v)
## y: x^2-x-6
## {x==(-2), x==3}
## [1] "\\left( x = -2, x = 3\\right)"
## {-2, 3}
We can also find values of x
where the polynomial equals
another constant. If we were working with strings via the low-level
interface it would be easy via paste()
, but as we are
working with ysym()
’s we use the solve()
function directly:
## {x==(Sqrt(37)+1)/2, x==(1-Sqrt(37))/2}
## [1] "\\left( x = \\frac{\\sqrt{37} + 1}{2} , x = \\frac{1 - \\sqrt{37}}{2} \\right)"
$$ \left( x = \frac{\sqrt{37} + 1}{2} , x = \frac{1 - \sqrt{37}}{2} \right) $$
$$\begin{align} 3 x y - y &= -5 x \\ x &= y + 4 \end{align}$$
## {{ x==2, y==(-2)},
## { x==2/3, y==(-10)/3}}
## [[1]]
## {2, -2}
##
## [[2]]
## {2/3, (-10)/3}
## [[1]]
## [[1]]$x
## [1] 2
##
## [[1]]$y
## [1] -2
##
##
## [[2]]
## [[2]]$x
## [1] 0.6666667
##
## [[2]]$y
## [1] -3.333333
do.call(rbind, lapply(seq_along(sol_envir), function(sol_no) {
sol_val <- sol_envir[[sol_no]]
data.frame(sol_no = sol_no,
eq_no = seq_along(sol_val),
lhs = eval(as_r(lhs), sol_val),
rhs = eval(as_r(rhs), sol_val))
}))
## sol_no eq_no lhs rhs
## 1 1 1 -10.0000000 -10.0000000
## 2 1 2 2.0000000 2.0000000
## 3 2 1 -3.3333333 -3.3333333
## 4 2 2 0.6666667 0.6666667
Ryacas
high-level referencePrinciple:
ysym(x)
converts x
to a
yac_symbol
that automatically runs yacas
when
needed. x
can both be a text string with yacas
commands or an R
vector/matrix.as_r(x)
: Is used to convert the yac_symbol
back to an R
representation.y_fn(x, fn, ...)
: Apply a yacas
function
fn
to the yac_symbol
x
,
i.e. fn(x, ...)
; note that this is evaluated immediately
when x
is a yac_symbol
as opposed to when
x
is a stringReference:
The following functions work with yac_symbol
s.
ysym()
: Create yac_symbol
ysym_make()
: Make a vector of
yac_symbol
sysym_ls()
: List declared yac_symbol
syac_*()
functions (see the “Getting started” vignette)
yac_str()
: Return yacas
stringyac_expr()
: Return R
expressionyac_silent()
: Do something silentlyyac_assign()
: Assign a variablesimplify(x, timeout = 2)
: Try yacas
’s
Simplify()
function. When the unix
package is
available, the timeout
(in seconds), stops trying after
that amount of time to avoid making the R
process
hang.tex()
: Converty_fn(x, fn, ...)
: Apply a yacas
function
fn
to the yac_symbol
x
,
i.e. fn(x, ...)
y_rmvars(x)
: Remove variable names in
x
deriv(expr, vars)
: takes derivative of
yac_symbol
expr
with respect to
vars
Jacobian(expr, vars)
: finds Jacobian of
yac_symbol
expr
(usually a vector of
expressions) with respect to vars
Hessian(expr, vars)
: finds Hessian matrix of
yac_symbol
expr
with respect to
vars
lim()
R
that has been implemented for
yac_symbol
s:
print()
c()
dim()
cbind()
rbind()
[
getter[<-
setter[[
getter%*%
matrix/vector multiplicationdiag()
getterdiag<-()
setterupper.tri()
getterlower.tri()
gettert()
solve()
(see above and in help page)integrate()
sum()
prod()
+
, -
, *
, /
,
^
sin()
, cos()
,
tan()
, asin()
, acos()
,
atan()
, asinh()
, acosh()
,
atanh()
, exp()
, log()
,
sqrt()