SymEngine Quick Start

#knitr::opts_chunk$set(
#    collapse = TRUE,
#    comment = "#>"
#)

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

It is currently in progress!

library(symengine)

Construct symbolic expressions

Create symbol x from a string.

x <- Symbol("x")
x

Parse an expression from a string

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

Manipulating symbolic expressions

Expand an expression

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

Substitute variables

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

Compute derivatives

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

Solve a polynomial equation

a <- Symbol("a")
poly <- x^2L + 2L*a*x + a^2L
solve(poly, x)

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

Construct a vector with Vector

lst <- list(x, x + y, x + y + z)
Vector(lst)

Construct a symbolic matrix with Matrix

nms <- paste0("x_", rep(1:2, 3), rep(1:3, 2))
Matrix(nms, nrow = 2)

Construct a symbolic matrix with rbind

vec <- Vector(x, y, z)
rbind(vec, vec^2L-1L)

Numerically evaluate symbolic expressions

expr <- x^y + exp(x)
func <- as.function(expr)
func(x = 1:10, y = 2)

Print Latex in Rmarkdown document

old_options <- options(symengine.latex = TRUE, symengine.latex.center = TRUE)
use_vars(x, y, .quiet = TRUE)
sqrt(x + y)
options(old_options)


Try the symengine package in your browser

Any scripts or data that you put into this service are public.

symengine documentation built on Oct. 23, 2022, 5:06 p.m.