symbolic
is an R package designed to provide symbolic algebra
capabilities natively in R using R functions.
It is highly experimental and dependent on external contributors to be a fully fledged Computer Algebra System (CAS). It will be developed to the extent needed for my upstream packages, specifically distributional which requires many symbolic algebra tools not yet readily available in R. Currently powered by yacas via Ryacas.
You can install the development version of symbolic like so:
remotes::install_github("mitchelloharawild/symbolic")
Prepare a function for symbolic transformations with as_symbolic()
.
library(symbolic)
#>
#> Attaching package: 'symbolic'
#> The following object is masked from 'package:stats':
#>
#> integrate
.f <- function(x, y) log(x + 1 + y)
.f
#> function(x, y) log(x + 1 + y)
.f_sym <- as_symbolic(.f)
.f_sym
#> {x, y}: log(x + 1 + y)
Internally, we use yacas for all the hard work.
.f_yac <- symbolic:::as_yacas(.f)
.f_yac
#> y: Ln(x+y+1)
symbolic:::r_from_yacas(.f_yac)
#> {x, y}: log(x + y + 1)
Typical base functions for math have symbolic
methods which return
functions.
solve(.f_sym, y, "x")
#> {y}: exp(y) - 1 - y
deriv(.f_sym, "x")
#> {x, y}: 1/(x + y + 1)
integrate(.f_sym, "x")
#> {x, y}: (x + y + 1) * log(x + y + 1) - (x + y + 1)