Ryacas
functionalityRyacas
makes the yacas
computer algebra
system available from within R
. The name yacas
is short for “Yet Another Computer Algebra System”. The
yacas
program is developed by Ayal Pinkhuis (who is also
the maintainer) and others, and is available at http://www.yacas.org/ for
various platforms. There is a comprehensive documentation (300+ pages)
of yacas
(also available at http://www.yacas.org/) and the documentation contains
many examples.
R
expressions, yacas
expressions and
Sym
objectsThe Ryacas
package works by sending
`commands'' to
yacaswhich makes the calculations and returns the result to
R`.
There are various different formats of the return value as well
R
expressionsA call to yacas
may be in the form of an R
expression which involves valid R calls, symbols or constants (though
not all valid R
expressions are valid). For example:
The result exp1
is not an expression in the
R
sense but an object of class "yacas"
. To
evaluate the resulting expression numerically, we can do
## [1] 15
yacas
expressionsSome commands are not proper R
expressions. For example,
typing
yacas(expression(D(x)Sin(x)))
produces an error. For such cases we can make a specification using
the yacas
syntax:
## yacas_expression(cos(x))
Sym
objectsProbably the most elegant way of working with yacas
is
by using Sym
objects. A Sym
object is a
yacas
character string that has the “Sym” class. One can
combine Sym
objects with other Sym
objects as
well as to other R
objects using +
,
-
and other similar R
operators.
The function Sym(x)
coerces an object x
to
a Sym
object by first coercing it to character and then
changing its class to “Sym”:
Operations on Sym
objects lead to new Sym
objects:
## yacas_expression(x + 4)
One can apply sin
, cos
, tan
,
deriv
, Integrate
and other provided functions
to Sym
objects. For example:
## yacas_expression(-cos(x))
In this way the communication with yacas
is
``tacit’’.
It is important to note the difference between the R
name x
and the symbol "x"
as illustrated
below:
## yacas_expression(xs)
## yacas_expression(xs + 4)
## [1] 9
The convention in the following is 1) that Sym
objects
match with their names that they end with an ‘s’, e.g.
Algebraic calculations:
## yacas_expression(823603)
## yacas_expression(149/21)
## yacas_expression(823643)
## yacas_expression(149/21)
Numerical evaluations:
## yacas_expression(-6)
## yacas_expression(-6)
Symbolic expressions:
## yacas_expression((x + 1) * (x - 1))
exp1 <- expression(x^2 + 2 * x^2)
exp2 <- expression(2 * exp0)
exp3 <- expression(6 * pi * x)
exp4 <- expression((exp1 * (1 - sin(exp3))) / exp2)
yacas(exp4)
## yacas_expression(3 * (x^2 * (1 - sin(6 * (x * pi))))/(2 * exp0))
## yacas_expression((xs + 1) * (xs - 1))
exp1 <- xs^2 + 2 * xs^2
exp0 <- Sym("exp0")
exp2 <- 2 * Sym(exp0)
exp3 <- 6 * Pi * xs
exp4 <- exp1 * (1 - sin(exp3)) / exp2
exp4
## yacas_expression(3 * (xs^2 * (1 - sin(6 * (xs * pi))))/(2 * exp0))
Combining symbolic and numerical expressions:
## yacas_expression(cos(x)^2 + 0.7080734182)
## yacas_expression(cos(xs)^2 + 0.708073418273571)
Differentiation:
## yacas_expression(cos(x))
## yacas_expression(cos(xs))
Integration:
## yacas_expression(TRUE)
## yacas_expression(cos(a) - cos(b))
## yacas_expression(cos(as) - cos(bs))
Expanding polynomials:
## yacas_expression(x^3 + 3 * x^2 + 3 * x + 1)
## yacas_expression(xs^3 + 3 * xs^2 + 3 * xs + 1)
Taylor expansion:
## yacas_expression(x + x^2/2 + x^3/6 + 1)
Printing the result in nice forms:
##
## 2 3
## x x
## x + -- + -- + 1
## 2 6
## [1] "x + \\frac{x ^{2}}{2} + \\frac{x ^{3}}{6} + 1"
##
## 2 3
## xs xs
## xs + --- + --- + 1
## 2 6
## [1] "xs + \\frac{xs ^{2}}{2} + \\frac{xs ^{3}}{6} + 1"
yacas
calculationsThe function Set()
and the operator :=
can
both be used to assign values to global variables.
## yacas_expression(60)
## yacas_expression(120)
The same can be achieved with Sym
objects: Consider:
## yacas_expression(60)
Now ns
exists as a variable in yacas
(and
we can make computations on this variable as above). However we have no
handle on this variable in R
. Such a handle is obtained
with
Now the R
variable ns
refers to the
yacas
variable ns
and we can make calculations
directly from R
, e.g:
## yacas_expression(123)
## yacas_expression(123)
## yacas_expression(15129)
Likewise:
## yacas_expression(cos(as))
## yacas_expression(2 * cos(as))
Clear a variable binding execute Clear()
:
## yacas_expression(120)
## yacas_expression(TRUE)
## yacas_expression(n)
## yacas_expression(1)
## yacas_expression(1)
## yacas_expression(TRUE)
## yacas_expression(ns)
Evaluations are generally exact:
## yacas_expression(1)
## yacas_expression(exp(1))
## yacas_expression(root(1/2, 2))
## yacas_expression(355/113)
## yacas_expression(1)
## yacas_expression(exp(1))
## yacas_expression(root(1/2, 2))
## yacas_expression(355/113)
To obtain a numerical evaluation (approximation), the
N()
function can be used:
## yacas_expression(2.7182818284)
## yacas_expression(0.7071067811)
## yacas_expression(3.1415929203)
## yacas_expression(2.71828182845905)
## yacas_expression(0.7071067811)
## yacas_expression(3.14159292035398)
The N()
function has an optional second argument, the
required precision:
## yacas_expression(2.66917293233083)
## yacas_expression(3.14159292035398)
The command Precision(n)
can be used to specify that all
floating point numbers should have a fixed precision of n digits:
## yacas_expression(Precision(5))
## yacas_expression(3.1415929203)
## yacas_expression(Precision(5))
## yacas_expression(3.1415929203)
Rational numbers will stay rational as long as the numerator and denominator are integers:
## yacas_expression(11/2)
## yacas_expression(11/2)
Some exact manipulations :
## yacas_expression(-12/7)
## yacas_expression(x)
## yacas_expression(x + y)
## yacas_expression(alpha + beta)
## yacas_expression(-12/7)
## yacas_expression(xs)
## yacas_expression(xs + ys)
## yacas_expression(xs + ys)
The imaginary unit i is denoted I and complex numbers can be entered as either expressions involving I or explicitly Complex(a,b) for a+ib.
## yacas_expression(-1)
## yacas_expression(complex_cartesian(7, 3))
## yacas_expression(complex_cartesian(7, -3))
## yacas_expression(complex_cartesian(cos(3), sin(3)))
## yacas_expression(-1)
## yacas_expression(complex_cartesian(7, 3))
## yacas_expression(complex_cartesian(7, -3))
## yacas_expression(complex_cartesian(cos(3), sin(3)))
%
operatorThe operator %
automatically recalls the result from the
previous line.
## yacas_expression((x + 1)^3)
## yacas_expression((x + 1)^3)
## yacas_expression((x + 1)^3)
## yacas_expression((xs + 1)^3)
## yacas_expression((xs + 1)^3)
PrettyForm
, PrettyPrint
and
TeXForm
There are different ways of displaying the output.
The (standard) yacas form is:
## Yacas matrix:
## [,1] [,2]
## [1,] a b
## [2,] c d
## yacas_expression((x + 1)^2 + k^3)
## Yacas matrix:
## [,1] [,2]
## [1,] a b
## [2,] c d
## yacas_expression((x + 1)^2 + k^3)
as <- Sym("as"); bs <- Sym("bs"); cs <- Sym("cs"); ds <- Sym("ds")
A <- List(List(as,bs), List(cs,ds))
ks <- Sym("ks")
B <- (1+xs)^2+ks^3
A
## Yacas matrix:
## [,1] [,2]
## [1,] as bs
## [2,] cs ds
## yacas_expression((xs + 1)^2 + ks^3)
The Pretty form is:
##
## / \
## | ( a ) ( b ) |
## | |
## | ( c ) ( d ) |
## \ /
##
## 2 3
## ( x + 1 ) + k
##
## / \
## | ( as ) ( bs ) |
## | |
## | ( cs ) ( ds ) |
## \ /
##
## 2 3
## ( xs + 1 ) + ks
## yacas_expression(8.15915283247898e+47)
## yacas_expression(Factorial(40))
Expand exp (x) in three terms around 0 and a:
## yacas_expression(x + x^2/2 + x^3/6 + 1)
## yacas_expression(exp(a) + exp(a) * (x - a) + (x - a)^2 * exp(a)/2 + (x - a)^3 * exp(a)/6)
## yacas_expression(xs + xs^2/2 + xs^3/6 + 1)
## yacas_expression(exp(as) + exp(as) * (xs - as) + (xs - as)^2 * exp(as)/2 + (xs - as)^3 * exp(as)/6)
The InverseTaylor()
function builds the Taylor series
expansion of the inverse of an expression. For example, the Taylor
expansion in two terms of the inverse of exp (x) around x = 0 (which is the Taylor expansion
of Ln(y)
around y = 1):
## yacas_expression(x - 1 - (x - 1)^2/2)
## yacas_expression(y - 1 - (y - 1)^2/2)
## yacas_expression(xs + xs^2/2 + 1)
## yacas_expression(ys - 1 - (ys - 1)^2/2)
Solve equations symbolically with the Solve()
function:
## Yacas vector:
## [1] x == a/(1 - a)
## Yacas vector:
## [1] x == 0 x == -1
## Yacas vector:
## [1] xs == as/(1 - as)
## Yacas vector:
## [1] xs == 0 xs == -1
## Yacas matrix:
## [,1] [,2]
## [1,] xs == root(6 - ys^2, 2) ys == ys
# FIXME
#Solve(List(mean==(xs/(xs+ys)), variance==((xs*ys)/(((xs+ys)^2) * (xs+ys+1)))),
# List(xs,ys))
(Note the use of the == operator, which does not evaluate to anything, to denote an “equation” object.)
To solve an equation (in one variable) like sin(x) − exp(x) = 0 numerically taking 0.5 as initial guess and an accuracy of 0.0001 do:
## yacas_expression(-3.1830630118)
## yacas_expression(-3.1830630118)
## yacas_expression(x^3 + 3 * x^2 + 3 * x + 1)
## yacas_expression(xs^3 + 3 * xs^2 + 3 * xs + 1)
The function Simplify() attempts to reduce an expression to a simpler form.
## yacas_expression((x + y)^3 - (x - y)^3)
## yacas_expression(6 * (x^2 * y) + 2 * y^3)
## yacas_expression((xs + y)^3 - (xs - y)^3)
## yacas_expression(6 * (xs^2 * y) + 2 * y^3)
Analytical derivatives of functions can be evaluated with the
D()
and deriv()
functions:
## yacas_expression(cos(x))
## yacas_expression(cos(xs))
These functions also accepts an argument specifying how often the derivative has to be taken, e.g:
## yacas_expression(sin(x))
## yacas_expression(sin(xs))
@ <<echo=F,results=hide>>= yacas(“Clear(a,b,A,B)”)
``` r
#yacas("Integrate(x,a,b)Sin(x)")
#yacas("Integrate(x,a,b)Ln(x)+x")
#yacas("Integrate(x)1/(x^2-1)")
yacas("Integrate(x)Sin(a*x)^2*Cos(b*x)")
## yacas_expression((2 * sin(b * x)/b - (sin(-2 * (x * a) - b * x)/(-2 * a - b) + sin(-2 * (x * a) + b * x)/(-2 * a + b)))/4)
#Integrate(sin(x),x,a,b)
#Integrate(log(x),x,a,b)
#Integrate(1/(x^2-1),x)
a <- Sym("a")
b <- Sym("b")
Integrate(sin(a*x)^2*cos(b*x),x)
## yacas_expression((2 * sin(b * xs)/b - (sin(-2 * (xs * a) - b * xs)/(-2 * a - b) + sin(-2 * (xs * a) + b * xs)/(-2 * a + b)))/4)
## yacas_expression(exp(1))
## yacas_expression(cos(x))
## yacas_expression(exp(1))
## yacas_expression(cos(xs))
## yacas_expression(2 * cos(a))
## yacas_expression(2 * cos(as))
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] u1 u1 0
## [2,] u1 0 u2
## [3,] 0 u2 0
##
## / \
## | ( u1 ) ( u1 ) ( 0 ) |
## | |
## | ( u1 ) ( 0 ) ( u2 ) |
## | |
## | ( 0 ) ( u2 ) ( 0 ) |
## \ /
u1 <- Sym("u1")
u2 <- Sym("u2")
E4 <- List(List(u1, u1, 0), List(u1, 0, u2), List(0, u2, 0))
PrettyForm(E4)
##
## / \
## | ( u1 ) ( u1 ) ( 0 ) |
## | |
## | ( u1 ) ( 0 ) ( u2 ) |
## | |
## | ( 0 ) ( u2 ) ( 0 ) |
## \ /
## Yacas matrix:
## [,1] [,2]
## [1,] u1 * u2^2/(u1^2 * u2^2) 1/u1 - u1 * u2^2/(u2^2 * u1^2)
## [2,] 1/u1 - u1 * u2^2/(u2^2 * u1^2) u1 * u2^2/(u2^2 * u1^2) - 1/u1
## [3,] -(u1 * u2/u1)/u2^2 u1 * u2/(u1 * u2^2)
## [,3]
## [1,] -(u1 * u2/u1)/u2^2
## [2,] u2 * u1/(u2^2 * u1)
## [3,] u1/u2^2
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] 1/u1 0 -1/u2
## [2,] 0 0 1/u2
## [3,] -1/u2 1/u2 u1/u2^2
##
## / \
## | / 1 \ ( 0 ) / -1 \ |
## | | -- | | -- | |
## | \ u1 / \ u2 / |
## | |
## | ( 0 ) ( 0 ) / 1 \ |
## | | -- | |
## | \ u2 / |
## | |
## | / -1 \ / 1 \ / u1 \ |
## | | -- | | -- | | --- | |
## | \ u2 / \ u2 / | 2 | |
## | \ u2 / |
## \ /
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] 1/u1 0 -1/u2
## [2,] 0 0 1/u2
## [3,] -1/u2 1/u2 u1/u2^2
##
## / \
## | / 1 \ ( 0 ) / -1 \ |
## | | -- | | -- | |
## | \ u1 / \ u2 / |
## | |
## | ( 0 ) ( 0 ) / 1 \ |
## | | -- | |
## | \ u2 / |
## | |
## | / -1 \ / 1 \ / u1 \ |
## | | -- | | -- | | --- | |
## | \ u2 / \ u2 / | 2 | |
## | \ u2 / |
## \ /
## yacas_expression(-(u1 * u2^2))
## yacas_expression((u1 * u2^2/(u2^2 * u1^2) - 1/u1) * (u1 * u2^2) * u1/(u1^2 * u2^4) - u1 * u2^2 * (u2 * u1) * (u1 * u2)/(u1 * u2^2 * (u2^2 * u1 * (u1^2 * u2^2))) - (1/u1 - u1 * u2^2/(u2^2 * u1^2)) * (u1 * u2)^2/(u2^4 * u1^2) - (1/u1 - u1 * u2^2/(u2^2 * u1^2))^2 * u1/u2^2 - (1/u1 - u1 * u2^2/(u2^2 * u1^2)) * (u2 * u1) * (u1 * u2)/(u2^4 * u1^2) - (u1 * u2^2/(u2^2 * u1^2) - 1/u1) * (u1 * u2)^2/(u1^2 * u2^4))
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] 1/u1 0 -1/u2
## [2,] 0 0 1/u2
## [3,] -1/u2 1/u2 u1/u2^2
## yacas_expression(-1/(u1 * u2^2))
## yacas_expression(-(u1 * u2^2))
## yacas_expression((u1 * u2^2/(u2^2 * u1^2) - 1/u1) * (u1 * u2^2) * u1/(u1^2 * u2^4) - u1 * u2^2 * (u2 * u1) * (u1 * u2)/(u1 * u2^2 * (u2^2 * u1 * (u1^2 * u2^2))) - (1/u1 - u1 * u2^2/(u2^2 * u1^2)) * (u1 * u2)^2/(u2^4 * u1^2) - (1/u1 - u1 * u2^2/(u2^2 * u1^2))^2 * u1/u2^2 - (1/u1 - u1 * u2^2/(u2^2 * u1^2)) * (u2 * u1) * (u1 * u2)/(u2^4 * u1^2) - (u1 * u2^2/(u2^2 * u1^2) - 1/u1) * (u1 * u2)^2/(u1^2 * u2^4))
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] 1/u1 0 -1/u2
## [2,] 0 0 1/u2
## [3,] -1/u2 1/u2 u1/u2^2
## yacas_expression(-1/(u1 * u2^2))
Note that the value returned by yacas
can be of
different types:
## *" ("+" (x ,1 ),"- (x ,1 ))
## "*" ("+" (x ,1 ),"-" (x ,1 ))