\documentclass[]{article} 

	
\usepackage[utf8]{inputenc}
\usepackage{hyperref,a4,color,boxedminipage,Sweave}
%\VignetteIndexEntry{Introduction to Ryacas}
%\VignettePackage{Ryacas}
%\VignetteDepends{XML}

% Definitions
\newcommand{\slan}{{\tt S}}
\newcommand{\rlan}{{\tt R}}
\newcommand{\ryacas}{{\tt Ryacas}}
\newcommand{\yacas}{{\tt yacas}}
\newcommand{\code}[1]{{\tt #1}}
\def\yac{\texttt{Yacas}}
\def\R{\texttt{R}}
\def\sym{\texttt{Sym}}

\setlength{\parindent}{0in}
\setlength{\textwidth}{140mm}
\setlength{\oddsidemargin}{10mm}

\title{\ryacas{} -- an \rlan{} interface to the \yacas{} computer
  algebra system}
\author{Rob Goedman, \and
Gabor Grothendieck,  \and
Sren Hjsgaard,  \and
Ayal Pinkus}


\begin{document}
\maketitle
\tableofcontents
\parskip5pt

<<echo=FALSE, results=hide>>=
library(Ryacas)
options(width=95)
@

\renewenvironment{Schunk}{\begin{center}
    \small
    \begin{boxedminipage}{0.95\textwidth}}{
    \end{boxedminipage}\end{center}}

%     \RecustomVerbatimEnvironment{Sinput}{Verbatim}%
%         {fontsize=\small,frame=single,framerule=1pt,
%           rulecolor=\color{red},   fillcolor=\color{yellow} }
%     \RecustomVerbatimEnvironment{Soutput}{Verbatim}%
%         {fontsize=\scriptsize, frame=single,framerule=0.1pt}


\section{Introduction} 
\label{sec:introduction}

\ryacas{} makes the \yacas{} computer algebra system available from
within \rlan. The name \yacas{} is short for ``Yet Another Computer Algebra
System''. The \yacas{} program 
is developed by Ayal Pinkhuis (who is also the maintainer)
and others, and is available at
\href{http://yacas.sourceforge.net}{http://yacas.sourceforge.net} for various
platforms. There is a comprehensive documentation (300+ pages) of
\yacas{} (also available at
\href{http://yacas.sourceforge.net}{http://yacas.sourceforge.net}) and the
documentation contains many examples.

% The examples given here are largely taken from the \yacas{}
% documentation (especially from the introductory chapter) but are
% organised differently.



\section{\R\ expressions,  \yacas\ expressions and \sym\ objects} 
\label{sec:hood}

The \ryacas{} package works by sending ``commands''
to \yacas{} which makes the calculations and returns the result
to \R{}. There are various different formats of the return value as well

\subsection{\R\ expressions}

A call to \yacas\ may be in the form of an \R\ expression which
involves valid R calls, symbols or constants (though not all valid \R\
expressions are valid). For example:
@ 
<<print=T>>=
exp1<- yacas(expression(Factor(x^2-1)))
@ %def 

The result \code{exp1} is not an expression in the \R\ sense but an
object of class \code{"yacas"}. 
To evaluate the resulting expression numerically, we can do
@ 
<<>>=
Eval(exp1, list(x=4))
@ %def 

\subsection{\yacas\ expressions}

Some commands are not proper \R\ expressions. For example, 
typing 
\begin{verbatim}
  yacas(expression(D(x)Sin(x)))
\end{verbatim}
produces an error. 
For such cases we can make
a specification using the \yacas{} syntax:
@ 
<<>>=
yacas("D(x)Sin(x)")
@ %def 


\subsection{\sym\ objects}

Probably the most elegant way of working with \yacas\ is by using
\sym\ objects.
A \sym\ object is a \yacas\ character string that has the "Sym" class.
One can combine \sym\ objects with other \sym\ objects as well as to
other \R\ objects using \code{+}, \code{-} and other similar \R\
operators.

The function \code{Sym(x)} coerces an object \code{x} to a \sym\ object by
first coercing it to character and then changing its class to "Sym":
@ 
<<print=T>>=
x<- Sym("x")
@ %def 

Operations on \sym\ objects lead to new \sym\ objects:
@ 
<<print=T>>=
x+4
@ %def 

One can apply \code{sin}, \code{cos}, \code{tan}, \code{deriv}, \code{Integrate}
and other provided functions to \sym\ objects. For example:
@ 
<<>>=
Integrate(sin(x), x)
@ %def 
In this way the communication with \yacas\ is ``tacit''.

It is important to note the difference between the \R\ name \code{x}
and the symbol \code{"x"} as illustrated below:
@ 
<<print=T>>=
x<- Sym("xs")
x
x+4
Eval(x+4, list(xs=5))
@ %def 

The convention in the following is 1) that \sym\ objects match with
their names that they end with an 's', e.g.
@ 
<<>>=
xs <- Sym('xs')
@ %def 


\section{A sample session} 
\label{sec:samplesession}

Algebraic calculations:
@ 
<<>>=
yacas(expression((10 + 2) * 5 + 7^7))
yacas(expression(1/14+5/21* (30- 1+ 1/2)))
@ %def 

@ 
<<>>=
#(Sym(10) + Sym(2)) * Sym(5) + Sym(7) ^ Sym(7)
Sym("10 * 2") * 5 + Sym(7) ^ 7
#(Sym(10) + 2) * 5 + Sym(7) ^ 7
#Sym("(10+2)*5 + 7^7")
Sym("1/14 + 5/21 * (30 - 1+1/2)")
@ %def 


Numerical evaluations:
@ 
<<>>=
yacas(expression(N(-12/2)))
@ %def 
@ 
<<>>=
Sym("-12/2")
#Eval(Sym("-12/2"))
@ %def 


Symbolic expressions:
<<>>=
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)
yacas(exp4)
@ 

@ 
<<>>=
Factor(xs^2-1)
exp1 <- xs^2 + 2 * xs^2
exp0 <- Sym("exp0")
exp2 <- 2 * Sym(exp0)
exp3 <- 6 * Pi * xs
exp4 <- exp1 * (1 - sin(exp3)) / exp2
exp4
@ %def 



Combining symbolic and numerical expressions:
@ 
<<>>=
yacas(expression(N(Sin(1)^2 + Cos(x)^2)))
@ %def 

@ 
<<>>=
N(sin(1)^2 + cos(xs)^2)
@ %def 


Differentiation:
<<>>=
yacas("D(x)Sin(x)")
@
@ 
<<>>=
deriv(sin(xs), xs)
@ %def 

Integration: 
@ 
<<echo=F,results=hide>>=
yacas("Clear(a,b,A,B)")
@ %def 

@ 
<<>>=
yacas("Integrate(x,a,b)Sin(x)")
@ %def 

@ 
<<>>=
as <- Sym("as")
bs <- Sym("bs")
Integrate(sin(xs), xs, as, bs)
@ %def 


Expanding polynomials:
@ 
<<>>=
yacas("Expand((1+x)^3)")
@ %def 

@ 
<<>>=
Expand((1+xs)^3)
@ %def 

Taylor expansion:
@ 
<<>>=
yacas("texp := Taylor(x,0,3) Exp(x)")
@ %def 

@ 
<<print=T>>=
texp <- Taylor(exp(xs), xs, 0, 3)
@ %def 


Printing the result in nice forms:
@ 
<<>>=
yacas("PrettyForm(texp)") 
yacas("TeXForm(texp)", retclass = "unquote")
@ %def 

@ 
<<>>=
PrettyForm(texp) 
TeXForm(texp)
@ %def 



\section{Simple \yac\ calculations}


\subsection{Setting and clearing a variable}

The function \code{Set()} and the operator \code{:=} can both be used to assign
values to global variables. 
@ 
<<>>=
yacas("n := (10 + 2) * 5")
yacas("n := n+n")
#yacas("Set(z, Cos(a))")
#yacas("z+z")
@ %def 

The same can be achieved with \sym\ objects: Consider:
@ 
<<>>=
Set(ns, (10 + 2) * 5)
@ %def 

Now \code{ns} exists as a variable in \yacas (and we
can make computations on this variable as above).
However we have no handle on
this variable in \R. Such a handle is obtained with
@ 
<<>>=
ns <- Sym("ns")
@ %def 
Now the \R\ variable \code{ns} refers to the \yacas\ variable
\code{ns} and we can make  calculations directly from \R, e.g:
@ 
<<>>=
Set(ns,123)
ns
ns^2
@ %def 

Likewise:
@ 
<<>>=
as <- Sym("as")
zs <- Sym("zs")
Set(zs, cos(as))
zs + zs
@ %def 


o clear a variable
binding execute \code{Clear()}:
@ 
<<>>=
yacas(expression(n))
yacas("Clear(n)")
yacas(expression(n))
@ %def 

@ 
<<>>=
Set(ns, 1)
ns <- Sym("ns")
ns
Clear(ns)
ns
@ %def 


\subsection{Symbolic and numerical evaluations, precision}

Evaluations are generally exact:
@ 
<<>>=
yacas("Exp(0)")
yacas("Exp(1)")
yacas("Sin(Pi/4)")
yacas("355/113")
@ %def 

@ 
<<>>=
exp(Sym(0))
exp(Sym(1))
sin(Pi/4)
Sym("355/113")
@ %def 

 To obtain a numerical evaluation
(approximation), the \code{N()} function can be used:
@ 
<<>>=
yacas("N(Exp(1))")
yacas("N(Sin(Pi/4))")
yacas("N(355/113)")
@ %def 

@ 
<<>>=
N(exp(1))
N(sin(Pi/4))
N(355/113)
@ %def 

The \code{N()} function has an optional second argument, the required precision:
@ 
<<>>=
yacas("N(355/133,20)")
@ %def 

@ 
<<>>=
N("355/113",20) 
@ %def 


The command \code{Precision(n)}
can be used to specify that all floating point numbers should have a
fixed precision of n digits:
@ 
<<>>=
yacas("Precision(5)")
yacas("N(355/113)")
@ %def 

@ 
<<>>=
Precision(5)
N("355/113")
@ %def 


\subsection{Rational numbers}

Rational numbers will stay rational as long as the numerator and
denominator are integers:
@ 
<<>>=
yacas(expression(55/10))
@ %def 

@ 
<<>>=
Sym("55 / 10")
@ %def 


\subsection{Symbolic calculation}
\label{sec:symbolicCalculation}

Some exact manipulations :
@ 
<<>>=
yacas("1/14+5/21*(30-(1+1/2)*5^2)")
yacas("0+x")
yacas("x+1*y")
yacas("Sin(ArcSin(alpha))+Tan(ArcTan(beta))")
@ %def 

@ 
<<>>=
Sym("1/14+5/21*(1*30-(1+1/2)*5^2)")
xs <- Sym("xs")
ys <- Sym("ys")
0+xs
xs+1*ys
sin(asin(xs))+tan(atan(ys))
@ %def 

\subsection{Complex numbers and the imaginary unit} 

The imaginary unit $i$ is denoted I and complex numbers can be entered
as either expressions involving I or explicitly Complex(a,b) for a+ib.
@ 
<<>>=
yacas("I^2")
yacas("7+3*I")
yacas("Conjugate(%)")
yacas("Exp(3*I)")
@ %def 

@ 
<<>>=
I^2
7+3*I
Conjugate(7+3*I)
exp(3*I)
@ %def 


\subsection{Recall the most recent line -- the \texttt{\%} operator}

The operator \texttt{\%} automatically recalls the result from the
previous line. 
@ 
<<>>=
yacas("(1+x)^3")
yacas("%")
yacas("z:= %")
@ %def 

@ 
<<>>=
(1+x)^3
zs <- Sym("%")
zs
@ %def 



\subsection{Printing with PrettyForm, PrettyPrint, TexForm and  TeXForm} 
\label{sec:printing}

There are different ways of displaying the output. 


\subsubsection{Standard form}
The (standard)
yacas form is:
@ 
<<>>=
yacas("A:={{a,b},{c,d}}")
yacas("B:= (1+x)^2+k^3")
yacas("A")
yacas("B")
@ %def 

@ 
<<>>=
as <- Sym("as"); bs <- Sym("bs"); cs <- Sym("cs"); ds <- Sym("ds")
A <- List(List(as,bs), List(cs,ds))
ks <- Sym("ks")
B <- (1+xs)^2+ks^3
A
B
@ %def 


\subsubsection{Pretty form}
The Pretty form is:
@ 
<<>>=
yacas("PrettyForm(A)")
yacas("PrettyForm(B)")
@ %def 

@ 
<<>>=
PrettyForm(A)
PrettyForm(B)
@ %def 



\subsubsection{TeX form}

The output can be displayed in TeX form:
@ 
<<>>=
yacas("TeXForm(B)", retclass = "character")
@ %def 

\section{Commands} 
\label{sec:commands}


\subsection{Factorial}

@ 
<<>>=
yacas("40!")
yacas("40!", retclass = "character")
@ %def 

@ 
<<>>=
Factorial(40)
@ %def 

\subsection{Taylor expansions} 

Expand $\exp(x)$ in three terms
around $0$ and $a$:
@ 
<<>>=
yacas("Taylor(x,0,3) Exp(x)")
yacas("Taylor(x,a,3) Exp(x)")
@ %def 

@ 
<<>>=
xs <- Sym("xs")
Taylor(exp(xs),xs,0,3)
as <- Sym("as")
Taylor(exp(x),x,as,3)
@ %def 

The \code{InverseTaylor()} function builds the Taylor series expansion of the
inverse of an expression. For example, the Taylor expansion in two
terms of the inverse of $\exp(x)$ around $x=0$ (which is the Taylor
expansion of $Ln(y)$ around $y=1$):
@ 
<<>>=
yacas("InverseTaylor(x,0,2)Exp(x)")
yacas("Taylor(y,1,2)Ln(y)")
@ %def 

@ 
<<>>=
InverseTaylor(exp(xs),xs,0,2)
Taylor(log(ys),ys,1,2)
@ %def 

\subsection{Solving equations}


\subsubsection{Solving equations symbolically}

Solve equations symbolically with the \code{Solve()} function:
@ 
<<>>=
yacas("Solve(x/(1+x) == a, x)")
yacas("Solve(x^2+x == 0, x)")
@ %def 

<<>>=
Solve(xs/(1+xs) == as, xs)
Solve(xs^2+xs == 0, xs)
@ %def 

<<>>=
Solve(List(xs^2+ys^2==6, xs-ys==3), List(xs,ys))
@ %def

<<>>=
mu <- Sym("mu") # mean
v <- Sym("v") # variance
Solve(List(mu==(xs/(xs+ys)), v==((xs*ys)/(((xs+ys)^2) * (xs+ys+1)))),
    List(xs,ys))
@ %def

(Note the use of the == operator, which does not evaluate to anything,
to denote an "equation" object.) 

\subsubsection{Solving equations numerically}
To solve an equation (in one variable) like $sin(x)-exp(x)=0$ numerically taking $0.5$
as initial guess and an accuracy of $0.0001$ do:
@ 
<<>>=
yacas("Newton(Sin(x)-Exp(x),x, 0.5, 0.0001)")
@ %def 

@ 
<<>>=
Newton(sin(xs)-exp(xs),xs, 0.5, 0.0001) 
@ %def 

\subsection{Expanding polynomials} 
@ 
<<>>=
yacas(expression(Expand((1+x)^3)))
@ %def 

@ 
<<>>=
Expand((x+1)^3)
@ %def 



\subsection{Simplifying an expression}

The function Simplify() attempts to reduce an expression
to a simpler form. 
@ 
<<>>=
y <- Sym("y")
yacas("(x+y)^3-(x-y)^3")
yacas("Simplify(%)")

@ %def 

@ 
<<>>=
(x+y)^3-(x-y)^3
Simplify("%")
@ %def 


\subsection{Analytical derivatives}

Analytical derivatives of functions can be evaluated with the
\code{D()} and \code{deriv()} functions:
@ 
<<>>=
yacas("D(x) Sin(x)")
@ %def 

@ 
<<>>=
deriv(sin(xs), xs)
@ %def 

These functions also accepts an argument specifying how often the
derivative has to be taken, e.g:
@ 
<<>>=
yacas("D(x,4)Sin(x)")
@ %def 

@ 
<<>>=
deriv(sin(xs),xs,4)
@ %def 

\subsection{Integration}

@ 
<<echo=F,results=hide>>=
yacas("Clear(a,b,A,B)")
@ %def 

@ 
<<>>=
#yacas("Integrate(x,a,b)Sin(x)")
#yacas("Integrate(x,a,b)Ln(x)+x")
#yacas("Integrate(x)1/(x^2-1)")
yacas("Integrate(x)Sin(a*x)^2*Cos(b*x)")
@ %def 

@ 
<<>>=
#Integrate(sin(x),x,a,b)
#Integrate(log(x),x,a,b)
#Integrate(1/(x^2-1),x)
a <- Sym("a")
b <- Sym("b")
Integrate(sin(a*x)^2*cos(b*x),x)
@ %def 


\subsection{Limits}
@ 
<<>>=
#yacas("Limit(x,0)Sin(x)/x")
yacas("Limit(n,Infinity)(1+(1/n))^n")
yacas("Limit(h,0) (Sin(x+h)-Sin(x))/h")
@ %def 

@ 
<<>>=
#Limit(sin(x)/x,x,0)
ns <- Sym("ns")
Limit((1+(1/ns))^ns,ns,Infinity)
hs <- Sym("hs")
Limit((sin(xs+hs)-sin(xs))/hs,hs,0)
@ %def 

\subsection{Variable substitution}

@ 
<<>>=
yacas("Subst(x,Cos(a))x+x")
@ %def 

@ 
<<>>=
Subst(xs+xs,xs,cos(as))
@ %def 

\subsection{Solving ordinary differential equations}

@ 
<<>>=
yacas("OdeSolve(y''==4*y)")
yacas("OdeSolve(y'==8*y)")
@ %def 








\section{Matrices}
\label{sec:matrices}
@ 
<<>>=
yacas("E4:={ {u1,u1,0},{u1,0,u2},{0,u2,0} }")
yacas("PrettyForm(E4)")
@ %def 

@ 
<<>>=
u1 <- Sym("u1")
u2 <- Sym("u2")
E4 <- List(List(u1, u1, 0), List(u1, 0, u2), List(0, u2, 0))
PrettyForm(E4)
@ %def 


\subsection{Inverse} 

@ 
<<>>=
yacas("E4i:=Inverse(E4)")
yacas("Simplify(E4i)")
yacas("PrettyForm(Simplify(E4i))")
@ %def 


@ 
<<>>=
E4i <- Inverse(E4)
Simplify(E4i)
PrettyForm(Simplify(E4i))
@ %def 


\subsection{Determinant}

@ 
<<>>=
yacas("Determinant(E4)")
yacas("Determinant(E4i)")
yacas("Simplify(E4i)")
yacas("Simplify(Determinant(E4i))")
@ %def 


@ 
<<>>=
determinant(E4)
determinant(E4i)
Simplify(E4i)
Simplify(determinant(E4i))
@ %def 



%%% -------------------------------------------------------------
\section{Miscellaneous} 
\label{sec:misc}



Note that the value returned by \yacas\ can be of different types:
@ 
<<print=T>>=
yacas(expression(Factor(x^2-1)),retclass='unquote')
yacas(expression(Factor(x^2-1)),retclass='character')
@ %def 


\end{document}

