MyNixOS website logo
Description

Interface to the 'SymEngine' Library.

Provides an R interface to 'SymEngine' <https://github.com/symengine/>, a standalone 'C++' library for fast symbolic manipulation. The package has functionalities for symbolic computation like calculating exact mathematical expressions, solving systems of linear equations and code generation.

symengine

R-CMD-check AppVeyor Buildstatus

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)
#> SymEngine Version: 0.9.0
#>  _____           _____         _         
#> |   __|_ _ _____|   __|___ ___|_|___ ___ 
#> |__   | | |     |   __|   | . | |   | -_|
#> |_____|_  |_|_|_|_____|_|_|_  |_|_|_|___|
#>       |___|               |___|

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)
#> Initializing 'x', 'y', 'z'
expr <- (x + y + z) ^ 2L - 42L
expand(expr)
#> (Add)    -42 + 2*x*y + 2*x*z + 2*y*z + x^2 + y^2 + z^2

Substitue z as a and y as x^2.

a <- S("a")
expr <- subs(expr, z, a)
expr <- subs(expr, y, x^2L)
expr
#> (Add)    -42 + (a + x + x^2)^2

Second derivative of expr with regards to x:

d1_expr <- D(expr, "x")
d2_expr <- D(d1_expr, "x")
expand(d2_expr)
#> (Add)    2 + 4*a + 12*x + 12*x^2

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

solutions <- solve(d2_expr, "x")
solutions
#> VecBasic of length 2
#> V( -1/2 + (-1/2)*sqrt(1 + (-1/3)*(2 + 4*a)), -1/2 + (1/2)*sqrt(1 + (-1/3)*(2 + 4*a)) )

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
#>      Solution1 Solution2
#> [1,] -6.280715  5.280715
#> [2,] -6.251811  5.251811
#> [3,] -6.222762  5.222762
#> [4,] -6.193564  5.193564
#> [5,] -6.164215  5.164215
#> [6,] -6.134714  5.134714

Numbers

The next prime number greater than 2^400.

n <- nextprime(S(~ 2 ^ 400))
n
#> (Integer)    2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493557

The greatest common divisor between the prime number and 42.

GCD(n, 42)
#> (Integer)    1

The binomial coefficient (2^30 ¦ 5).

choose(S(~ 2^30), 5L)
#> (Integer)    11893730661780666387808571314613824587300864

Pi “computed” to 400-bit precision number.

if (symengine_have_component("mpfr"))
    evalf(Constant("pi"), bits = 400)
#> (RealMPFR,prec400)   3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066

Object Equality

x + y == S("x + y")
#> [1] TRUE
x + y != S("x + y")
#> [1] FALSE
sin(x)/cos(x)
#> (Mul)    sin(x)/cos(x)
tan(x) == sin(x)/cos(x) # Different internal representation
#> [1] FALSE

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.

Metadata

Version

0.2.6

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows