User-defined yacas rules

Mikkel Meyer Andersen and Søren Højsgaard

2023-01-16

library(Ryacas)

Included rules

yacas comes with a number of rules all defined in the yacas directory of the installed package:

system.file(package = "Ryacas", "yacas")
## [1] "/tmp/Rtmp8hYNaH/Rinst909f59779d54/Ryacas/yacas"

For example in the sums.rep folder, a number of rules for sums are defined in the code.ys file.

As an example, the fact that \[ \sum_{k = 1}^n (2k-1) = n^2 \] is defined in yacas as

SumFunc(_k,1,_n,2*_k-1, n^2 );

and the geometric sum is defined as

SumFunc(_k,0,_n,(r_IsFreeOf(k))^(_k), (1-r^(n+1))/(1-r) );

These can be verified:

yac_str("Sum(i, 1, m, 2*i-1)")
## [1] "m^2"
yac_str("Sum(i, 0, m, 2^i)")
## [1] "2^(m+1)-1"

There are also rules in yacas that are able to let the user change some limits of some sums, e.g. for the geometric sum:

yac_str("Sum(i, 1, m, 2^i)")
## [1] "2^(m+1)-2"

Custom rules

But what about changing the limit of the first sum? I.e. instead of \[ \sum_{k = 1}^n (2k-1) = n^2 \] then know that \[ \sum_{k = 0}^n (2k-1) = -1 + \sum_{k = 1}^n (2k-1) = n^2 - 1 . \] But what does yacas say?

yac_str("Sum(i, 0, m, 2*i-1)")
## [1] "Sum(i,0,m,2*i-1)"

We can then add our own rule by:

yac_silent("SumFunc(_k,0,_n,2*_k-1, n^2 - 1)")

And then try again:

yac_str("Sum(i, 0, m, 2*i-1)")
## [1] "m^2-1"

A good source of inspiration for writing custom rules is reading the included rules, but there is a lot to programming in yacas and we refer to yacas’s documentation, specifically the chapter Programming in Yacas.