Title: | R Interface to the 'Yacas' Computer Algebra System |
---|---|
Description: | Interface to the 'yacas' computer algebra system (<http://www.yacas.org/>). |
Authors: | Mikkel Meyer Andersen [aut, cre, cph], Rob Goedman [aut, cph], Gabor Grothendieck [aut, cph], Søren Højsgaard [aut, cph], Grzegorz Mazur [aut, cph], Ayal Pinkus [aut, cph], Nemanja Trifunovic [cph] (UTF-8 part of yacas (src/yacas/include/yacas/utf8*)) |
Maintainer: | Mikkel Meyer Andersen <[email protected]> |
License: | GPL |
Version: | 1.1.5 |
Built: | 2024-11-07 04:28:51 UTC |
Source: | https://github.com/r-cas/ryacas |
Ryacas allows one to use the yacas computer algebra package entirely from within R.
Please read the "Getting started" vignette.
Extract or replace parts of an object
## S3 method for class 'yac_symbol' x[i, j]
## S3 method for class 'yac_symbol' x[i, j]
x |
A |
i |
row indices specifying elements to extract or replace |
j |
column indices specifying elements to extract or replace |
Extract parts of an object
## S3 method for class 'yac_symbol' x[[i]]
## S3 method for class 'yac_symbol' x[[i]]
x |
A |
i |
indices specifying elements to extract |
Extract or replace parts of an object
## S3 replacement method for class 'yac_symbol' x[i, j] <- value
## S3 replacement method for class 'yac_symbol' x[i, j] <- value
x |
A |
i |
row indices specifying elements to extract or replace |
j |
column indices specifying elements to extract or replace |
value |
the value to replace |
Matrix multiplication
x %*% y
x %*% y
x |
A |
y |
A |
If x
is a yacas command as string, convert to a character vector/matrix in R.
If x
is a yac_symbol
(e.g. from ysym()
), then convert it to a numeric object
if there are no variables or a character type if there are variables.
as_r(x)
as_r(x)
x |
yacas list or list of lists to convert |
In yacas a vector is a list, and a matrix is a list of lists.
Convert R vector/matrix to yacas vector (list) or matrix (list of lists)
as_y(x)
as_y(x)
x |
R vector to convert |
Convert yac symbol to character
## S3 method for class 'yac_symbol' as.character(x, ...)
## S3 method for class 'yac_symbol' as.character(x, ...)
x |
A |
... |
not used |
Combine R Objects by Columns
## S3 method for class 'yac_symbol' cbind(..., deparse.level = 1)
## S3 method for class 'yac_symbol' cbind(..., deparse.level = 1)
... |
Objects to bind |
deparse.level |
Not used |
Find the derivative of yac symbol
## S3 method for class 'yac_symbol' deriv(expr, ...)
## S3 method for class 'yac_symbol' deriv(expr, ...)
expr |
A |
... |
variables as character vector to take derivate with respect to |
From base::det()
.
det(x, ...)
det(x, ...)
x |
If |
... |
further arguments passed to |
(x <- matrix(1:4, ncol = 2)) det(x) det(ysym(x))
(x <- matrix(1:4, ncol = 2)) det(x) det(ysym(x))
From base::diag()
.
diag(x, ...)
diag(x, ...)
x |
If |
... |
further arguments passed to |
From base::diag()
.
diag(x) <- value
diag(x) <- value
x |
If |
value |
New value for |
Find the Hessian matrix of yac symbol
Hessian(expr, ...)
Hessian(expr, ...)
expr |
A |
... |
variables as character vector to take Hessian with respect to |
If f
is a yac_symbol
, yacas
's Integrate()
is used.
Else, stats::integrate()
is used.
integrate(f, ...)
integrate(f, ...)
f |
Function to integrate. See details. |
... |
See details. |
Additional arguments:
yac_symbol
: var
, lower
, upper
Else (stats::integrate()
): lower
, upper
Find the Jacobian matrix of yac symbol
Jacobian(expr, ...)
Jacobian(expr, ...)
expr |
A |
... |
variables as character vector to take Jacobian with respect to |
If first argument is a yac_symbol
, yacas
's Limit()
is used.
lim(...)
lim(...)
... |
See details. |
Arguments:
yac_symbol
: f
, var
, val
, from_left
, from_right
Lower and upper triangular part of a matrix
lower.tri(x, diag = FALSE)
lower.tri(x, diag = FALSE)
x |
If |
diag |
Whether diagonal is included. |
Math functions
## S3 method for class 'yac_symbol' Math(x, ...)
## S3 method for class 'yac_symbol' Math(x, ...)
x |
|
... |
further arguments passed to methods |
Math operators
## S3 method for class 'yac_symbol' Ops(e1, e2)
## S3 method for class 'yac_symbol' Ops(e1, e2)
e1 |
A |
e2 |
A |
Matrix Power
pow(x, n, ...) ## Default S3 method: pow(x, n, ...)
pow(x, n, ...) ## Default S3 method: pow(x, n, ...)
x |
If |
n |
|
... |
further arguments passed to |
(x <- matrix(c(1, 2, 2, 3), ncol = 2)) pow(x, 2) pow(ysym(x), 2)
(x <- matrix(c(1, 2, 2, 3), ncol = 2)) pow(x, 2) pow(ysym(x), 2)
Product of Vector Elements
## S3 method for class 'yac_symbol' prod(expr, ..., na.rm = FALSE)
## S3 method for class 'yac_symbol' prod(expr, ..., na.rm = FALSE)
expr |
Expression to be multiplied |
... |
Not used |
na.rm |
Not used |
Combine R Objects by Rows
## S3 method for class 'yac_symbol' rbind(..., deparse.level = 1)
## S3 method for class 'yac_symbol' rbind(..., deparse.level = 1)
... |
Objects to bind |
deparse.level |
Not used |
Simplify expression
simplify(x, timeout = 2)
simplify(x, timeout = 2)
x |
A |
timeout |
timeout in seconds before simplification is aborted; only works when package unix is available |
This generic function solves the equation $a x = b$ for $x$.
## S3 method for class 'yac_symbol' solve(a, b, ...)
## S3 method for class 'yac_symbol' solve(a, b, ...)
a |
A |
b |
A |
... |
See details and examples. |
When a
is a matrix and b
not provided,
this finds the inverse of a
.
When a
is a matrix and a vector b
is provided, the
linear system of equations is solved.
Note that solving non-linear equations:
solve(a, b)
: find roots of a
for variable b
, i.e. yacas Solve(a == 0, b)
solve(a, b, v)
: find solutions to a == b
for variable v
, i.e. yacas Solve(a == b, v)
This also works for a system of equations (when a
is a vector)
A <- outer(0:3, 1:4, "-") + diag(2:5) a <- 1:4 B <- ysym(A) b <- ysym(a) solve(A) solve(B) solve(A, a) solve(B, b) poly <- ysym("x^2 - x - 6") solve(poly, "x") # Solve(poly == 0, x) solve(poly, 3, "x") # Solve(poly == 3, x)
A <- outer(0:3, 1:4, "-") + diag(2:5) a <- 1:4 B <- ysym(A) b <- ysym(a) solve(A) solve(B) solve(A, a) solve(B, b) poly <- ysym("x^2 - x - 6") solve(poly, "x") # Solve(poly == 0, x) solve(poly, 3, "x") # Solve(poly == 3, x)
If only expr
given: sum elements.
## S3 method for class 'yac_symbol' sum(expr, var, lower, upper, ..., na.rm = FALSE)
## S3 method for class 'yac_symbol' sum(expr, var, lower, upper, ..., na.rm = FALSE)
expr |
Expression to be summed |
var |
Variable to sum |
lower |
Lower limit |
upper |
Upper limit |
... |
Not used |
na.rm |
Not used |
Else: sums expr
by letting var
taking values
from lower
to upper
(potentially Inf
)
The trace of a square matrix is the sum of the diagonal elements.
tr(x, ...) ## Default S3 method: tr(x, ...)
tr(x, ...) ## Default S3 method: tr(x, ...)
x |
If |
... |
further arguments passed to |
(x <- matrix(1:4, ncol = 2)) tr(x) tr(ysym(x))
(x <- matrix(1:4, ncol = 2)) tr(x) tr(ysym(x))
Lower and upper triangular part of a matrix
upper.tri(x, diag = FALSE)
upper.tri(x, diag = FALSE)
x |
If |
diag |
Whether diagonal is included. |
Vectorize
vec(x, ...) ## Default S3 method: vec(x, ...)
vec(x, ...) ## Default S3 method: vec(x, ...)
x |
If |
... |
further arguments passed to |
(x <- matrix(1:9, ncol = 3)) vec(x) vec(ysym(x))
(x <- matrix(1:9, ncol = 3)) vec(x) vec(ysym(x))
Half-Vectorize
vech(x, ...) ## Default S3 method: vech(x, ...)
vech(x, ...) ## Default S3 method: vech(x, ...)
x |
If |
... |
further arguments passed to |
A <- mtcars[, c(1, 3, 4, 5, 6, 7)] x <- cov(A) vech(x) vech(ysym(x))
A <- mtcars[, c(1, 3, 4, 5, 6, 7)] x <- cov(A) vech(x) vech(ysym(x))
Give a variable a value
with_value(x, var, val)
with_value(x, var, val)
x |
yac_symbol |
var |
Variable |
val |
Value |
Evaluate a yacas expression by replacing variables with values as for the given list.
y_eval(expr, ..., as.r = FALSE)
y_eval(expr, ..., as.r = FALSE)
expr |
a valid yacas expression |
... |
a list of assignements (see example) |
as.r |
if TRUE, then the expression is evaluated as R (if any variable to be substituted in the expression is a vector, then a vector is returned). If it is FALSE (default), a yacc expression is returned, replacing scalar variables. |
# Evaluate as yacas object eq <- ysym("2*y+x^2+2*x-3") y_eval(eq, x=3, y=2) # Evaluate as R expression: y_eval(eq, x=3, y=2, as.r=TRUE) # This allows to use vectors: y_eval(eq, x=1:10, y=2, as.r=TRUE) # and to plot functions: curve(y_eval(eq, x=x, y=2, as.r=TRUE), xlim=c(0,10))
# Evaluate as yacas object eq <- ysym("2*y+x^2+2*x-3") y_eval(eq, x=3, y=2) # Evaluate as R expression: y_eval(eq, x=3, y=2, as.r=TRUE) # This allows to use vectors: y_eval(eq, x=1:10, y=2, as.r=TRUE) # and to plot functions: curve(y_eval(eq, x=x, y=2, as.r=TRUE), xlim=c(0,10))
yacas
callPrepare simple yacas
call
y_fn(x, fn, ...)
y_fn(x, fn, ...)
x |
parameter to function |
fn |
function with parameter |
... |
additional arguments to |
y_fn("x^2 - 1", "TeXForm") yac_str(y_fn("x^2 - 1", "TeXForm")) y_fn("x^2 - 1", "Factor") yac_str(y_fn("x^2 - 1", "Factor")) cmd <- "x^2 - 1 == 0" %>% y_fn("Solve", "x") cmd sol <- yac_str(cmd) sol yac_str(y_rmvars(sol))
y_fn("x^2 - 1", "TeXForm") yac_str(y_fn("x^2 - 1", "TeXForm")) y_fn("x^2 - 1", "Factor") yac_str(y_fn("x^2 - 1", "Factor")) cmd <- "x^2 - 1 == 0" %>% y_fn("Solve", "x") cmd sol <- yac_str(cmd) sol yac_str(y_rmvars(sol))
Pretty print yacas strings
y_print(x)
y_print(x)
x |
yacas string, e.g. a matrix |
A <- diag(4) Ayac <- as_y(A) y_print(Ayac) B <- A B[2, 2] <- "-t" Byac <- as_y(B) Byac y_print(Byac)
A <- diag(4) Ayac <- as_y(A) y_print(Ayac) B <- A B[2, 2] <- "-t" Byac <- as_y(B) Byac y_print(Byac)
This only builds a yacas command.
You need to also call yac_str()
, yac_expr()
or similar.
This is the reason that it does not call yacas: it depends on how you
want it returned (string, expression).
y_rmvars(x)
y_rmvars(x)
x |
yacas command |
cmd <- "{x == 2, y == 4}" yac_str(cmd) yac_str(y_rmvars(cmd))
cmd <- "{x == 2, y == 4}" yac_str(cmd) yac_str(y_rmvars(cmd))
Run yacas command
yac(x, rettype = c("str", "expr", "silent"))
yac(x, rettype = c("str", "expr", "silent"))
x |
yacas command |
rettype |
|
yac("D(x) x^2 + 4*x") yac("D(x) x^2 + 4*x", rettype = "str") yac("D(x) x^2 + 4*x", rettype = "expr") yac("D(x) x^2 + 4*x", rettype = "silent")
yac("D(x) x^2 + 4*x") yac("D(x) x^2 + 4*x", rettype = "str") yac("D(x) x^2 + 4*x", rettype = "expr") yac("D(x) x^2 + 4*x", rettype = "silent")
Assign yacas variable
yac_assign(value, x)
yac_assign(value, x)
value |
Expression |
x |
Variable name |
Interactive interface to the yacas
yac_cli(enable_history = TRUE)
yac_cli(enable_history = TRUE)
enable_history |
Use |
The user types valid yacas input and presses return. Type 'quit' to return to R prompt.
Output of yacas is returned.
Note that command will use R history()
and modify it by default.
Yacas is given a limited amount of time to complete, otherwise
\[1\] CommandLine(1) : User interrupted calculation
is returned. E.g.
Taylor(x,0,5) 1/(1+x) will work, but Taylor(x,0,12) 1/(1+x) is likely to
take too long.
## Not run: yac_cli() (x+y)^3-(x-y)^3 Simplify(%) q ## End(Not run)
## Not run: yac_cli() (x+y)^3-(x-y)^3 Simplify(%) q ## End(Not run)
Run yacas command returning R expression
yac_expr(x)
yac_expr(x)
x |
yacas command |
yac_expr("D(x) x^2 + 4*x") yac_expr("Limit(x, 1) (x^2 - 1)/(x - 1)") yac_expr("Sum(n, 1, Infinity, (1/2)^n)") yac_expr("Fibonacci(10)") yac_expr("Sum(n, 1, 10, Fibonacci(n))")
yac_expr("D(x) x^2 + 4*x") yac_expr("Limit(x, 1) (x^2 - 1)/(x - 1)") yac_expr("Sum(n, 1, Infinity, (1/2)^n)") yac_expr("Fibonacci(10)") yac_expr("Sum(n, 1, 10, Fibonacci(n))")
Run yacas command silently
yac_silent(x)
yac_silent(x)
x |
yacas command |
Run yacas command returning string/character
yac_str(x)
yac_str(x)
x |
yacas command |
yac_str("D(x) x^2 + 4*x") yac_str("Limit(x, 1) (x^2 - 1)/(x - 1)") yac_str("Sum(n, 1, Infinity, (1/2)^n)") yac_str("Fibonacci(10)") yac_str("Sum(n, 1, 10, Fibonacci(n))") yac_str("TeXForm(x^2 - 1)")
yac_str("D(x) x^2 + 4*x") yac_str("Limit(x, 1) (x^2 - 1)/(x - 1)") yac_str("Sum(n, 1, Infinity, (1/2)^n)") yac_str("Fibonacci(10)") yac_str("Sum(n, 1, 10, Fibonacci(n))") yac_str("TeXForm(x^2 - 1)")
This is an alias for ysym()
.
See description there.
yac_symbol(x)
yac_symbol(x)
x |
A vector or a matrix |
A yac_symbol
Note that this results in multiple calls to yacas
and
the performance may be slower than manually using e.g. yac_str()
.
ysym(x)
ysym(x)
x |
A vector or a matrix |
A yac_symbol
List defined yac_symbols
ysym_ls(print_details = FALSE)
ysym_ls(print_details = FALSE)
print_details |
print content of symbols |