This vignette contains some basic code snippets for use with the symengine package.

It is currently in progress!

Construct symbolic expressions

Create symbol x from a string.

x <- Symbol("x")
x
## (Symbol) x

Parse an expression from a string

expr <- S("(x + 2*y)^3")
expr
## (Pow)    (x + 2*y)^3

Manipulating symbolic expressions

Expand an expression

use_vars(x, y, z, .quiet = TRUE)
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

Substitute variables

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

Compute derivatives

use_vars(x, y, .quiet = TRUE)
expr <- (x + 2L*sin(y))^3L
D(expr, y)
## (Mul)    6*cos(y)*(x + 2*sin(y))^2

Solve a polynomial equation

a <- Symbol("a")
poly <- x^2L + 2L*a*x + a^2L
solve(poly, x)
## VecBasic of length 1
## V( -a )

Construct vector and matrix

Construct a vector with c

use_vars(x, y, z, .quiet = TRUE)
vec <- c(x, x + y, x + y - z)
vec
## VecBasic of length 3
## V( x, x + y, x + y - z )

Construct a vector with Vector

lst <- list(x, x + y, x + y + z)
Vector(lst)
## VecBasic of length 3
## V( x, x + y, x + y + z )

Construct a symbolic matrix with Matrix

nms <- paste0("x_", rep(1:2, 3), rep(1:3, 2))
Matrix(nms, nrow = 2)
## DenseMatrix of dim 2 x 3
## [x_11, x_13, x_12]
## [x_22, x_21, x_23]

Construct a symbolic matrix with rbind

vec <- Vector(x, y, z)
rbind(vec, vec^2L-1L)
## DenseMatrix of dim 2 x 3
## [x, y, z]
## [-1 + x**2, -1 + y**2, -1 + z**2]

Numerically evaluate symbolic expressions

expr <- x^y + exp(x)
func <- as.function(expr)
func(x = 1:10, y = 2)
##  [1]     3.718282    11.389056    29.085537    70.598150   173.413159
##  [6]   439.428793  1145.633158  3044.957987  8184.083928 22126.465795