knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

symengine

R-CMD-check AppVeyor Build status

symengine is an R interface to the SymEngine C++ library for symbolic computation.

Installation

There are some dependencies needed on Unix systems. You may install them with

zypper install cmake gmp-devel mpfr-devel mpc-devel    ## openSUSE
dnf    install cmake gmp-devel mpfr-devel libmpc-devel ## Fedora
apt    install cmake libgmp-dev libmpfr-dev libmpc-dev ## Debian
brew   install cmake gmp mpfr libmpc                   ## Mac OS

Then you can install the R package with

devtools::install_github("symengine/symengine.R")

On Windows, you will need to install Rtools42 for building the package from source.

Please report any problem installing the package on your system.

library(symengine)

Usage

Also check the documentation site with built vignettes and help pages at http://symengine.marlin.pub.

Manipulating Symbolic Expressions

use_vars(x, y, z)
expr <- (x + y + z) ^ 2L - 42L
expand(expr)

Substitue z as a and y as x^2.

a <- S("a")
expr <- subs(expr, z, a)
expr <- subs(expr, y, x^2L)
expr

Second derivative of expr with regards to x:

d1_expr <- D(expr, "x")
d2_expr <- D(d1_expr, "x")
expand(d2_expr)

Solve the equation of d2_expr == 0 with regards to x.

solutions <- solve(d2_expr, "x")
solutions

Numerically Evaluate Symbolic Expressions

For the two solutions above, we can convert them into a function that gives numeric output with regards to given input.

func <- as.function(solutions)
ans <- func(a = -100:-95)
colnames(ans) <- c("Solution1", "Solution2")
ans

Numbers

The next prime number greater than 2^400.

n <- nextprime(S(~ 2 ^ 400))
n

The greatest common divisor between the prime number and 42.

GCD(n, 42)

The binomial coefficient (2^30 ¦ 5).

choose(S(~ 2^30), 5L)

Pi "computed" to 400-bit precision number.

if (symengine_have_component("mpfr"))
    evalf(Constant("pi"), bits = 400)

Object Equality

x + y == S("x + y")
x + y != S("x + y")
sin(x)/cos(x)
tan(x) == sin(x)/cos(x) # Different internal representation

Acknowledgement

This project was a Google Summer of Code project under the organization of The R Project for Statistical Computing in 2018. The student was Xin Chen, mentored by Jialin Ma and Isuru Fernando.



symengine/symengine.R documentation built on Feb. 28, 2024, 2:12 a.m.