diff --git a/.Rbuildignore b/.Rbuildignore deleted file mode 100644 index 2fa3ac9..0000000 --- a/.Rbuildignore +++ /dev/null @@ -1,14 +0,0 @@ -^.*\.Rproj$ -^\.Rproj\.user$ -^\.travis\.yml$ -^README\.Rmd$ -^README-.*\.png$ -^appveyor\.yml$ -^cran-comments\..*$ -CMakeFiles -^[.].*$ -^.*/[.].*$ -^_pkgdown\.yml$ -^docs$ -^pkgdown$ -^\.github$ diff --git a/.github/.gitignore b/.github/.gitignore deleted file mode 100644 index 2d19fc7..0000000 --- a/.github/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.html diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml deleted file mode 100644 index 40e4be7..0000000 --- a/.github/workflows/R-CMD-check.yaml +++ /dev/null @@ -1,90 +0,0 @@ -# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. -# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions -on: - push: - branches: - - main - - master - pull_request: - branches: - - main - - master - -name: R-CMD-check - -jobs: - R-CMD-check: - runs-on: ${{ matrix.config.os }} - - name: ${{ matrix.config.os }} (${{ matrix.config.r }}) - - strategy: - fail-fast: false - matrix: - config: - - {os: windows-latest, r: 'release'} - - {os: windows-latest, r: 'devel-ucrt'} - - {os: macOS-latest, r: 'release'} - - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - env: - R_REMOTES_NO_ERRORS_FROM_WARNINGS: true - RSPM: ${{ matrix.config.rspm }} - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - - steps: - - uses: actions/checkout@v2 - - - uses: r-lib/actions/setup-r@v1 - with: - r-version: ${{ matrix.config.r }} - - - uses: r-lib/actions/setup-pandoc@v1 - - - name: Query dependencies - run: | - install.packages('remotes') - saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) - writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") - shell: Rscript {0} - - - name: Cache R packages - if: runner.os != 'Windows' - uses: actions/cache@v2 - with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - - name: Install system dependencies - if: runner.os == 'Linux' - run: | - while read -r cmd - do - eval sudo $cmd - done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') - - - name: Install dependencies - run: | - remotes::install_deps(dependencies = TRUE) - remotes::install_cran("rcmdcheck") - shell: Rscript {0} - - - name: Check - env: - _R_CHECK_CRAN_INCOMING_REMOTE_: false - run: | - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran", "--ignore-vignettes"), build_args = c("--no-build-vignettes"), error_on = "warning", check_dir = "check") - cat("####### 00check.log ######################\n") - system2("cat", "check/*.Rcheck/00check.log") - cat("####### 00install.out ######################\n") - system2("cat", "check/*.Rcheck/00install.out") - shell: Rscript {0} - - - name: Upload check results - if: failure() - uses: actions/upload-artifact@main - with: - name: ${{ runner.os }}-r${{ matrix.config.r }}-results - path: check diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml deleted file mode 100644 index 762cf46..0000000 --- a/.github/workflows/pkgdown.yaml +++ /dev/null @@ -1,49 +0,0 @@ -on: - push: - branches: - - main - - master - -name: pkgdown - -jobs: - pkgdown: - runs-on: macOS-latest - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@v2 - - - uses: r-lib/actions/setup-r@v1 - - - uses: r-lib/actions/setup-pandoc@v1 - - - name: Query dependencies - run: | - install.packages('remotes') - saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) - writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") - shell: Rscript {0} - - - name: Cache R packages - uses: actions/cache@v2 - with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - - name: Install dependencies - run: | - remotes::install_deps(dependencies = TRUE) - install.packages("pkgdown", type = "binary") - install.packages("odeintr", type = "binary") - shell: Rscript {0} - - - name: Install package - run: R CMD INSTALL . - - - name: Deploy package - run: | - git config --local user.email "actions@github.com" - git config --local user.name "GitHub Actions" - Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e8786e3..0000000 --- a/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -.Rproj.user -.Rhistory -.RData -.Ruserdata -src/*.o -src/*.so -src/*.dll -.dir-locals.el -src/CMakeFiles -CMakeFiles -inst/doc -src/Makevars -docs -/src/upstream/ -/src/upstream.tar -/tools/SYMENGINE_BUNDLED diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.nojekyll @@ -0,0 +1 @@ + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 71ead4c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,42 +0,0 @@ -# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r - -language: r -cache: packages -r: - - release - - devel -os: - - linux - - osx -sudo: false - -matrix: - exclude: - - r: devel - os: osx - -# env: -# matrix: -# - SYMENGINE_COMMIT=master -# - SYMENGINE_COMMIT=535ff3c7716c8a3ca4254e51b1b9cd5baf5d8b19 - -apt_packages: - - cmake - - libgmp-dev - - libmpfr-dev - - libmpc-dev - - devscripts - -#brew_packages: -# - cmake -# - gmp -# - mpfr -# - libmpc - -before_install: - - if [ $TRAVIS_OS_NAME = osx ]; then brew install cmake gmp mpfr libmpc checkbashisms || true; fi - -warnings_are_errors: true - -notifications: - email: false diff --git a/404.html b/404.html new file mode 100644 index 0000000..4b36d1a --- /dev/null +++ b/404.html @@ -0,0 +1,115 @@ + + +
+ + + + +odeintr by Timothy H. Keitt is an R package for integrating differential equations with the Boost odeint library. It allows specifying the model in C++ code and compiling it on the fly with Rcpp.
+Here we provide an interface to the odeintr
package, but
odeintr
.However, the interface is not stable and may subject to change in future. This mainly serves an example of how to extend symengine and you are welcome to check out the source code.
+ +## SymEngine Version: 0.9.0
+## _____ _____ _
+## | __|_ _ _____| __|___ ___|_|___ ___
+## |__ | | | | __| | . | | | -_|
+## |_____|_ |_|_|_|_____|_|_|_ |_|_|_|___|
+## |___| |___|
+A ordinary differential equation could be constructed with dxdt(x) == rhs
where x
and rhs
will be converted to SymEngine’s ‘Basic’ S4 object. This works by defining a S4 method of ==
for the return type of dxdt()
.
For example
+ +## Ordinary differential equation:
+## d(x)/dt == 3.0/(1.0 + x)
+ODESystem
will take a list of ordinary differential equations, generate C++ source code and compile on the fly with Rcpp. The following is the Lorenz system.
+sigma <- 10
+rho <- 28
+beta <- 8/3
+use_vars(x, y, z)
## Initializing 'x', 'y', 'z'
+
+lorenz_sys <- list(
+ dxdt(x) == sigma * (y - x),
+ dxdt(y) == (rho - z) * x - y,
+ dxdt(z) == - beta * z + x * y
+)
+lorenz_sys <- ODESystem(lorenz_sys, method = "rk5_i")
The method argument is passed to odeintr::compile_sys
.
A S4 method of predict
is defined to run the model with given initial conditions, duration and step_size.
+res <- predict(lorenz_sys, init = c(x=1, y=1, z=1),
+ duration = 100, step_size = 0.001, start = 0)
+head(res)
## Time x y z
+## 1 0.000 1.000000 1.000000 1.0000000
+## 2 0.001 1.000130 1.025989 0.9983486
+## 3 0.002 1.000516 1.051961 0.9967278
+## 4 0.003 1.001158 1.077922 0.9951378
+## 5 0.004 1.002051 1.103879 0.9935788
+## 6 0.005 1.003193 1.129840 0.9920511
+
+Example of Van der Pol oscillator from odeintr
package.
+use_vars(x, y)
## Initializing 'x', 'y'
+
+vdp_sys <- ODESystem(
+ dxdt(x) == y,
+ dxdt(y) == 2 * (1 - x * x) * y - x,
+ method = "bsd" # Bulirsch-Stoer
+)
+res <- predict(vdp_sys, init = rep(1e-4, 2), duration = 100, step_size = 0.01)
+oldpar <- par(mfrow = c(2, 2), mar = rep(0.5, 4), oma = rep(5, 4), xpd = NA)
+make.plot <- function(xy, xlab = NA, ylab = NA)
+ plot(xy, col = "steelblue", lwd = 2, type = "l",
+ axes = FALSE, xlab = xlab, ylab = ylab)
+plot.new()
+make.plot(res[, c(3, 1)]); axis(3); axis(4)
+make.plot(res[, c(1, 2)], "Time", "X1"); axis(1); axis(2)
+make.plot(res[, c(3, 2)], "X2"); axis(1); axis(4)
+title(main = "Van der Pol Oscillator", outer = TRUE)
+par(oldpar)
This vignette contains some basic code snippets for use with the symengine
package.
It is currently in progress!
+ +
+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
+symengine
is an R interface to the SymEngine C++ library for symbolic computation.
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.
+ +Also check the documentation site with built vignettes and help pages at http://symengine.marlin.pub.
+
+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)) )
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
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)
.
Pi “computed” to 400-bit precision number.
+
+if (symengine_have_component("mpfr"))
+ evalf(Constant("pi"), bits = 400)
+#> (RealMPFR,prec400) 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066
S4 method of D
defined for Basic
. It returns
+the derivative of expr
with regards to name
.
+name
may be missing if there is only one symbol in
+expr
.
# S4 method for SymEngineDataType
+D(expr, name)
A Basic object.
A character vector or a Basic object of type Symbol.
Same type as expr
argument.
Construct DoubleVisitor
object from Basic
or VecBasic
+and use it to numerically evaluate symbolic expressions.
DoubleVisitor(
+ exprs,
+ args,
+ perform_cse = TRUE,
+ llvm_opt_level = if (symengine_have_component("llvm")) 3L else -1L
+)
+
+visitor_call(visitor, input, do_transpose = FALSE)
A Basic object or a VecBasic object to be evaluated.
A VecBasic object indicating order of input arguments. Can be missing.
Boolean.
Integer. If negative, it will return a LambdaDoubleVisitor
,
+otherwise it will return a LLVMDoubleVisitor
with the specified optimization level.
A DoubleVisitor object.
A numeric matrix. Each row is input value for one argument.
Boolean. Matters when exprs
is a VecBasic.
+If true, output will have each column for one symbolic expression, otherwise
+each row for one symbolic expression.
DoubleVisitor
returns a callable LambdaDoubleVisitor
or
LLVMDoubleVisitor
. visitor_call
returns a numeric vector or matrix.
DoubleVisitor
constructs the visitor and visitor itself is callable.
+visitor_call
is the low level function to call the visitor with input.
a <- S("a")
+b <- S("b")
+c <- S("c")
+vec <- c(log(a), log(a)/log(b) + c)
+func <- DoubleVisitor(vec, args = c(a, b, c))
+args(func)
+#> function (a, b, c)
+#> NULL
+
+## Use closure
+func(a = 1:10, b = 10:1, c = 1.43)
+#> [,1] [,2]
+#> [1,] 0.0000000 1.430000
+#> [2,] 0.6931472 1.745465
+#> [3,] 1.0986123 1.958321
+#> [4,] 1.3862944 2.142414
+#> [5,] 1.6094379 2.328244
+#> [6,] 1.7917595 2.543283
+#> [7,] 1.9459101 2.833677
+#> [8,] 2.0794415 3.322789
+#> [9,] 2.1972246 4.599925
+#> [10,] 2.3025851 Inf
+
+## Use visitor_call
+input <- rbind(a = 1:10, b = 10:1, c = 1.43)
+visitor_call(func, input, do_transpose = TRUE)
+#> [,1] [,2]
+#> [1,] 0.0000000 1.430000
+#> [2,] 0.6931472 1.745465
+#> [3,] 1.0986123 1.958321
+#> [4,] 1.3862944 2.142414
+#> [5,] 1.6094379 2.328244
+#> [6,] 1.7917595 2.543283
+#> [7,] 1.9459101 2.833677
+#> [8,] 2.0794415 3.322789
+#> [9,] 2.1972246 4.599925
+#> [10,] 2.3025851 Inf
+
FunctionSymbol
creates a Basic object with type FunctionSymbol
.
+Function
returns a generator.
Function(name)
+
+FunctionSymbol(name, args)
Name of the function symbol
Dependent symbols
FunctionSymbol
returns a Basic
. Function
returns
+ a function that will return a Basic
f <- Function("f")
+a <- Symbol("a")
+b <- Symbol("b")
+f(a, b)
+#> (FunctionSymbol) f(a, b)
+e <- f(a, f(a + b))
+D(e, a)
+#> (Add) Subs(Derivative(f(_xi_1), _xi_1), (_xi_1), (a + b))*Subs(Derivative(f(a, _xi_2), _xi_2), (_xi_2), (f(a + b))) + Subs(Derivative(f(_xi_1, f(a + b)), _xi_1), (_xi_1), (a))
+FunctionSymbol("f", c(a,b))
+#> (FunctionSymbol) f(a, b)
+
This function constructs a symbolic matrix (DenseMatrix
S4 object)
+with a similar interface with R's matrix
function.
Matrix(data, nrow = 1L, ncol = 1L, byrow = FALSE)
A R object.
Number of rows and columns.
Boolean value. Whether the data should be filled by row or by column.
DenseMatrix
S4 object.
This is a wrapper of the odeintr
R package using
+symengine objects to specify the ODE system and C code
+generation functionality from symengine to generate the
+C++ source. The dxdt
function and defined ==
S4 method
+allow one to intuitively specify the ODE system with symengine
+objects. The ODESystem
will generate C++ source
+and compile on the fly with Rcpp. Then predict
can be used to get
+results.
dxdt(x)
+
+# S4 method for DxdtOdeConstructor,ANY
+==(e1, e2)
+
+ODESystem(
+ odesys,
+ ...,
+ method = "rk5_i",
+ atol = 1e-06,
+ rtol = 1e-06,
+ compile = TRUE
+)
+
+# S4 method for ODESystem
+predict(object, init, duration, step_size = 1, start = 0)
A SymEngine Basic object of type Symbol or a R object
+that will be converted to Symbol(x)
.
A DxdtOdeConstructor S4 object which can be returned by `dxdt`.
A Basic object or an R object that will be converted to `S(e2)`.
DxdtOde S4 objects that can be returned with +`dxdt(x) == rhs`. Or `odesys` can be a list of DxdtOde S4 objects +when there is no dot arguments.
Passed to `odeintr::compile_sys`.
Logical, whether to compile the C++ source. Useful if +you only want to obtain the code.
A ODESystem S4 object.
A numeric vector specifying the initial conditions. It can +be named with the variable names or it can be unnamed but in the +same of order of equations.
Passed to the function generated by +`odeintr::compile_sys`.
dxdt
returns a DxdtOdeConstructor S4 object.
S4 method of `==` for "DxdtOdeConstructor" returns a DxdtOde S4 object.
+ + +`ODESystem` returns a ODESystem S4 object.
+ + +`predict` returns a dataframe.
+# A differential equation specified with dxdt and ==
+x <- Symbol("x")
+eq <- dxdt(x) == 1/exp(x)
+print(eq)
+#> Ordinary differential equation:
+#> d(x)/dt == 1.0*exp(-x)
+# \donttest{
+## Lorenz system
+use_vars(x, y, z)
+#> Initializing ‘x’, ‘y’, ‘z’
+sigma <- 10
+rho <- 28
+beta <- 8/3
+lorenz_sys <- ODESystem(
+ dxdt(x) == sigma * (y - x),
+ dxdt(y) == (rho - z) * x - y,
+ dxdt(z) == - beta * z + x * y
+)
+res <- predict(
+ lorenz_sys, init = c(x = 1, y = 1, z = 1), duration = 100, step_size = 0.001
+)
+plot(res[, c(2, 4)], type = 'l', col = "steelblue", main = "Lorenz Attractor")
+
+# }
+
`S` and `Basic` converts a R object to a Basic object. `Symbol`, `Real` and `Constant` +construct a Basic object with type "Symbol", "RealDouble"/"RealMPFR" +and "Constant", respectively.
+S(x)
+
+Basic(x)
+
+Symbol(x)
+
+Constant(x)
+
+Real(x, prec = NULL)
A R object.
If supplied, the argument will be parsed as a Basic object of type +RealMPFR.
A Basic
S4 object.
For double vector, `S` will check whether it is a whole number -- if true, +it will be converted to a Integer type. If this behavior is not desired, +you can use `Basic` or `as(x, "Basic")`.
+S("(x + y)^2")
+#> (Pow) (x + y)^2
+S(~ (x + y)^2)
+#> (Pow) (x + y)^2
+S(NaN)
+#> (NaN) NaN
+S(42)
+#> (Integer) 42
+Basic(42)
+#> (RealDouble) 42.0
+as(42, "Basic")
+#> (RealDouble) 42.0
+pi <- Constant("pi")
+evalf(pi)
+#> (RealDouble) 3.14159265358979
+if (symengine_have_component("mpfr"))
+ evalf(pi, 300)
+#> (RealMPFR,prec300) 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
+Real(42)
+#> (RealDouble) 42.0
+if (symengine_have_component("mpfr"))
+ Real(42, prec = 140)
+#> (RealMPFR,prec140) 42.000000000000000000000000000000000000000
+
A symbolic vector is represented by VecBasic
S4 class.
+Vector
and V
are constructors of VecBasic
.
Vector(x, ...)
+
+V(...)
R objects.
A VecBasic
.
There are some differences between Vector
and V
.
For double values, V
will check whether they are
+ whole number, and convert them to integer if so.
+ Vector
will not.
V
does not accept "non-scalar" arguments,
+ like Vector(c(1,2,3))
.
These functions are used to access the underlying properties of a
+Basic
object.
get_type(x)
+
+get_args(x)
+
+get_hash(x)
+
+get_str(x)
+
+free_symbols(x)
+
+function_symbols(x)
+
+get_name(x)
+
+get_prec(x)
A Basic object.
get_type()
, get_hash()
, get_str()
, get_name()
+ return a string.
get_args()
, free_symbols()
, function_symbols()
+ return a VecBasic
S4 object.
get_prec()
returns an integer.
Return the internal type
Return the internal arguments of a Basic object as a VecBasic
Return the hash as a string
Return the string representation of the Basic object
Return free symbols in an expression
Return function symbols in an expression
Return name of a Basic object of type FunctionSymbol
Return precision of a Basic object of type RealMPFR
These are S4 methods defined for Basic
, VecBasic
+and DenseMatrix
.
# S4 method for Basic,Basic
+==(e1, e2)
+
+# S4 method for Basic,Basic
+!=(e1, e2)
+
+# S4 method for SymEngineDataType,SymEngineDataType
+Arith(e1, e2)
+
+# S4 method for SymEngineDataType,vector
+Arith(e1, e2)
+
+# S4 method for vector,SymEngineDataType
+Arith(e1, e2)
+
+# S4 method for SymEngineDataType,missing
+-(e1, e2)
+
+# S4 method for SymEngineDataType,missing
++(e1, e2)
+
+# S4 method for DenseMatrix,DenseMatrix
+%*%(x, y)
+
+# S4 method for VecBasic,VecBasic
+%*%(x, y)
+
+# S4 method for DenseMatrix,VecBasic
+%*%(x, y)
+
+# S4 method for DenseMatrix,vector
+%*%(x, y)
+
+# S4 method for VecBasic,DenseMatrix
+%*%(x, y)
+
+# S4 method for vector,DenseMatrix
+%*%(x, y)
+
+# S4 method for SymEngineDataType
+Math(x)
+
+# S4 method for SymEngineDataType
+sinpi(x)
+
+# S4 method for SymEngineDataType
+cospi(x)
+
+# S4 method for SymEngineDataType
+tanpi(x)
+
+# S4 method for SymEngineDataType
+log(x, base)
+
+# S4 method for SymEngineDataType
+log2(x)
+
+# S4 method for SymEngineDataType
+log10(x)
+
+# S4 method for SymEngineDataType
+log1p(x)
+
+# S4 method for SymEngineDataType
+expm1(x)
+
+# S4 method for SymEngineDataType
+sum(x, ..., na.rm = FALSE)
+
+# S4 method for SymEngineDataType
+prod(x, ..., na.rm = FALSE)
Objects.
Ignored
==
and !=
will return a logical vector. Other
+ functions will return a Basic
, VecBasic
or DenseMatrix
.
S3 methods of cbind
and rbind
defined for
+DenseMatrix
and VecBasic
.
DenseMatrix, VecBasic or R objects.
Not used.
DenseMatrix
S4 object.
Generate C/MathML/LaTeX/JavaScript code string from a Basic
+or VecBasic
object.
codegen(x, type = c("ccode", "mathml", "latex", "jscode"))
A Basic or a VecBasic object.
One of "ccode", "mathml", "latex" and "jscode".
A character vector.
+Miscellaneous S4 methods defined for converting a Basic
or
+VecBasic
object to R number/string/language object.
# S4 method for Basic
+as.character(x)
+
+# S4 method for Basic
+as.numeric(x)
+
+# S4 method for Basic
+as.integer(x)
+
+# S4 method for VecBasic
+as.character(x)
+
+# S4 method for VecBasic
+as.numeric(x)
+
+# S4 method for VecBasic
+as.integer(x)
+
+as.language(x)
+
+# S4 method for Basic
+as.language(x)
The object to be converted.
Same as default methods of these generics. as.language()
may return symbol
, integer
, double
or call
.
These are miscellaneous S3/S4 methods defined for DenseMatrix
class.
# S3 method for DenseMatrix
+as.matrix(x, ...)
+
+# S4 method for DenseMatrix
+dim(x)
+
+# S4 method for DenseMatrix
+dim(x) <- value
+
+# S4 method for VecBasic
+dim(x) <- value
+
+# S4 method for Basic
+dim(x) <- value
+
+# S4 method for DenseMatrix
+dimnames(x) <- value
+
+# S4 method for DenseMatrix
+dimnames(x)
+
+# S4 method for DenseMatrix
+length(x)
+
+# S4 method for DenseMatrix,ANY
+[[(x, i, j, ...)
+
+# S4 method for DenseMatrix
+[[(x, i, j, ...) <- value
+
+# S4 method for DenseMatrix
+[(x, i, j, ..., drop = TRUE)
+
+# S4 method for DenseMatrix
+[(x, i, j, ...) <- value
A DenseMatrix object.
Arguments for subsetting, assignment or replacing.
Same or similar with the generics of these methods.
+S4 method of det
defined for DenseMatrix
.
det(x, ...)
+
+# S4 method for DenseMatrix
+det(x, ...)
A DenseMatrix object.
Unused.
A Basic
object.
mat <- Matrix(LETTERS[1:9], 3)
+det(mat)
+#> (Add) im*A*exp(1) + B*F*G + C*D*H - (im*B*D + A*F*H + C*G*exp(1))
+
This function will evaluate a SymEngine object to its "numerical" form
+with given precision. User may further use as.double()
to convert
+to R value.
evalf(expr, bits = 53L, complex = FALSE)
A SymEngine object.
The precision.
Whether or not to be evaluated as a complex number.
Same type as expr
argument.
This function takes a SymEngine object and return +its expanded form.
+expand(x)
A Basic/VecBasic/DenseMatrix S4 object.
Same type as input.
+expr <- S(~ (x + y) ^ 3)
+expand(expr)
+#> (Add) 3*x*y^2 + 3*x^2*y + x^3 + y^3
+
These functions currently use DoubleVisitor
to
+convert a Basic/VecBasic object to a DoubleVisitor
which
+essentially is a S4 class extending R function.
lambdify(x, args, backend = c("auto", "lambda", "llvm"), perform_cse = TRUE)
+
+# S3 method for BasicOrVecBasic
+as.function(x, args, backend = "auto", perform_cse = TRUE, ...)
A Basic object or a VecBasic object.
A VecBasic object specifying the arguments of the resulted function.
+It will be passed to DoubleVisitor
and can be missing.
One of "auto", "lambda" and "llvm". If "auto",
+getOption("lambdify.backend")
will be used to determine the value. If that
+option is not set, it will be determined based on symengine_have_component("llvm")
.
Passed to DoubleVisitor
.
Not used
A DoubleVisitor
S4 object.
These are some special mathematical functions and functions +related to number theory.
+LCM(a, b)
+
+GCD(a, b)
+
+nextprime(a)
+
+factorial(x)
+
+# S4 method for SymEngineDataType
+factorial(x)
+
+choose(n, k)
+
+# S4 method for SymEngineDataType
+choose(n, k)
+
+zeta(a)
+
+lambertw(a)
+
+dirichlet_eta(a)
+
+erf(a)
+
+erfc(a)
+
+# S4 method for SymEngineDataType,SymEngineDataType
+atan2(y, x)
+
+kronecker_delta(x, y)
+
+lowergamma(x, a)
+
+uppergamma(x, a)
+
+# S4 method for SymEngineDataType,SymEngineDataType
+beta(a, b)
+
+# S4 method for SymEngineDataType
+psigamma(x, deriv = 0L)
+
+# S4 method for SymEngineDataType
+digamma(x)
+
+# S4 method for SymEngineDataType
+trigamma(x)
SymEngine objects (Basic
/VecBasic
/DenseMatrix
).
+Some functions require Integer type.
Same type as input.
+Solve system of symbolic equations or solve a polynomial equation. +Depending on types of arguments, it supports different modes. +See Details and Examples.
+solve(a, b, ...)
+
+# S4 method for DenseMatrix
+solve(a, b, ...)
+
+# S4 method for VecBasic
+solve(a, b, ...)
+
+# S4 method for Basic
+solve(a, b, ...)
Objects, see details.
Not used.
A VecBasic
or DenseMatrix
S4 object.
solve
is a generic function dispatched on the class of the first argument.
If a
is a (square) DenseMatrix, it solves the equation
+ a %*% x = b
for x
. (similar to solve.default()
)
If a
is a DenseMatrix and b
is missing, b
is
+ taken to be an identity matrix and solve
will return the
+ inverse of a
. (similar to solve.default()
)
If a
is a VecBasic, it solves the system of linear equations
+ represented by a
with regards to symbols represented in b
.
If a
is a Basic, it solves the polynomial equation represented by
+ a with regards to the symbol represented in b
.
## Inverse of a symbolic matrix
+mat <- Matrix(c("A", "B", "C", "D"), 2)
+solve(mat)
+#> DenseMatrix of dim 2 x 2
+#> [(1 + B*C/(A*(D - B*C/A)))/A, -C/(A*(D - B*C/A))]
+#> [-B/(A*(D - B*C/A)), (D - B*C/A)**(-1)]
+
+## Solve a %*% x == b
+a <- Matrix(c("a11", "a21", "a12", "a22"), 2) # a is a 2x2 matrix
+b <- Vector("b1", "b2") # b is a length 2 vector
+solve(a, b) # Solution of x (2x1 matrix)
+#> DenseMatrix of dim 2 x 1
+#> [(b1 - a12*(b2 - b1*a21/a11)/(a22 - a21*a12/a11))/a11]
+#> [(b2 - b1*a21/a11)/(a22 - a21*a12/a11)]
+
+## Solve the system of linear equations represented by a with regards to
+## symbols in b
+a <- Vector(~ -2*x + y - 4, # A system of linear equations
+ ~ 3*x + y - 9)
+b <- Vector(~x, ~y) # Symbols to solve (x and y)
+solve(a, b) # Solution of x and y
+#> VecBasic of length 2
+#> V( 1, 6 )
+
This function will substitute expr
with pairs of
+values in the dot arguments. The length of dot arguments must
+be a even number.
subs(expr, ...)
A Basic
S4 object.
Pairs of Basic objects or values can be converted to Basic
.
+In the order of "from1, to1, from2, to2, ...".
Same type as expr
.
R/symengine.R
+ symengine.Rd
symengine
is a R package for symbolic computation.
SymEngine library is a standalone fast symbolic +manipulation library written in C++. It allows computation over mathematical expressions +in a way which is similar to the traditional manual computations of mathematicians and +scientists. The R interface of the library tries to provide a user-friendly way to do +symbolic computation in R and can be integrated into other packages to help solve related +tasks. The design of the package is somehow similar to the SymPy +package in Python. Unlike some other computer algebra systems, it does not invent its own +language or domain specific language but uses R language to manipulate the symbolic +expressions.
+symengine
uses the S4 dispatch system extensively to differentiate between calculation
+over normal R objects and symengine objects. For example, the semantics of sin
in
+expr <- Symbol("x"); sin(expr)
is different from the sin
used over normal R numbers.
Basic
is simply a S4 class holding a pointer representing a symbolic expression
+in symengine. Basic
objects have the same S4 class but can have different
+C-level representations which can be accessed via get_type()
.
+For example, Basic(~ 1/2)
will have "Rational" type and Basic(1/2)
will have
+"RealDouble" type.
A Basic
object will also have a list of associated sub-components
+which can be accessed via get_args()
. For example, (expr <- S("x") * 3L * S("a"))
+will have type "Mul", and as.list(get_args(expr))
will show the three factors of
+the multiplication.
A Basic
object can be constructed via Basic()
, S()
, Symbol()
, Constant()
or
+Real()
.
VecBasic and DenseMatrix are S4 classes representing a symbolic vector or matrix.
+They can be constructed with Vector()
, V()
, Matrix()
, c()
, rbind()
+or cbind()
. For example the following code will construct a 2x3 matrix.
The following functions are expected to work naturally with VecBasic and DenseMatrix +classes.
[
, [[
, [<-
and [[<-
for subsetting and assignment.
dim()
, dim<-
, length()
, t()
, det()
, rbind()
, cbind()
, c()
, rep()
%*%
for matrix multiplication
solve(a, b)
: solve a %*% x = b
where a
is a square DenseMatrix and
+b
is a VecBasic/DenseMatrix.
solve(a)
: find the inverse of a
where a
is a square DenseMatrix.
solve(a, b)
: solve system of linear equations represented by a
(VecBasic) with
+regards to symbols in b
(VecBasic).
Further, the R functions that work on Basic objects (e.g. sin
) are expected work
+on VecBasic and DenseMatrix objects as well in a vectorized manner.
The following is a (incomplete) list of functions that are expected to work with
+symengine objects. Note that these functions can also be used inside a formula or
+R language objects and passed to S or Basic or Vector to construct symengine
+objects. For example S(~ sin(x) + 1)
and S(quote(sin(x) + 1))
.
+
, -
, *
, /
, ^
abs
, sqrt
, exp
, expm1
, log
, log10
, log2
, log1p
cos
, cosh
, sin
, sinh
, tan
, tanh
, acos
, acosh
, asin
, asinh
, atan
, atanh
cospi
, sinpi
, tanpi
, gamma
, lgamma
, digamma
, trigamma
lambertw
, zeta
, dirichlet_eta
, erf
, erfc
atan2
, kronecker_delta
, lowergamma
, uppergamma
, psigamma
, beta
Functions to get symengine logo, version and external libraries built with.
+symengine_version()
+
+symengine_ascii_art()
+
+symengine_have_component(
+ which = c("mpfr", "flint", "arb", "mpc", "ecm", "primesieve", "piranha", "boost",
+ "pthread", "llvm")
+)
+
+symengine_compilation_notes()
A character vector.
Character vector.
+S4 methods of t
defined for Basic
, VecBasic
+and DenseMatrix
.
t(x)
+
+# S4 method for Basic
+t(x)
+
+# S4 method for VecBasic
+t(x)
+
+# S4 method for DenseMatrix
+t(x)
A SymEngine object.
A DenseMatrix
S4 object.
This is a convenient way to initialize variables and assign them in the given +environment.
+use_vars(..., .env = parent.frame(), .quiet = FALSE)
All the arguments will be quoted and parsed, if a argument is named, +the name will be used as the name of variable to assign, otherwise the +argument can only be a symbol.
Environment to assign.
Whether to supress the message.
Invisibly returns a list of assigned variables.
+Miscellaneous S4 methods defined for VecBasic
class.
# S4 method for VecBasic
+length(x)
+
+# S4 method for VecBasic
+rep(x, ...)
+
+# S4 method for Basic
+rep(x, ...)
+
+# S3 method for VecBasic
+unique(x, ...)
+
+# S4 method for BasicOrVecBasic
+c(x, ...)
+
+# S4 method for VecBasic,numeric
+[[(x, i, j, ...)
+
+# S4 method for VecBasic
+[(x, i, j, ..., drop = TRUE)
+
+# S4 method for VecBasic
+[[(x, i) <- value
+
+# S4 method for VecBasic
+[(x, i, j, ...) <- value
Basic object or Vecbasic object.
Arguments for subsetting or replacing.
Same or similar to the generics.
+