Introduction to Yacas: Getting started, tutorial and examples
Introduction
Yacas (Yet Another Computer Algebra System) is a small and highly
flexible computer algebra language.
The syntax uses a infix-operator grammar parser. The distribution contains
a small library of mathematical functions, but its real strength
is in the language in which you can easily write your own symbolic
manipulation algorithms. It supports arbitrary precision arithmetic.
Getting started
Read the file INSTALL for instructions on how to compile Yacas.
After launching Yacas, you should be able to halt again by typing
Typically, lines should end with a ; , although this is not required (Yacas will append a ;).
There is also some online help. If you type
you should be able to read this manual. Typing
should give you help on that function. Type
to get some random examples.
In this documentation you should find some examples on how to use Yacas.
Syntax
The syntax is handled by an infix operator grammar parser. This
means that most of the times you type in expressions of the form Func(var1,var2) , or using infix operators, a+b , prefix operators -x ,or postfix operators x++. Last but not least there are the 'bodied'
operators, which look like normal functions f(x) but with the last
argument outside of the argument list: more like f(x)y . A typical example is the function While, which takes on the
form While(predicate)body;
Lists
In addition, there are lists. Lists are groups of items, represented
by putting the objects between braces. The list of objects a,b, and c
could be entered by typing {a,b,c}.
In this system, vectors are represented through lists. Matrices are
lists of lists.
Lists can be accessed through the [[i]] operator. Examples: when you enter
then
evaluates to b, and
evaluates to {b,c,d}. Here
evaluates to {2,3,4}.
Note that spaces around the .. operator are needed, because otherwise
the parser will not be able to distinguish it from a number.
Another list type is the associated list, which can act as a mini
database. Indexing can go through strings. As an example, first
enter
and then
. Then,
would return
Compound bodies
Multiple commands can be grouped together using the [ and ] brackets.
the form
evaluates a, then b, then c, and returns the result
of evaluating c.
Threading
Some functions can be threaded. This means that calling the function
with a list as argument will result in a list with that function being
called on each item in the list. Eg.
will result in {Sin(a),Sin(b),Sin(c)}. This functionality is
implemented for most normal analytic functions and arithmetic
operators.
Pure functions
Pure functions are currently implemented using the operator Apply.
The following line:
Apply( {{x,y},x+y} , {2,3} );
|
would also evaluate to 5.
Here, {{x,y},x+y} is treated as a pure function, x and y becoming the
local variables bound to the parameters passed, and x+y the body.
Tutorial
This section should get you up and running with Yacas in a short
time. To see all the functions in action the "tests" file
that comes with the standard scripts should demonstrate how to use
them. "tests" is a test suite for Yacas.
A Quick Introduction: Yacas As A Calculator
You are now ready to enter expressions. For instance, typing 2+3; will result in a prompt
In( 0 ) = 2+3;Out( 0 ) = 5;
|
With each input line there is an associated output line.
Try FullForm(a+b*c); and you will see
the text (+ a (* b c )) appear on the screen. This
is the 'internal representation' of the expressions, lists
just like LISP.
The Linux version of Yacas has a command line similar to other
scripting languages. It holds a history, so you can browse back
to what you typed in.
The standard scripts already contain a simple math library for
doing symbolic simplification:
In( 1 ) = 0+a;Out( 1 ) = a; In( 2 ) = 1*a; Out( 2 ) = a; In( 2 ) = Sin(ArcSin(a)); Out( 2 ) = a;
|
Yacas can deal with arbitrary precision numbers:
In( 3 ) = 20!;Out( 3 ) = 2432902008176640000;
|
When dealing with floating point numbers, the command Precision(n);can be used to specify that floating point numbers should have n
digits.
Analytic derivatives of functions can also be performed:
In( 4 ) = D(x) Sin(x);Out( 4 ) = Cos(x); In( 5 ) = D(x) D(x) Sin(x); Out( 5 ) = -Sin(x);
|
Rational functions will stay rational as long as the numerator and
denominator are integers, so 55/10; will evaluate to 11/2 .
You can override this behaviour by using N : N(55/10) will evaluate to 5.5 .
This behaviour holds for most analytic functions. Yacas will try
to maintain an accurate answer (in terms of integers) in favour
of using floating point numbers. N changes this. If the value for
pi is needed, use Pi. This will be set to the correct value
when N(Pi) is called. Yacas might know some simplification rules
(especially with trigonometric functions).
And some very simple equation solving algorithms are in place:
In( 6 ) = Solve(a+x*y=z,x);Out( 6 ) = (z-a)/y;
|
Currently Solve only deals with equations where the variable to
be solved for only occurs once in the equation. In the future there
will be more sophisticated algorithms.
Taylor series are supported. Typing
In( 7 ) = Taylor(x,0,3) Exp(x)
|
will result in 1+x+(1/2)*x^2+(1/6)*x^3 . As this form may be a little
bit hard to read, you might then want to type
In( 8 ) = PrettyForm(%);
/ 1 \ 2 / 1 \ 3
1 + x + | - | * x + | - | * x
\ 2 / \ 6 /
Out( 8 ) = True;
|
The % refers to the result from the previous line. PrettyForm tries
to render the formula in a better format for reading, using text.
Variables
Yacas supports variables.There are two places where variables are
stored, globally or locally. Variables default to global, unless
specifically declared local (a variable var can be declared
local with the function Local(var) ).
Try typing
and then
The result will be 2 . The variable a has been globally set to 2. To clear the
variable binding, just call Clear(a);. a; will now
evaluate to a. This is one of the properties of the evaluation
scheme of Yacas: when something can not be evaluated any further,
it is returned as the final result.
The standard scripts offer the operator := for assigning
values to variables (amongst other things), so the rest of this
document will use := instead. The equivalent of Set(a,2); would in this case be a:=2;
Lists
Lists can be typed in using the { and } brackets. They evaluate the
arguments, and return a list with results of evaluating each element.
So, typing
would evaluate to {3,3}
The idea of using lists to represent expressions dates back to the
language LISP, which was developed in the 70's. Together with a
small set of operations on lists very powerful symbolic manipulation
algorithms can be built. Lists can also be abused, when a variable
number of arguments are expected. Lists are also used as a representation
for vectors. This section will take a look at some of
the operations on lists Yacas provides.
Lets take one variable and set it to a list:
In( n ) = m:={a,b,c};Out( n ) = True; In( n+1 ) = Length(m); Out( n+1 ) = 3; In( n+2 ) = Reverse(m); Out( n+2 ) = {c,b,a}; In( n+3 ) = m; Out( n+3 ) = {a,b,c}; In( n+4 ) = Concat(m,m); Out( n+4 ) = {a,b,c,a,b,c}; In( n+5 ) = m[[1]]; Out( n+5 ) = a; In( n+6 ) = Nth(m,2); Out( n+6 ) = b;
|
These are only a small introduction of course. Consult the reference
section to see more operations on lists.
List as vectors: Linear Algebra
Vectors are represented through lists. The list {1,2,3} would
be a three-dimensional vector with components 1,2 and 3. Matrices
are represented as a 'vector of vectors'.
Vector components can be assigned values usign the {{:=}} operator:
In( n ) = l:=ZeroVector(3);Out( n ) = True; In( n+1 ) = l; Out( n+1 ) = {0,0,0}; In( n+2 ) = l[[ 2 ]]:=2; Out( n+2 ) = True; In( n+2 ) = l; Out( n+2 ) = {0,2,0};
|
Yacas can perform matrix multiplications, multiplications of
matrices with vectors, numbers, etcetera. The standard Yacas
supplied scripts also support taking the determinant and inverse
of a matrix, and solving linear sets of equations, solving A x = b for x, where A is a matrix, and
x and b are vectors. There are several more matrix
operations which are supported. See the reference for the full list.
Defining simplification rules
Yacas has a simple and easy to use pattern matching scheme
for simplifying expressions. Examples can be found in for instance
the files "standard", "stdfuncs", "deriv" and "solve" that
come with the Yacas distribution.
As an example, consider the following lines:
IsPositiveInteger(_n) <-- IsInteger(n) And n>0;
10 # f(0) <-- 1;
20 # f(n_IsPositiveInteger) <-- n*f(n-1);
|
IsPositiveInteger is actually defined in the standard
distribution, so it is not necessary. The other two lines obviously
define a factorial function n*(n-1)*...*1.
The <-- assigns a rule to be applied to a specific
function. The _n in rule specifies that any object
can be matched, and the local variable n is assigned to it.
The 0 should match perfectly. n_IsPositiveInteger is
the same as _n with the proviso that IsPositiveInteger(n) should return True
otherwise the pattern is not matched. Some rules have a number followed
by a # in front of them. This number defines an ordering
for the pattern matching rules. 0 is the lowest allowed
value, and will be tried first. Multiple rules can have the same number.
This just means that it doesn't matter what order these patterns are
tried in. If no number is supplied, 0 is assumed.
Additional predicates can be specified too. IsPositiveInteger could also have been defined as:
10 # IsPositiveInteger(n_IsInteger)_(n>0) <-- True;
20 # IsPositiveInteger(_n) <-- False;
|
Where the first rule specifies that if n is an integer, and greater than
zero, the predicate is true, and the second rule states that the
predicate is false otherwise. The (n>0) clause is added
after the pattern, and implies that this predicate should return true also,
if this pattern is to match. This is useful for defining predicates involving
more than one argument.
Some other examples:
_x + 0 <-- x;
_x - _x <-- 0;
ArcSin(Sin(_x)) <-- x;
|
Yacas will first try to match the pattern, like a template. It will then
assign the relevant variables, and try the predicates as stated in the
pattern. The post-predicate (defined after the pattern) is tried after
all these matched. The simplification rule _x - _x <-- 0;specifies that the two terms left and right of the minus sign should
match.
There is a slightly more complex way of defining rules, using the RuleBase
and Rule functions, but these are generally not necessary.
Examples
A small tour of the capabilities Yacas currently offers:
large factorials, using arbitrary
precision integers.
convert to another number base.
expands the expression into a polynomial.
apply an operator to a list of arguments. This
example would evaluate to 5.
Apply({{x,y},x+y},{2,3});
|
apply a pure function to a list of arguments. This
example would evaluate to 5.
takes the derivative of a function.
solve a function for a variable.
calculate the taylor series expansion of a function.
calculate the limit of a function as a variable
approaches a value.
Newton(Sin(x),x,3,0.0001);
|
use the Newton method for finding a zero.
create a matrix with the elements specified
in the vector on the diagonal.