Elaborate reference of Ryacas functionality

Rob Goedman, Gabor Grothendieck, Søren Højsgaard, Ayal Pinkus and Mikkel Meyer Andersen

2020-09-29

library(Ryacas0)

Introduction

Ryacas 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 objects

The Ryacas package works by sending `commands'' toyacaswhich makes the calculations and returns the result toR`. There are various different formats of the return value as well

R expressions

A 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 expressions

Some 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 objects

Probably 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.

A sample session

Algebraic calculations:

yacas(expression((10 + 2) * 5 + 7^7))
## yacas_expression(823603)
yacas(expression(1/14+5/21* (30- 1+ 1/2)))
## yacas_expression(149/21)
#(Sym(10) + Sym(2)) * Sym(5) + Sym(7) ^ Sym(7)
Sym("10 * 2") * 5 + Sym(7) ^ 7
## yacas_expression(823643)
#(Sym(10) + 2) * 5 + Sym(7) ^ 7
#Sym("(10+2)*5 + 7^7")
Sym("1/14 + 5/21 * (30 - 1+1/2)")
## yacas_expression(149/21)

Numerical evaluations:

yacas(expression(N(-12/2)))
## yacas_expression(-6)
Sym("-12/2")
## yacas_expression(-6)
#Eval(Sym("-12/2"))

Symbolic expressions:

yacas(expression(Factor(x^2-1)))
## 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))
Factor(xs^2-1)
## 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(N(Sin(1)^2 + Cos(x)^2)))
## yacas_expression(cos(x)^2 + 0.7080734182)
N(sin(1)^2 + cos(xs)^2)
## yacas_expression(cos(xs)^2 + 0.708073418273571)

Differentiation:

yacas("D(x)Sin(x)")
## yacas_expression(cos(x))
deriv(sin(xs), xs)
## yacas_expression(cos(xs))

Integration:

yacas("Clear(a,b,A,B)")
## yacas_expression(TRUE)
yacas("Integrate(x,a,b)Sin(x)")
## yacas_expression(cos(a) - cos(b))
as <- Sym("as")
bs <- Sym("bs")
Integrate(sin(xs), xs, as, bs)
## yacas_expression(cos(as) - cos(bs))

Expanding polynomials:

yacas("Expand((1+x)^3)")
## yacas_expression(x^3 + 3 * x^2 + 3 * x + 1)
Expand((1+xs)^3)
## yacas_expression(xs^3 + 3 * xs^2 + 3 * xs + 1)

Taylor expansion:

yacas("texp := Taylor(x,0,3) Exp(x)")
## yacas_expression(x + x^2/2 + x^3/6 + 1)
texp <- Taylor(exp(xs), xs, 0, 3)
# Or: texp <- Sym("texp")

Printing the result in nice forms:

yacas("PrettyForm(texp)") 
## 
##      2    3    
##     x    x     
## x + -- + -- + 1
##     2    6
yacas("TeXForm(texp)")
## [1] "x + \\frac{x ^{2}}{2}  + \\frac{x ^{3}}{6}  + 1"
PrettyForm(texp) 
## 
##        2     3    
##      xs    xs     
## xs + --- + --- + 1
##       2     6
TeXForm(texp)
## [1] "xs + \\frac{xs ^{2}}{2}  + \\frac{xs ^{3}}{6}  + 1"

Simple yacas calculations

Setting and clearing a variable

The 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)

Symbolic and numerical evaluations, precision

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

Rational numbers will stay rational as long as the numerator and denominator are integers:

## yacas_expression(11/2)
## yacas_expression(11/2)

Symbolic calculation

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)

Complex numbers and the imaginary unit

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)))

Recall the most recent line – the % operator

The 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)

Printing with PrettyForm, PrettyPrint and TeXForm

There are different ways of displaying the output.

Standard form

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)
## Yacas matrix:
##      [,1] [,2]
## [1,] as   bs  
## [2,] cs   ds
## yacas_expression((xs + 1)^2 + ks^3)

Pretty form

The Pretty form is:

## 
## /              \
## | ( a ) ( b )  |
## |              |
## | ( c ) ( d )  |
## \              /
## 
##          2    3
## ( x + 1 )  + k
## 
## /                \
## | ( as ) ( bs )  |
## |                |
## | ( cs ) ( ds )  |
## \                /
## 
##           2     3
## ( xs + 1 )  + ks

TeX form

The output can be displayed in TeX form:

## [1] "\\left( x + 1\\right)  ^{2} + k ^{3}"
## [1] "\\left( xs + 1\\right)  ^{2} + ks ^{3}"

Commands

Factorial

## yacas_expression(8.15915283247898e+47)
## yacas_expression(Factorial(40))

Taylor expansions

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)

Solving equations

Solving equations symbolically

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

(Note the use of the == operator, which does not evaluate to anything, to denote an “equation” object.)

Solving equations numerically

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)

Expanding polynomials

## yacas_expression(x^3 + 3 * x^2 + 3 * x + 1)
## yacas_expression(xs^3 + 3 * xs^2 + 3 * xs + 1)

Simplifying an expression

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

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))

Integration

@ <<echo=F,results=hide>>= yacas(“Clear(a,b,A,B)”) ```

## 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)
## 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)

Limits

## yacas_expression(exp(1))
## yacas_expression(cos(x))
## yacas_expression(exp(1))
## yacas_expression(cos(xs))

Variable substitution

## yacas_expression(2 * cos(a))
## yacas_expression(2 * cos(as))

Solving ordinary differential equations

## yacas_expression(C545 * exp(2 * x) + C549 * exp(-2 * x))
## yacas_expression(C579 * exp(8 * x))

Matrices

yacas("E4:={ {u1,u1,0},{u1,0,u2},{0,u2,0} }")
## Yacas matrix:
##      [,1] [,2] [,3]
## [1,] u1   u1   0   
## [2,] u1   0    u2  
## [3,] 0    u2   0
yacas("PrettyForm(E4)")
## 
## /                       \
## | ( 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 )   |
## \                       /

Inverse

## 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  /  |
## \                        /

Determinant

## 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))

Miscellaneous

Note that the value returned by yacas can be of different types:

yacas(expression(Factor(x^2-1)),retclass='unquote')
## *" ("+" (x ,1 ),"- (x ,1 ))
yacas(expression(Factor(x^2-1)),retclass='character')
## "*" ("+" (x ,1 ),"-" (x ,1 ))

Output width

Set output width:

get_output_width()
## [1] 60
set_output_width(120)
get_output_width()
## [1] 120