symengine/symengine.R

Name: symengine.R

Owner: symengine

Description: A R interface to the symbolic manipulation library SymEngine.

Created: 2017-10-14 03:08:08.0

Updated: 2018-03-14 06:36:27.0

Pushed: 2018-03-14 06:36:26.0

Homepage:

Size: 171

Language: C++

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

symengine

Travis-CI Build Status

This is an experiment to provide a R interface to the SymEngine library. It is still in progress, but if you are interested, please contact Jialin Ma marlin-@gmx.cn and Isuru Fernando isuruf@gmail.com.

This project is expected to be a GSoC 2018 project under the organization of The R Project for Statistical Computing.

Installation

Note: It is not supported on Windows yet.

Currently, you will have to install or compile symengine library manually on your computer. Please follow the instruction at https://github.com/symengine/symengine.

Then in R, try

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

Please report any problem installing the package on your system.

ary(symengine)
ymEngine Version: 0.3.0
_____           _____         _         
   __|_ _ _____|   __|___ ___|_|___ ___ 
__   | | |     |   __|   | . | |   | -_|
_____|_  |_|_|_|_____|_|_|_  |_|_|_|___|
     |___|               |___|

ttaching package: 'symengine'
he following object is masked from 'package:base':

   diff
Usage
Symbol

A symbol or variable can be constructed from character.

- Symbol("x"))
Symbol) x
- Symbol("y"))
Symbol) y
y
Pow)    x^y
Integer, RealDouble, RealMPFR

There are explicit constructors for such types:

ger(42L)
Integer)    42
Double(base::pi)
RealDouble) 3.14159265358979

For large integer and high-precision floating number that R can not hold, you can construct “Integer” or “RealMPFR” from character. For example:

937169318
1] 8.615937e+12
nteger(8615937169318)
arning: NAs introduced by coercion to integer range
1] NA
ger("8615937169318")
Integer)    8615937169318
ger("8615937169318") ^ 9L
Integer)    261651187038033556722865852191251735369739650060120439731902444918211391981554448221540729228593041137656153397421568
r
DO, currently not available
MPFR("3.1415926535897932384626433832795028841971693993751058209", bits = 70)

Comparing with the mpfr function in Rmpfr package:

r::mpfr("3.1415926535897932384626433832795028841971693993751058209", precbits = 70)
 'mpfr' number of precision  187   bits 
1] 3.141592653589793238462643383279502884197169399375105820901

Or simply use the SymEngine parser instead of the explicit constructors:

615937169318")
Integer)    8615937169318
.1415926535897932384626433832795028841971693993751058209")
RealMPFR)   3.1415926535897932384626433832795028841971693993751058209
Complex, ComplexDouble and ComplexMPC

There will be explicit constructors (TODO):

Or use the parser:

 + 9I")
Complex)    6 + 9*I

Or use:

 9L * Constant("I")
Add)    6 + 9*i

The mpc library is used for holding complex number with arbitrary precision, similar to mpfr library for floating number.

.3 + 23.9999999999999999999I")
ComplexMPC) 2.29999999999999982236 + 23.9999999999999999999*I
Constants

For example:

tant("pi")
Constant)   pi
Constant("pi") / 2L)
Integer)    1
Generic Conversion and Parser

As already showed in the above examples, S converts a R object to SymEngine object. When the input is a character, it will parse the expression to produce appropriate object.

)
Integer)    6
2)
RealDouble) 4.2
r
- S("x"))
Symbol) x
- S(~ k))          # Currently only work with "symbol"
Symbol) k
- Constant("b"))
Constant)   b
 * x + b")
Add)    b + k*x
x + b
Add)    k*x + b
r
i")
Constant)   pi
in(pi)")
Integer)    0
r
tan(x) + sin(x))^2")
Pow)    (sin(x) + tan(x))^2
 + 2 >= a")
LessThan)   a <= 2 + a
Substitue a Variable

Substitute a variable with another one:

 S("x")
 S("a")
r <- (tan(x) + sin(x) + a) ^ 2L)
Pow)    (a + sin(x) + tan(x))^2
r
(expr, "x", "a")
Pow)    (a + sin(a) + tan(a))^2
(expr, "x", 3.1415926)
Pow)    (-7.94093388050907e-23 + a)^2
(expr, "x", Constant("pi"))
Pow)    a^2
(expr, "x", Constant("pi") * 2L/3L)
Pow)    (a + (-1/2)*sqrt(3))^2
Expand an Expression

Pow)    (a + sin(x) + tan(x))^2
nd(expr)
Add)    2*a*sin(x) + 2*a*tan(x) + 2*sin(x)*tan(x) + a^2 + sin(x)^2 + tan(x)^2
Derivative

Pow)    (a + sin(x) + tan(x))^2
(expr, "x")
Mul)    2*(a + sin(x) + tan(x))*(1 + tan(x)^2 + cos(x))
(expr, "a")
Mul)    2*(a + sin(x) + tan(x))
Evaluate an Expression
f(Constant("pi"), bits = 999)
RealMPFR)   3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127
Object Equality and Hash
y == S("x + y")
1] TRUE
y != S("x + y")
1] FALSE
r
x)
Tan)    tan(x)
x)/cos(x)
Mul)    sin(x)/cos(x)
x) == sin(x)/cos(x) # Different internal representation
1] FALSE
r
(tan(x))
1] "5308874006"
(sin(x)/cos(x))
1] "46369110327826722"
N Theory

TODO

FunctionSymbol

TODO

(x, y)")
FunctionSymbol) f(x, y)
Lambdify

TODO

Vector
 vecbasic("a", 32L, 32)

ecBasic of length 3
1] a
2] 32
3] 32.0
]
ecBasic of length 2
1] 32
2] 32.0
]]
Symbol) a
 S("a + b"))
ecBasic of length 4
1] a
2] 32
3] 32.0
4] a + b
Matrix (DenseMatrix and SparseMatrix)

TODO

Under the Hood

The SymEngine objects are implemented with “externalptr”:

 Symbol(~ x)
x)
ormal class 'Basic' [package "symengine"] with 1 slot
 ..@ .xData:<externalptr>

See “src/interface.c” for the C code that wraps the symengine api.

Related R Packages
Notes on some dependencies

The SymEngine library can optionally depend on some external libraries, which is configured by CMake, see the list of CMake options in README of SymEngine and the configure script of Rlibsymengine.

A few notes:

  1. GMP (GNU Multiple Precision Arithmetic Library) is a C library that can be used to store and do arithmetic calculation with big integers and rationals. It has an R interface (gmp package).

  2. mpfr (Multiple Precision Floating-Point Reliable) is a C library that depends on the GMP library and is used for arbitrary precision floating number arithmetic calculations. It has an R interface (Rmpfr package). This is an optional dependency for SymEngine.

  3. mpc () is a C library that extends the mpfr library for the arithmetic of complex numbers with arbitrarily precision. There is a R package mpc which is not on CRAN, but available at R forge.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.