Learn R Programming

caracas: Computer algebra in R

Installation

caracas is available on CRAN and can be installed as usual:

install.packages('caracas')

Please ensure that you have SymPy installed, or else install it:

if (!caracas::has_sympy()) {
  caracas::install_sympy() 
}

To build and install from Github with vignettes run this command from within R (please install remotes first if not already installed):

# install.packages('remotes')
remotes::install_github("r-cas/caracas", build_vignettes = TRUE)

You can also install the package without vignettes if needed as follows:

remotes::install_github("r-cas/caracas")

Configuring the Python environment

The caracas package uses the reticulate package (to run Python code). Thus, if you wish to configure your Python environment, you need to 1) load reticulate, 2) configure the Python environment, and 3) load caracas. The Python environment can be configured as described here. Again, this need to be done before loading caracas.

Development site

See https://github.com/r-cas/caracas.

Online documentation

See https://r-cas.github.io/caracas/.

Origin of name

The name “caracas” is intended to mean “(inter)face to computer algebra system(s)” - notice that “cara” is Spanish (Castellano to be precise) for “face”.

Code of conduct

Please note that the caracas project is released with a Contributor Code of Conduct (available in CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.

Brief introduction

library(caracas)
# options(caracas.print.prettyascii = TRUE)
x <- symbol('x')
eq <- 2*x^2 - x
eq
#> c:    2    
#>    2⋅x  - x
as.character(eq)
#> [1] "2*x^2 - x"
as_expr(eq)
#> expression(2 * x^2 - x)
tex(eq)
#> [1] "2 x^{2} - x"
solve_sys(eq, x)
#> x = 0
#> x = 1/2
der(eq, x)
#> c: 4⋅x - 1
subs(eq, x, "y")
#> c:    2    
#>    2⋅y  - y
A <- matrix(c("x", 2, 0, "2*x"), 2, 2)
B <- as_sym(A)
B
#> c: ⎡x   0 ⎤
#>    ⎢      ⎥
#>    ⎣2  2⋅x⎦
Binv <- inv(B)
Binv
#> c: ⎡ 1      ⎤
#>    ⎢ ─    0 ⎥
#>    ⎢ x      ⎥
#>    ⎢        ⎥
#>    ⎢-1    1 ⎥
#>    ⎢───  ───⎥
#>    ⎢  2  2⋅x⎥
#>    ⎣ x      ⎦
tex(Binv)
#> [1] "\\left[\\begin{matrix}\\frac{1}{x} & 0\\\\- \\frac{1}{x^{2}} & \\frac{1}{2 x}\\end{matrix}\\right]"
eigenval(Binv)
#> [[1]]
#> [[1]]$eigval
#> c: 1
#>    ─
#>    x
#> 
#> [[1]]$eigmult
#> [1] 1
#> 
#> 
#> [[2]]
#> [[2]]$eigval
#> c:  1 
#>    ───
#>    2⋅x
#> 
#> [[2]]$eigmult
#> [1] 1

Please find more examples in the other vignettes available at https://r-cas.github.io/caracas/.

Contribute, issues, and support

Please use the issue tracker at https://github.com/r-cas/caracas/issues if you want to notify us of an issue or need support. If you want to contribute, please either create an issue or make a pull request.

Copy Link

Version

Install

install.packages('caracas')

Monthly Downloads

610

Version

2.1.1

License

GPL

Issues

Pull Requests

Stars

Forks

Maintainer

Mikkel Andersen

Last Published

November 30th, 2023

Functions in caracas (2.1.1)

diag

Matrix diagonal
add_prefix

Add prefix to each element of matrix
Ops.caracas_symbol

Math operators
as_sym

Convert R object to caracas symbol
as_vec

Stacks matrix to vector
diag.caracas_symbol

Matrix diagonal
factor_

Expand expression
diag_

Symbolic diagonal matrix
fraction_parts

Get numerator and denominator of a fraction
free_symbols

Get free symbol in expression
diff_mat

Difference matrix
generic-matrices

Generate generic vectors and matrices
matrify

Creates matrix from array symbol
expand

Expand expression
matrix-products

Matrix multiplication
special_matrices

Special matrices: zeros_sym, ones_sym, eye_sym
[.caracas_symbol

Extract or replace parts of an object
t.caracas_symbol

Transpose of matrix
expand_func

Expand a function expression
sympy_version

Get 'SymPy' version
Math.caracas_symbol

Math functions
N

Numerical evaluation
kronecker,caracas_symbol,caracas_symbol-method

Kronecker product of two matrices
is_sym

Is object a caracas symbol
jacobian

Compute Jacobian
dim<-.caracas_symbol

Dimensions of a caracas symbol
dim.caracas_symbol

Dimensions of a caracas symbol
lim

Limit of a function
drop_remainder

Remove remainder term
simplify

Simplify expression
eval_to_symbol

Create a symbol from a string
reciprocal_matrix

Elementwise reciprocal matrix
rowSums_colSums

Form Row and Column Sums
prod_

Product of a function
all_vars

All variables
rankMatrix_

Rank of matrix
subs

Substitute symbol for value
to_something

Coerce caracas object
[<-.caracas_symbol

Extract or replace parts of an object
apart

Partial fraction decomposition on a rational function
as.character.caracas_symbol

Convert symbol to character
tuplify

Convert object to tuple
cumsum.caracas_symbol

Cumulative Sums
def_sym

Define (invisibly) caracas symbols in global environment
do_la

Do linear algebra operation
solve.caracas_symbol

Solve a System of Linear Equations
symbol

Create a symbol
symbol_class

Ask type of caracas symbol
doit

Perform calculations setup previously
as_character

Coerce symbol to character
collect

Collects common powers of a term in an expression
colspan

Column space (range) of a symbolic matrix
expand_log

Expand a logarithmic expression
has_sympy

Check if 'SymPy' is available
diag-set

Replace matrix diagonal
expand_trig

Expand a trigonometric expression
matrix_

Symbolic matrix
get_sympy

Access 'SymPy' directly
diag<-.caracas_symbol

Replace diagonal
matrix_cross_product

Matrix cross product
as_character_matrix

Get matrix as character matrix
scale_matrix

Create list of factors as in a product
as_diag

Construct diagonal matrix from vector
as_expr

Convert caracas object to R
as_func

Convert expression into function object.
%>%

Pipe
install_sympy

Install 'SymPy'
linalg

Do linear algebra operation
listify

Convert object to list of elements
int

Integrate a function
solve_lin

Solve a linear system of equations
solve_sys

Solves a system of non-linear equations
print.caracas_scaled_matrix

Print scaled matrix
der

Symbolic differentiation of an expression
score_hessian

Score and Hessian matrix
sum.caracas_symbol

Summation
sum_

Sum of a function
taylor

Taylor expansion
ls_sym

List defined symbols
der2

Symbolic differentiation of second order of an expression
print.caracas_solve_sys_sol

Print solution
mat_pow

Matrix power
get_basis

Get basis
get_py

Access 'py' object
print.caracas_symbol

Print symbol
symbol_is_matrix

Check if object is a caracas matrix
sympy_func

Call a SymPy function directly on x
sym_inherits

Ask if type of caracas symbol is of a requested type
sym_class

Ask type of caracas symbol
tex

Export object to TeX
vectorfy

Creates symbol vector from list of caracas symbols
unbracket

Remove inner-most dimension
unscale_matrix

Extract matrix from scaled matrix
tex.caracas_scaled_matrix

Export scaled matrix to tex
texshow

Dump latex representation of sympy object.
ask

Ask for a symbol's property
cancel

Put rational function into standard form