-
Notifications
You must be signed in to change notification settings - Fork 8
/
README.Rmd
145 lines (104 loc) · 3 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# symengine
<!-- badges: start -->
[![R-CMD-check](https://github.com/symengine/symengine.R/workflows/R-CMD-check/badge.svg)](https://github.com/symengine/symengine.R/actions)
[![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/rr0tdh8ykvs04qg2?svg=true)](https://ci.appveyor.com/project/symengine/symengine-r)
<!-- badges: end -->
`symengine` is an R interface to the [SymEngine C++ library](https://github.com/symengine/symengine)
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
```{r, eval=FALSE}
devtools::install_github("symengine/symengine.R")
```
On Windows, you will need to install [Rtools42](https://cran.r-project.org/bin/windows/Rtools/rtools42/rtools.html)
for building the package from source.
Please report any problem installing the package on your system.
```{r}
library(symengine)
```
## Usage
Also check the documentation site with built vignettes and help pages at
http://symengine.marlin.pub.
### Manipulating Symbolic Expressions
```{r}
use_vars(x, y, z)
expr <- (x + y + z) ^ 2L - 42L
expand(expr)
```
Substitue `z` as `a` and `y` as `x^2`.
```{r}
a <- S("a")
expr <- subs(expr, z, a)
expr <- subs(expr, y, x^2L)
expr
```
Second derivative of `expr` with regards to `x`:
```{r}
d1_expr <- D(expr, "x")
d2_expr <- D(d1_expr, "x")
expand(d2_expr)
```
Solve the equation of `d2_expr == 0` with regards to `x`.
```{r}
solutions <- solve(d2_expr, "x")
solutions
```
### 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.
```{r}
func <- as.function(solutions)
ans <- func(a = -100:-95)
colnames(ans) <- c("Solution1", "Solution2")
ans
```
### Numbers
The next prime number greater than 2^400.
```{r}
n <- nextprime(S(~ 2 ^ 400))
n
```
The greatest common divisor between the prime number and 42.
```{r}
GCD(n, 42)
```
The binomial coefficient `(2^30 ¦ 5)`.
```{r}
choose(S(~ 2^30), 5L)
```
Pi "computed" to 400-bit precision number.
```{r}
if (symengine_have_component("mpfr"))
evalf(Constant("pi"), bits = 400)
```
### Object Equality
```{r}
x + y == S("x + y")
x + y != S("x + y")
```
```{r}
sin(x)/cos(x)
tan(x) == sin(x)/cos(x) # Different internal representation
```
## 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.