\documentclass[]{article}
\usepackage{inputenc,hyperref,a4,color,boxedminipage,Sweave}

% 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}}


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

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


\begin{document}
\maketitle
\tableofcontents
<<echo=FALSE, results=hide>>=
library(Ryacas)
options(width=60)
@

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



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

\ryacas{} makes the \yacas{} computer algebra system available from
within \rlan. (\yacas{} is short for ``Yet Another Computer Algebra
System''). 

This document is based on the yacas Sym interface and is similar
to another document based on the interface provided by R yacas function.

\yacas{} is developed by Ayal Pinkhuis (who is also the maintainer)
and others, and is available at
\href{yacas.sourceforge.org}{yacas.sourceforge.org} for various
platforms. There is a comprehensive documentation (300+ pages) of
\yacas{} (also available at
\href{yacas.sourceforge.org}{yacas.sourceforge.org}) 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.

A Sym object is a yacas character string that has the "Sym" class.
Using +, - and other similar R operators.
One can combine Sym objects with other Sym objects as well as to
other R objects.  Sym(x) coerces object x to a Sym object by
first coercing it to character and then changing its class to "Sym".
One can also apply sin, cos, tan, deriv, Integrate
and other provided functions to Sym objects as shown in the examples below.


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

Algebraic calculations:
@ 
<<>>=
(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:
@ 
<<>>=
Sym("-12/2")
Eval(Sym("-12/2"))
@ %def 

Working with symbolic expressions:
<<>>=

x <- Sym("x")
Factor(x^2-1)
exp1 <- x^2 + 2 * x^2
exp0 <- Sym("exp0")
exp2 <- 2 * Sym(exp0)
exp3 <- 6 * Pi * x
exp4 <- exp1 * (1 - sin(exp3)) / exp2
exp4
@ 

Working with numerical expressions:
....

Combining symbolic and numerical expressions:
@ 
<<>>=
N(sin(1)^2 + cos(x)^2)
@ %def 


Differentiation:
<<>>=
deriv(sin(x), x)
@

Integration: [!!! This is odd: I thought yacas was case sensitive...]]]
@ 
<<>>=
Set(A, 'ssss')
a <- Sym("a")
b <- Sym("b")
Integrate(sin(x), x, a, b)
Clear("A")
Integrate(sin(x), x, a, b)
@ %def 

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

Taylor expansion:
@ 
<<>>=
texp <- Taylor(exp(x), x, 0, 3)
@ %def 

Printing the result in nice forms:
@ 
<<>>=
PrettyForm(texp) 
TeXForm(texp)
@ %def 



\section{Simple \yac\ calculations}


\subsection{Setting and clearing a variable}

The function Set() command can both be used to assign
values to global variables. 
@ 
<<>>=
Set(n, "(10 + 2) * 5")
n <- Sym("n")
Set(n, n+n)

a <- Sym("a")
Set(z, cos(a))
z <- Sym("z")
z + z

@ %def 


To clear a variable
binding execute Clear():
@ 
<<>>=
Set(n, 1)
n <- Sym("n")
n
Clear(n)
n
@ %def 

\subsection{Symbolic and numerical evaluations, precision}

Evaluations are generally exact:
@ 
<<>>=
exp(0)
exp(1)
sin(Pi/4)
Sym("355/113")
@ %def 

 To obtain a numerical evaluation
(approximation), the N() function can be used:
@ 
<<>>=
N(exp(1))
N(sin(Pi/4))
N(355/113)
@ %def 

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

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


\subsection{Rational numbers}

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


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

Some exact manipulations :
@ 
<<>>=
Sym("1/14+5/21*(1*30-(1+1/2)*5^2)")
x <- Sym("x")
0+x
y <- Sym("y")
x+y
sin(asin(x))+tan(atan(y))
@ %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.
@ 
<<>>=
I^2
7+3*I
Conjugate("%")
exp(3*I)
@ %def 

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

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



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

There are different ways of displaying the output. The (standard)
yacas form is:
@ 
<<>>=
a <- Sym("a"); b <- Sym("b"); c <- Sym("c"); d <- Sym("d")
A <- List(List(a,b), List(c,d))
k <- Sym("k")
B <- (1+x)^2+k^3
A
B
@ %def 

The Pretty form is:
@ 
<<>>=
PrettyForm(A)
PrettyForm(B)
@ %def 

An alternative is the PrettyPrinter [!!! Why does this give the same
result as before??? Earlier I got XML output as well... Is something
not reset???]
@ 
<<>>=
PrettyPrinter("PrettyForm")
A
PrettyPrinter()
A
PrettyPrinter("OMForm")
A
@ %def 

The output can be displayed in TeX form as well:
@ 
<<>>=
TeXForm(B)
@ %def 



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


\subsection{Factorial}

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

\subsection{Taylor expansions} 

Expand exp(x) in three terms
around 0 and a:
@ 
<<>>=
Taylor(exp(x),x,0,3)
a <- Sym("a")
Taylor(exp(x),x,a,3)
@ %def 

The 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 log(y) around y=1):
@ 
<<>>=
InverseTaylor(exp(x),x,0,2)
Taylor(log(y),y,1,2)
@ %def 

\subsection{Solving equations}


\subsubsection{Solving equations symbolically}

Solve equations symbolically with:
@ 
<<>>=
Solve(x/(1+x) == a, x)
Solve(x^2+x == 0, x)
@ %def 
(Note the use of the == operator, which does not evaluate to anything,
to denote an "equation" object.) Solve() is rather limited.

\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:
@ 
<<>>=
Newton(sin(x)-exp(x),x, 0.5, 0.0001) 
@ %def 

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



\subsection{Simplifying an expression}

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


\subsection{Analytical derivatives}

Analytical derivatives of functions can be evaluated:
@ 
<<>>=
deriv(sin(x), x)
deriv(deriv(sin(x)))
@ %def 

The \code{deriv} function also accepts an argument specifying how often the
derivative has to be taken, e.g:
@ 
<<>>=
deriv(sin(x),x,2)
@ %def 

\subsection{Integration}

!!! Problem arises because A was defined above (a is not defined, though)
(Seems ok in Sym version.)
@ 
<<>>=
Integrate(sin(x),x,a,b)
Integrate(log(x),x,a,b)
Integrate(1/(x^2-1),x)
Integrate(sin(a*x)^2*cos(b*x),x)
@ %def 


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


\subsection{Variable substitution}

@ 
<<>>=
Subst(x+x,x,cos(a))
@ %def 

\subsection{Solving ordinary differential equations}

OdeSolve notation not supported by Sym currently.

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




\section{Matrices}
\label{sec:matrices}
@ 
<<>>=
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} 

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


\subsection{Determinant}

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


\end{document}
