Title: | Legacy 'Ryacas' (Interface to 'Yacas' Computer Algebra System) |
---|---|
Description: | A legacy version of 'Ryacas', an 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-2 |
Version: | 0.4.4 |
Built: | 2024-11-11 05:16:24 UTC |
Source: | https://github.com/r-cas/ryacas0 |
Ryacas0 allows one to use the yacas computer algebra package entirely from
within R. It takes an R expression, an R one line function or a yacas
string and returns an R expression or a variety of other formats. It can be
used for symbolic mathematics, exact arithmetic, ASCII pretty printing and R
to TeX conversions. The main command is yacas
and ?yacas
provides some information on installation and startup.
The following are sources of information on "Ryacas0"
:
DESCRIPTION file | library(help = Ryacas0) |
List of demo files | demo(package = "Ryacas0") |
Demo file | demo("Ryacas0") |
Demo | demo("Ryacas0-PrettyPrinter") |
Demo | demo("Ryacas0-Function") |
Demo | demo("Ryacas0-Sym") |
Demo | demo("Ryacas0-Expr") |
List Vignettes | vignette(package = "Ryacas0") |
Vignette | vignette("Ryacas0") |
This File | package?Ryacas0 |
Help files | ?yacas, ?yacasTranslations, ?yacmode, ?Sym |
Help files - Windows | ?yacasInstall |
News | RShowDoc("NEWS", package = "Ryacas0") |
Acknowledgements | RShowDoc("THANKS", package = "Ryacas0") |
Wish List | RShowDoc("WISHLIST", package = "Ryacas0") |
Home page | https://github.com/r-cas/ryacas0/ |
There is a note in the help file of the yacas command that discusses a number of installation and startup issues.
print(yacas(expression(integrate(1/x, x)))) print(yacas("Integrate(x)1/x")) x <- Sym("x"); Integrate(1/x, x) acos(Sym("1/2"))
print(yacas(expression(integrate(1/x, x)))) print(yacas("Integrate(x)1/x")) x <- Sym("x"); Integrate(1/x, x) acos(Sym("1/2"))
Simple and raw conversion to yacas
## S3 method for class 'character' as.Sym(x, ...)
## S3 method for class 'character' as.Sym(x, ...)
x |
An R character vector. |
... |
Not used |
x <- c("a", "2", "4", "c", "d", "6") x y <- as.Sym(x) y Eval(y, list(a = 3, c = 3, d = 3))
x <- c("a", "2", "4", "c", "d", "6") x y <- as.Sym(x) y Eval(y, list(a = 3, c = 3, d = 3))
Simple and raw conversion to yacas
## S3 method for class 'matrix' as.Sym(x, ...)
## S3 method for class 'matrix' as.Sym(x, ...)
x |
An R character matrix. |
... |
Not used |
x <- matrix(c("a", "2", "4", "c", "d", "6"), 3, 2) x y <- as.Sym(x) y Eval(y, list(a = 3, c = 3, d = 3))
x <- matrix(c("a", "2", "4", "c", "d", "6"), 3, 2) x y <- as.Sym(x) y Eval(y, list(a = 3, c = 3, d = 3))
Get body of function as an expression.
bodyAsExpression(x)
bodyAsExpression(x)
x |
An R function. |
This function is similar to the R body
function except that function
returns a call object whereas this one returns an expression usable in
Ryacas calculations.
An expression.
# construct an R function for the Burr probability density # function (PDF) given the Burr cumulative distribution function (CDF) BurrCDF <- function(x, c = 1, k = 1) 1-(1+x^c)^-k # transfer CDF to yacas yacas(BurrCDF) # create a template for the PDF from the CDF BurrPDF <- BurrCDF # differentiate CDF and place resulting expression in body body(BurrPDF) <- yacas(expression(deriv(BurrCDF(x,c,k))))[[1]] # test out PDF BurrPDF(1)
# construct an R function for the Burr probability density # function (PDF) given the Burr cumulative distribution function (CDF) BurrCDF <- function(x, c = 1, k = 1) 1-(1+x^c)^-k # transfer CDF to yacas yacas(BurrCDF) # create a template for the PDF from the CDF BurrPDF <- BurrCDF # differentiate CDF and place resulting expression in body body(BurrPDF) <- yacas(expression(deriv(BurrCDF(x,c,k))))[[1]] # test out PDF BurrPDF(1)
Evaluate a yacas expression.
Eval(x, env = parent.frame(), ...)
Eval(x, env = parent.frame(), ...)
x |
Object to be evaluated. |
env |
Environment or list in which to perform evaluation. |
... |
Not currently used. |
Eval(yacas(expression(x*x)), list(x=2)) # same x <- 2 Eval(yacas(expression(x*x)))
Eval(yacas(expression(x*x)), list(x=2)) # same x <- 2 Eval(yacas(expression(x*x)))
Get width of yacas output
get_output_width()
get_output_width()
Sym()
objectsLists all Sym()
objects in the global environment (.GlobalEnv
)
getSyms(all.names = FALSE)
getSyms(all.names = FALSE)
all.names |
a logical value. If |
getSyms() xs <- Sym("x") getSyms()
getSyms() xs <- Sym("x") getSyms()
The $y$'th root of $x$, i.e. $x^(1/y)$.
root(x, y)
root(x, y)
x |
Number to take $y$'th root of |
y |
Root |
The root y'th root of x
Set or get options for the Ryacas package
Ryacas_options(...)
Ryacas_options(...)
... |
Option names to retrieve option values or |
The following options are supported
module_matvec_enabled
(default TRUE
): Print yacas List()
's as
vectors and List(List(), ...)
's as matrices.
prettyform_default
(default FALSE
): Print yacas as PrettyForm()
as default.
Set width of yacas output
set_output_width(w)
set_output_width(w)
w |
Width in number of characters |
Yacas' Solve(eq, x)
can return e.g.
x == expr
and {x == expr1, x == expr2, ...}
.
Some usages are easier if the initial x ==
part is removed.
This is the purpose of this function.
stripvar(expr, var)
stripvar(expr, var)
expr |
Expression where |
var |
Name of variable, e.g. |
Expression with left-hand side removed
Similar to yacas()
but silent.
This can be useful when working with yacas directly.
syacas(x, ...)
syacas(x, ...)
x |
A yacas character string or an R expression without terminating semicolon to be processed by yacas. |
... |
Additional arguments ultimately passed down to
|
yacas
The Symbol interface to yacas.
Sym(...) Expr(x)
Sym(...) Expr(x)
... |
An R character string or object that can be coerced to a character string. |
x |
An R expression. |
An object of class "Sym"
is internally a yacas character string. An
object of class "Expr"
is internally an R expression. One can combine
such objects using the Math and Ops R operators (see help(Math) and
help(Ops) for a list). Also there are methods for a number of R generics:
as.character.Sym
, as.expression.Sym
, determinant.Sym
,
deriv.Sym
and print.Sym
and yacas-oriented functions: Clear,
Conjugate, Expand, Factor, Factorial, I, Identity, Infinity, Integrate,
Inverse, InverseTaylor, Limit, List, N, Newton, Pi, Precision, PrettyForm,
PrettyPrinter, Set, Simplify, Solve, Subst, Taylor, TeXForm, Transpose,
Ver and "%Where%" all of which have the same meaning as the corresponding yacas
commands. Try vignette("Rycas-Sym")
for many examples.
Get Sym
objects with getSyms()
.
Sym
returns a "Sym"
object and Expr
returns an
"Expr"
object.
Currently the only Expr
methods implemented are
as.character.Expr, deriv.Expr, Math.Expr, Ops.Expr and print.Expr.
x <- Sym("x") x*x Integrate(x*x, x) Sym("%") %Where% list(x = 10) acos(Sym("1/2")) y <- Exprq(x) y*y deriv(y*y, y) Exprq(acos(1/2))
x <- Sym("x") x*x Integrate(x*x, x) Sym("%") %Where% list(x = 10) acos(Sym("1/2")) y <- Exprq(x) y*y deriv(y*y, y) Exprq(acos(1/2))
Get Yacas variables
y_ls()
y_ls()
Vector of variables defined in yacas
Interface to the yacas computer algebra system.
yacas(x, ...) ## S3 method for class 'character' yacas(x, verbose = FALSE, method, retclass = c("expression", "character", "unquote"), addSemi = TRUE, ...)
yacas(x, ...) ## S3 method for class 'character' yacas(x, verbose = FALSE, method, retclass = c("expression", "character", "unquote"), addSemi = TRUE, ...)
x |
A yacas character string or an R expression without terminating semicolon to be processed by yacas. |
... |
Additional arguments ultimately passed down to
|
verbose |
A logical value indicating verbosity of output or
|
method |
method used to communicate with yacas. If |
addSemi |
If |
retclass |
The class of the first component of the yacas structure. It
defaults to |
The user supplies an R expression, an R function name corresponding to a function with a single line body, a formula or a yacas input string. In the case of a formula it is regarded as an expression represented by the right hand side of the formula while the left hand side, if any, is ignored.
Note the silent version syacas()
.
An R object of class "yacas"
is returned. If
PrettyPrinter("OMForm")
is in effect, which it is by default, then
the first component is an R expression and the OMForm
component
contains OpenMath XML code. In other cases the first component is NULL and
the YacasForm
or PrettyForm
components have display
information.
Generally an expression. Refer to details.
Windows Installation. On Windows one can install Ryacas
by
issuing the commands:
install.packages("Ryacas", dep = TRUE)
|
library(Ryacas)
|
yacasInstall()
|
or by using the Packages | Install package(s)
menu in place of the
first command. The second command downloads scripts.dat
and
yacas.exe
from the internet and installs them into
R_HOME/library/Ryacas/yacdir
where R_HOME
is the location of
your R
installation.
Normally the default locations of yacas, its initialization file and the
scripts file are sufficient but, if necessary, they can be overridden via
the environment variables: YACAS_HOME
, YACAS_INIT
and
YACAS_SCRIPTS
. The YACAS_INVOKE_STRING
environment variable
discussed in the next section overrides all three of these.
All OS Installation. The YACAS_INVOKE_STRING
environment variable
can be used to override the invocation string for yacas. Normally it is not
used. If it does need to be used then a typical use might be:
library(Ryacas)
|
# only need to do the file.copy
command once
|
file.copy(system.file("yacdir/R.ys", package =
"Ryacas"), "~/.yacsrc")
|
# this needs to be done once per
session
|
Sys.setenv(YACAS_INVOKE_STRING = "yacas -pc --server
9734")
|
demo(Ryacas) # test it out
|
yacmode. There is also a utility yacmode which is called without arguments and just turns R into a terminal into yacas until one quits out of it (and back to R) by entering stop, end, quit, exit or e.
Startup. yacas
starts up when yacasStart()
is called or the
first time yacas
is called. yacas is shut down when
yacasStop()
is called or when the package is detached using the
detach()
R command. On Windows, when yacas is shut down, the yacas
process is terminated on Windows XP Pro but not on other versions of
Windows. In those cases there will be a dangling process that the user must
terminate manually.
Translation. The translation process occurs in several steps. If the input
to the yacas
function is an expression then it is translated to a
valid yacas character string (otherwise, it is sent to yacas unprocessed).
Yacas then processes the string and if retclass="expression"
it is
translated back to an R expression (otherwise it is sent back unprocessed).
Examples of translations are:
R | yacas |
sin(x) | Sin(x) |
deriv(sin, x) | Deriv(x)Sin(x) |
log(x) | Ln(x) |
yacas(expression(Factor(x^2-1))) exp1 <- expression(x^2 + 2 * x^2) exp2 <- expression(2 * exp0) exp3 <- expression(6 * pi * x) exp4 <- expression((exp1 * (1 - sin(exp3))) / exp2) print(yacas(exp4)) print(yacas("Version()")) # yacas version # see demo("Ryacas-Function")
yacas(expression(Factor(x^2-1))) exp1 <- expression(x^2 + 2 * x^2) exp2 <- expression(2 * exp0) exp3 <- expression(6 * pi * x) exp4 <- expression((exp1 * (1 - sin(exp3))) / exp2) print(yacas(exp4)) print(yacas("Version()")) # yacas version # see demo("Ryacas-Function")
This is a low-level function for evaluating yacas expression represented as string.
yacas_evaluate(expr)
yacas_evaluate(expr)
expr |
Yacas expression |
Result of evaluating expr
by yacas in OpenMath format and
side-effects of the evaluation
yacas_evaluate("D(x)Sin(x^2)")
yacas_evaluate("D(x)Sin(x^2)")
Translations from R to the yacas computer algebra system.
The translation process occurs in several steps. If the input to the
yacas
function is an expression then it is translated to a valid
yacas character string (otherwise, it is sent to yacas unprocessed). Yacas
then processes the string and if retclass="expression"
it is
translated back to an R expression (otherwise it is sent back unprocessed).
Currently supported translations are:
CONSTANTS | ||
R | yacas | |
= | ===== | |
pi | Pi | |
OPERATORS | ||
R | yacas | |
= | ===== | |
7 \%\% 3 | Mod(7, 3) | |
7 \%\/\% 3 | Div(7, 3) | |
FUNCTIONS | ||
R | yacas | |
= | ===== | |
sin(x) | Sin(x) | |
cos(x) | Cos(x) | |
tan(x) | Tan(x) | |
asin(x) | ArcSin(x) | |
acos(x) | ArcCos(x) | |
atan(x) | ArcTan(x) | |
exp(x) | Exp(x) | |
sqrt(x) | Sqrt(x) | |
log(x) | Ln(x) | |
choose(n, k) | Bin(n, k) | |
gamma(x) | Gamma(x) | |
deriv(sin, x) | Deriv(x)Sin(x) | |
integrate(f, a, b) | Integrate(x, a, b)f(x) | |
list() | List() | |
factorial(n) | n! | |
Note the Limit example in demo(Ryacas0) for adding translations on the fly.
The complete table under development.
Rob J Goedman
Interactive interface to the yacas
yacmode(enable_history = TRUE)
yacmode(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.
invisible NULL
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: yacmode() (x+y)^3-(x-y)^3 Simplify(%) q ## End(Not run)
## Not run: yacmode() (x+y)^3-(x-y)^3 Simplify(%) q ## End(Not run)