Symbolic Algebra with R
R Base
The D Function
\[a * (2 * x)\]
The DD Function
DD <- function(expr, name, order = 1){
if(order < 1) stop("'order' must be >= 1")
if (order == 1) {return(invisible(D(expr, name)))}
else {DD(D(expr, name), name, order = order - 1)}
}
ep3 <- DD(expression(x ^ 4 + a * x ^ 3 + b * x ^ 2 + c * x + d), "x", order = 2)
ep3 <- paste0("$$", deparse(ep3), "$$")
\[4 * (3 * x^2) + a * (3 * (2 * x)) + b * 2\]
Evaluate a Derivative Function
expr <- expression(x^2)
expr_d1 <- D(expr, "x")
eval_expr <- function(expr, b = 5, ...){
args <- list(...)
list2env(args, environment())
eval(expr)
}
ep4 <- paste0("Evaluating ", "$", deparse(expr_d1), "$"
, " at x = 3 "
, "has a value of "
, eval_expr(expr_d1, x = 3)
)
Evaluating \(2 * x\) at x = 3 has a value of 6
R Ryacas Package
- Ref: https://cran.r-project.org/web/packages/Ryacas/Ryacas.pdf
- Ref: https://blog.ephorie.de/doing-maths-symbolically-r-as-a-computer-algebra-system-cas
Simplify a mathmatical function
library(Ryacas)
mf0 <- "2*a*b*a^2/b-a^3"
mf0_s <- paste0("Simplify(", mf0, ")")
mf1_e <- as_r(yac_str(mf0_s))
mf1_s <- paste0("Simplify(", as.character(mf1_e), ")")
mf2_e <- as_r(yac_str(mf1_s))
fml0 <- paste0("$$", mf0, "$$")
fml1 <- paste0("$$", as.character(mf1_e), "$$")
fml2 <- paste0("$$", as.character(mf2_e), "$$")
\[2*a*b*a^2/b-a^3\] \[(b * a^3)/b\] \[a^3\]
Solve an equation
eq <- "y == a + b*x, x"
sv <- as_r(yac_str(paste0("Solve(", eq, ")")))
fml_eq <- paste0("$$", eq, "$$")
fml_sv <- paste0("$$", as.character(sv), "$$")
\[y == a + b*x, x\] \[x==(y-a)/b\]
Expand an expression
ep0 <- "(x - y)^3"
ep1 <- as_r(yac_str(paste0("Expand(", ep0, ")")))
fml_ep0 <- paste0("$$", ep0, "$$")
fml_ep1 <- paste0("$$", ep1, "$$")
\[(x - y)^3\] \[x^3 - 3 * y * x^2 + 3 * y^2 * x - y^3\]
Derivative of a function
eq <- "Pi*x^4 + 2*x^3 - 3*x^2 + 4*x - 5 + x^(1/2) + Ln(x) + Exp(x)"
eq1 <- as_r(yac_str(paste0("D(x, 1)", eq)))
as_latex <- function(str){paste0("$$", str, "$$")}
\[Pi*x^4 + 2*x^3 - 3*x^2 + 4*x - 5 + x^(1/2) + Ln(x) + Exp(x)\] \[4 * pi * x^3 + 6 * x^2 - 6 * x + 1/(2 * sqrt(x)) + 1/x + exp(x) + 4\]
R sessionInfo
R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] Ryacas_1.1.5 Wu_0.0.0.9000 flexdashboard_0.6.1 [4] lme4_1.1-31 Matrix_1.5-3 mgcv_1.8-38
[7] nlme_3.1-152 png_0.1-8 scales_1.2.1
[10] nnet_7.3-16 labelled_2.10.0 kableExtra_1.3.4
[13] plotly_4.10.1 gridExtra_2.3 ggplot2_3.4.1
[16] DT_0.27 tableone_0.13.2 magrittr_2.0.3
[19] lubridate_1.9.2 dplyr_1.1.0 plyr_1.8.8
[22] data.table_1.14.8 rmdformats_1.0.4 knitr_1.42
loaded via a namespace (and not attached): [1] httr_1.4.5 pkgload_1.3.2 sass_0.4.5 tidyr_1.3.0
[5] jsonlite_1.8.4 viridisLite_0.4.1 splines_4.2.0 bslib_0.4.2
[9] assertthat_0.2.1 yaml_2.3.7 pillar_1.8.1 lattice_0.20-45
[13] glue_1.6.2 digest_0.6.29 rvest_1.0.3 minqa_1.2.5
[17] colorspace_2.1-0 htmltools_0.5.4 survey_4.1-1 pkgconfig_2.0.3
[21] haven_2.5.2 bookdown_0.32 purrr_1.0.1 webshot_0.5.4
[25] svglite_2.1.1 timechange_0.2.0 tibble_3.1.8 generics_0.1.3
[29] ellipsis_0.3.2 cachem_1.0.6 withr_2.5.0 klippy_0.0.0.9500 [33] lazyeval_0.2.2 cli_3.6.0 survival_3.2-13 evaluate_0.20
[37] fansi_1.0.4 MASS_7.3-54 forcats_1.0.0 xml2_1.3.3
[41] tools_4.2.0 hms_1.1.2 mitools_2.4 lifecycle_1.0.3
[45] stringr_1.5.0 munsell_0.5.0 compiler_4.2.0 jquerylib_0.1.4
[49] systemfonts_1.0.4 rlang_1.0.6 grid_4.2.0 nloptr_2.0.3
[53] rstudioapi_0.13 htmlwidgets_1.5.4 rmarkdown_2.20 boot_1.3-28
[57] gtable_0.3.1 DBI_1.1.3 R6_2.5.1 fastmap_1.1.0
[61] utf8_1.2.2 stringi_1.7.12 Rcpp_1.0.10 vctrs_0.5.2
[65] tidyselect_1.2.0 xfun_0.37