Getting started with Yacas
Introduction
Yacas (Yet Another Computer Algebra System) is a small and highly flexible
general-purpose 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. The core engine supports
arbitrary precision arithmetic (it can also optionally be linked with
the GNU arbitrary precision math library libgmp2) and is able to execute symbolic manipulations on various mathematical objects by following user-defined rules.
Installing Yacas
Read the file INSTALL for instructions on how to compile Yacas. Yacas is
portable across most Unix-ish platforms and requires only a standard C++
compiler such as g++.
At the base Yacas accepts text as input and returns text as output.
This makes it very platform-independent. Apart from Unix Yacas has been
compiled on Windows and EPOC32 aka Psion (which doesn't come with a standard c++
library!).
The source code to compile Yacas for Windows can be found at the
Sourceforge repository.
For Unix, compilation basically amounts to the standard sequence
./configure
make
make install
|
The arbitrary precision math in Yacas will be generally faster if you compile Yacas with the "libgmp" library (configure option --enable-gmp). Precompiled Red Hat (RPM) and Debian (DEB) packages are also available.
Using the console mode
You can run Yacas in the console mode simply by typing yacas. The Yacas command prompt looks like this:
and Yacas's answers appear after the prompt
A Yacas session may be terminated by typing Exit() or
quit. Pressing ^C will also quit Yacas;
however, pressing ^C while Yacas is busy with a calculation will stop
just that calculation.
A session can be restarted (forgetting all previous
definitions and results) by typing restart
Typically, you would enter one statement per line, for example
Statements should end with a semicolon (;) although this is not required (Yacas will append a ; at end of line to finish the statement).
All documentation is accessible from the Yacas prompt. If you type
you should be able to read all available manuals (Yacas will run lynx to show you the HTML documentation). You can also get help on individual functions: to read about the function Sum(), type
You can also type Example(); to get some random examples of Yacas calculations.
The command line has a history list, so it should be easy to
browse through the expressions you entered previously using the Up and Down arrow keys.
Typing the first few characters of a previous expression and then
hitting the TAB key makes Yacas recall the last expression
in the history list that matches these first characters.
Commands spanning multiple lines can (and actually have to) be entered by using a trailing backslash \ at end of each continued line. For example:
In> a:=2+3+
Error on line 1 in file [CommandLine]
Line error occurred on:
>>>
Error parsing expression
In> a:=2+3+ \
In> 1
Out> 6;
|
The error after our first attempt occurred because Yacas has appended a semicolon at end of the first line and 2+3+; is not a valid Yacas expression. Incidentally, any text Yacas prints without a prompt is either messages printed by functions as their side-effect, or error messages. Resulting values of expressions are always printed after an Out> prompt.
Yacas as a symbolic calculator
We are ready to try some calculations. Yacas uses a C-like infix syntax and is case-sensitive. Here are some exact manipulations with fractions for a start:
In> 1/14+5/21*(30-(1+1/2)*5^2);
Out> -12/7;
|
The standard scripts already contain a simple math library for
symbolic simplification of basic algebraic functions. Any names such as x are treated as independent, symbolic variables and are not evaluated by default.
In> 0+x;
Out> x;
In> x+1*y;
Out> x+y;
In> Sin(ArcSin(alpha))+ArcCos(Cos(beta));
Out> alpha+beta;
In> (x+y)^3-(x-y)^3
Out> (x+y)^3-(x-y)^3;
In> Simplify(%)
Out> 6*x^2*y+2*y^3;
|
The special operator % automatically recalls the result from the previous line. The function Simplify attempts to reduce an expression to a simpler form. Note that standard function names in Yacas are typically capitalized. Multiple capitalization such as "ArcSin" is sometimes used. The underscore character _ is a reserved operator symbol and cannot be part of variable or function names.
Yacas can deal with arbitrary precision numbers:
In> 20!; Out> 2432902008176640000; |
When dealing with floating point numbers, the command Precision(n); can be used to specify that all floating point numbers should have a fixed precision of n digits:
In> Precision(30);
Out> True;
In> 1.0/243;
Out> 0.004115226337448559670781893004;
|
Note that we need to enter 1.0 as a floating-point constant to force the approximate calculation, otherwise the fraction would have been left unevaluated. The value True is a boolean constant.
Analytic derivatives of functions can be evaluated:
In> D(x) Sin(x);
Out> Cos(x);
In> D(x) D(x) Sin(x);
Out> -Sin(x);
|
Rational numbers 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 the
numerical evaluation function N(): for example, N(55/10) will evaluate to 5.5 . This
behaviour holds for most math functions. Yacas will try to maintain an
exact answer (in terms of integers or fractions) instead of using
floating point numbers, unless N() is used. If the
value for the constant pi is needed, use Pi.
This will be replaced by the (approximate) numerical value when
N(Pi) is called. Yacas knows some simplification rules
using Pi (especially with trigonometric functions). 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+i*b.
Some simple equation solving algorithms are in place:
In> Solve(a+x*y==z,x);
Out> (z-a)/y;
In> Solve({11*x+3*y==1,2*x+y==0},{x,y})
Out> {{1/5,-2/5}};
|
(Note the use of the == operator, which does not evaluate to anything, to denote an "equation" object.) Currently Solve is rather limited and 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
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 type
In> PrettyForm(%);
/ 1 \ 2 / 1 \ 3
1 + x + | - | * x + | - | * x
\ 2 / \ 6 /
Out> True;
|
The function PrettyForm() tries to render the formula in a better format for reading, using just ASCII text.
Variables
Yacas supports variables:
In> Set(a,Cos(0));
Out> True;
In> a:=a+1;
Out> 2;
|
The variable a has now been globally set to 2. The function Set() and the operator := can both be used to assign values to global variables. (Variables
local to procedures can also be defined; see below the chapters on
programming.) To clear a variable binding, execute
Clear(a);. a; will now evaluate to just
a. This is one of the properties of the evaluation
scheme of Yacas: when some object can not be evaluated or transformed
any further, it is returned as the final result.
Currently there is no difference between assigning variables using Set() or using the operator :=. The latter can however also assign lists and define functions.
Functions
The := operator can be used to define functions:
will define a new function, "f", that accepts
one argument and returns twice the square of that argument.
A function name such as "f" may be used by different functions if they take different numbers of arguments:
In> f(x):=x^2;
Out> True;
In> f(x,y):=x*y;
Out> True;
In> f(3)+f(3,2);
Out> 15;
|
Functions may also return boolean values (True and False). Such functions are called predicates. For example, IsNumber() and IsInteger() are predicates defined in the standard library:
In> IsNumber(2+x);
Out> False;
In> IsInteger(15/5);
Out> True;
|
When assigning variables, the right hand side is evaluated before it
is assigned. Thus
will set a
to 4. This is however not the case for functions. When entering f(x):=x+x the right hand side, x+x, is
not evaluated before being assigned. This can be forced by using the
assignment in combination with Eval:
will first evaluate x+x to 2*x before
assigning it to the user function f. This specific example is not a
very useful one but it will come in handy when the operation being
performed on the right hand side is expensive. For example, if we
evaluate a Taylor series expansion before assigning it to the
user-defined function, the engine doesn't need to create the Taylor
series expansion each time that user-defined function is called.
Strings and lists
In addition to numbers and variables, Yacas supports strings and lists. Strings are simply sequences of characters enclosed by double quotes, for example: "this is a string with \"quotes\" in it". Lists are ordered groups of items, as usual. Yacas represents lists by putting the objects between braces and separating them with commas. The list consisting of objects a, b, and c could be entered by typing {a,b,c}. In Yacas, vectors are represented as lists and matrices as lists of lists. In fact, any Yacas expression can be converted to a list (see below).
Items in a list can be accessed through the [ ]
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 necessary, or else the
parser will not be able to distinguish it from a number.
Another use of lists is the associative list, sometimes called a hash table, which is implemented in Yacas simply as a list of key-value pairs. Keys must be strings and values may be any objects. Associative lists can also work as mini-databases. As an example, first
enter
and then
u["name"]:="Isaia";
u["occupation"]:="prophet";
u["is alive"]:=False;
|
Then, u["name"] would return "Isaia". The list u now contains three sublists, as we can see:
In> u;
Out> { {"is alive", False}, {"occupation", "prophet"}, {"name", "Isaia"} };
|
Assignment of multiple variables is also possible using lists. For instance, {x,y}:={2!,3!} will result in 2 being assigned to
x and 6 to y.
Lists evaluate their arguments, and return a list with results of evaluating each element. So, typing {1+2,3}; 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 used as function arguments when a variable number of arguments are expected.
Let's try some list operations now:
In> m:={a,b,c};
Out> True;
In> Length(m);
Out> 3;
In> Reverse(m);
Out> {c,b,a};
In> Concat(m,m);
Out> {a,b,c,a,b,c};
In> m[1]:="blah blah";
Out> True;
In> m;
Out> {"blah blah",b,c};
In> Nth(m,2);
Out> b;
|
Many more list operations are described in the reference manual.
Linear Algebra
Vectors of fixed dimension are represented as lists of their components. 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 just like list items, since they are in fact list items:
In> l:=ZeroVector(3); Out> True; In> l; Out> {0,0,0}; In> l[ 2 ]:=2; Out> True; In> l; Out> {0,2,0}; |
Yacas can perform multiplication of matrices, vectors and numbers as usual
in linear algebra:
In> v:={1,0,0,0}
Out> {1,0,0,0};
In> E4:={ {0,u1,0,0},{d0,0,u2,0},{0,d1,0,0},{0,0,d2,0}}
Out> {{0,u1,0,0},{d0,0,u2,0},{0,d1,0,0},{0,0,d2,0}};
In> CharacteristicEquation(E4,x)
Out> x^4-x*u2*d1*x-u1*d0*x^2;
In> Expand(%,x)
Out> x^4-(u2*d1+u1*d0)*x^2;
In> v+E4*v+E4*E4*v+E4*E4*E4*v
Out> {1+u1*d0,d0+(d0*u1+u2*d1)*d0,d1*d0,d2*d1*d0};
|
The standard Yacas script library also includes taking the determinant and
inverse of a matrix, finding eigenvectors and eigenvalues (in simple cases) and solving linear sets of equations, such as A x = b where A is a matrix, and x and b are vectors. There
are several more supported matrix operations. See the reference manual for
more details.
Control flow: conditionals, loops, blocks
The Yacas language includes some constructs and functions for control flow. Looping can be done with either a ForEach() or a While() function call. The function ForEach(x, list) body executes its body for each element of the list and assigns the variable "x" to that element each time. The function call While(predicate) body repeats "body" until the "predicate" returns False.
Conditional execution is implemented by the If(predicate, body1, body2) function call, which works like the C language construct (predicate) ? body1 : body2. If the condition is true, "body1" is executed, otherwise "body2" and the corresponding value is returned. For example, the absolute value of a number "x" can be computed with:
absx := If( x>=0, x, -x );
|
If several operations need to be executed in sequence to obtain a result, you can use a Prog() function call or equivalently the [ ] construct.
To illustrate these constructs, let us create a list of all even integers from 2 to 20 and compute the product of all those integers except those divisible by 3. (This is not necessarily the most economical way to do it in Yacas.)
In> L := {};
Out> {};
In> i := 2;
Out> 2;
In> While(i<=20) [ L:= Append(L, i); i := i+2; ]
Out> True;
In> L;
Out> {2,4,6,8,10,12,14,16,18,20};
In> answer := 1;
Out> 1;
In> ForEach(i, L) If (Mod(i, 3)!=0, answer := answer * i);
Out> True;
In> answer;
Out> 2867200;
|
We used a shorter form of If(predicate, body) with only one body which is executed when the condition holds. If the condition does not hold, this function call returns False.