分类:
2007-06-11 20:49:10
Currently, the Yacas programming language is stable and seems powerful enough for all computer algebra applications. External libraries providing additional functionality may be dynamically loaded into Yacas via the "plugin" mechanism.
The base Yacas application accepts text as input and returns text as output. This makes it rather platform-independent. Apart from Unix-like systems, Yacas has been compiled on Windows and on 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 .
For Unix, compilation basically amounts to the standard sequence
./configure make make install |
A note for MacOS X 10.4 users; for now loading dynamic link libraries does not work well (December 2005). So in order to build Yacas properly on Mac OS X the collowing configure command can be used:
./configure CFLAGS="-O9 -DDISABLE_DYNAMIC" CXXFLAGS="-O9 -DDISABLE_DYNAMIC" --enable-server --disable-shared |
The arbitrary precision math in Yacas will be generally faster if you compile Yacas with the libgmp library (the option --with-numlib=gmp for the configure script). Precompiled Red Hat (RPM) and Debian (DEB) packages are also available.
Additionally, LaTeX-formatted documentation in PostScript and PDF formats can be produced by the command
make texdocs |
or, alternatively, by passing --enable-ps-doc or --enable-pdf-doc to ./configure when building Yacas. In the latter case, the documentation will be automatically rebuilt every time the documentation changes (which is useful when maintaining the documentation).
In addition, there is also a Java version of the lower-level interpreter. The code for this Java version can be found in the directory "JavaYacas", and can be compiled with the make file "makefile.yacas", by typing in:
make -f makefile.yacas |
The interpreter can then be invoked from the command line with:
java -jar yacas.jar |
or alternatively it can be invoked as an applet, by opening yacasconsole.html.
The binary files that comprise the entire binary release for the Java version are:
To get copy-pasting to work in the applet version, you need to tell the Java virtual machine that you trust this applet to get or set data on the clipboard of your OS.
On Unix-like systems this means creating a file ~/.java.policy if it does not already exist, and then adding the following lines to it:
grant codeBase "*" { permission java.awt.AWTPermission "accessClipboard"; }; |
This grants all applets residing at ~apinkus/* access to the clipboard.
This can be done in a similar way on Windows, but the directory where you need to place the .java.policy file depends on the version of Windows that is running.
The Java version has almost all the features the C++ version has. In fact, there is no reason the Java version should not have all the same features. At the time of writing (version 1.0.58), plugins are not available yet, not all command line arguments are available yet, and the command line prompt does not have the history yet.
In> |
Out> |
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
In> Sin(Pi/2); Out> 1; |
Statements should end with a semicolon (;) although this is not required in interactive sessions (Yacas will append a semicolon at end of line to finish the statement).
All documentation is accessible from the Yacas prompt. If you type
In> ?? |
In> ?Sum |
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.
When a few characters have been typed, the command line will use the characters before the cursor as a filter into the history, and allow you to browse through all the commands in the history that start with these characters quickly, instead of browsing through the entire history.
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; |
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.
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))+Tan(ArcTan(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> N(1/243); Out> 0.004115226337448559670781893004; |
The N function has an optional second argument, the required precision:
In> N(1/234,10) Out> 0.0042735042; In> N(1/234,20) Out> 0.0042735042735042735; In> N(1/234,30) Out> 0.004273504273504273504273504273; |
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); |
The D function also accepts an argument specifying how often the derivative has to be taken. In that case, the above expressions can also be written as:
In> D(x,1)Sin(x) Out> Cos(x); In> D(x,2)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 behavior by using the numerical evaluation function N(). For example, N(55/10) will evaluate to 5.5 . This behavior 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. Where the value for the constant pi is needed, use the built-in variable Pi. It 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+ib.
Some simple equation solving algorithms are in place:
In> Solve(x/(1+x) == a, x); Out> {x==a/(1-a)}; In> Solve(x^2+x == 0, x); Out> {x==0,x==(-1)}; |
(Note the use of the == operator, which does not evaluate to anything, to denote an "equation" object.) Currently Solve is rather limited, but in the future there will be more sophisticated algorithms.
Taylor series are supported, for example:
In> Taylor(x,0,3) Exp(x) Out> 1+x+(1/2)*x^2+(1/6)*x^3; |
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 ASCII text. You can also export an expression to TeX by typing TeXForm(...).
In> Set(a,Cos(0)); Out> True; In> a:=a+1; Out> 2; |
Currently there is no difference between assigning variables using Set() or using the operator :=. The latter can however also assign lists and define functions.
f(x):=2*x*x |
One and the same function name such as f may be used by different functions if they take different numbers of arguments (but not if they merely take different types of arguments, since Yacas does not have a strict type system):
In> f(x):=x^2; Out> True; In> f(x,y):=x*y; Out> True; In> f(3)+f(3,2); Out> 15; |
Yacas predefines True and False as boolean values. Functions returning boolean values 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
a:=2*2 |
f(x):=Eval(x+x) |
"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
uu:={a,b,c,d,e,f}; |
uu[2]; |
uu[2 .. 4]; |
2 .. 4 |
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
u:={}; |
u["name"]:="Isaia"; u["occupation"]:="prophet"; u["is alive"]:=False; |
Now, 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"} }; |
Lists evaluate their arguments, and return a list with results of evaluating each element. So, typing {1+2,3}; would evaluate to {3,3}.
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.
The idea of using lists to represent expressions dates back to the language LISP developed in the 1970's. From 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.
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 matrix operations supported. See the reference manual for more details.
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 evaluated, otherwise "body2" is evaluated, and the corresponding value is returned. For example, the absolute value of a number 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 features, 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. (What follows 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.
The above example is not the shortest possible way to write out the algorithm. A more 'functional' approach would go like this:
First construct a list with all even numbers from 2 to 20. For this we use the .. operator to set up all numbers from one to ten, and then multiply that with two.
In> 2*(1 .. 10) Out> {2,4,6,8,10,12,14,16,18,20}; |
Now we want an expression that returns all the even numbers up to 20 which are not divisible by 3. For this we can use Select, which takes as first argument a predicate that should return True if the list item is to be accepted, and false otherwise, and as second argument the list in question:
In> Select({{n},Mod(n,3)!=0},2*(1 .. 10)) Out> {2,4,8,10,14,16,20}; |
The numbers 6, 12 and 18 have been correctly filtered out.
All that remains is to factor the items in this list. For this we can use Factorize, which accepts a list as argument and returns the product of all the items in that list:
In> Factorize(Select({{n},Mod(n,3)!=0},2*(1 .. 10))) Out> 2867200; |
A more flexible function one can use is UnFlatten:
In> UnFlatten({a,b,c},"*",1) Out> a*b*c; In> UnFlatten({a,b,c},"+",0) Out> a+b+c; |
Using UnFlatten, the result becomes:
In> UnFlatten(Select({{n},Mod(n,3)!=0},2*(1 .. 10)),"*",1) Out> 2867200; |
Additional example calculations including the results can be found here:
100!; |
ToBase(16,255); FromBase(16,"2FF"); |
Expand((1+x)^5); |
Apply("+",{2,3}); |
Apply({{x,y},x+y},{2,3}); |
D(x)D(x) Sin(x); |
Solve(a+x*y==z,x); |
Taylor(x,0,5) Sin(x); |
Limit(x,0) Sin(x)/x; |
Newton(Sin(x),x,3,0.0001); |
DiagonalMatrix({a,b,c}); |
Integrate(x,a,b) x*Sin(x); |
Factors(x^2-1); |
Apart(1/(x^2-1),x); |
The function g(q,phi,chi) is defined by
To solve this problem, we prepare a separate file with the following Yacas code:
/* Auxiliary function */ g1(n, q, phi, chi) := [ Local(s); s := q^2-n^2; N(Cos(n*chi) * If(s=0, 1/2, /* Special case of s=0: avoid division by 0 */ Sin(Sqrt(s)*phi)/Sin(2*Sqrt(s)*phi) /* now s != 0 */ /* note that Sqrt(s) may be imaginary here */ ) ); ]; /* Main function */ g(q, phi, chi) := [ Local(M, n); M := 16; /* Exp(-M) will be the precision */ /* Use N() to force numerical evaluation */ N(1/2*Sin(q*phi)/Sin(2*q*phi)) + /* Estimate the necessary number of terms in the series */ Sum(n, 1, N(1+Sqrt(q^2+M^2/phi^2)), g1(n, q, phi, chi)) ; ]; /* Parameters */ q:=3.5; phi:=2; /* Make a function for plotting: it must have only one argument */ f(x) := g(q, phi, x); /* Plot from 0 to 2*Pi with 80 points */ Plot2D(f(x), 0: 2*Pi); |
Name this file "fun1" and execute this script by typing
Load("fun1"); |
The default operation of Yacas is to run in the interactive console mode. Yacas accepts several options that modify its operation. Here is a summary of options:
Options can be combined, for example
yacas -pc filename |
Here is a more detailed description of the command-line options.
yacas -c |
yacas -f |
yacas -p |
yacas -t |
yacas [options] {filename} |
yacas -v |
yacas -d |
yacas --patchload |
yacas --init [file] |
yacas --read-eval-print [expression] |
There is also a fallback read-eval-print loop in the kernel; it can be invoked by passing an empty string to this command line option, as --read-eval-print "".
An alternative way to replace the default read-eval-print loop is to write a custom initialization script that implements the read-eval-print loop function REP() instead of yacasinit.ys.
Care has to be taken with this option because a Yacas session may become unusable if the read-eval-print expression doesn't function correctly.
yacas --server |
Commands can be sent to the server by sending a text line as one block of data, and the server will respond back with another text block.
One can test this function by using telnet. First, set up the server by calling
yacas --server 9734 |
telnet 127.0.0.1 9734 |
Some security measures and resource management measures have been taken. No more than 10 connections can be alive at any time, a calculation cannot take more than 30 seconds, and Yacas operates in the secure mode, much like calling an expression by passing it as an argument to the Secure function. This means that no system calls are allowed, and no writing to local files, amongst other things. Something that has not been taken care of yet is memory use. A calculation could take up all memory, but not for longer than 30 seconds.
The server is single-threaded, but has persistent sessions for at most 10 users at a time, from which it can service requests in a sequential order. To make the service multi-threaded, a solution might be to have a proxy in front of the service listening to the port, redirecting it to different processes which get started up for users (this has not been tried yet).
The flag --single-user-server can be passed on to instruct yacas to start in single-user mode. In this mode, unsecure operations can be performed (like reading from and writing to files), and the calculation may take more than 30 seconds. The yacas process will automatically be shut down when the last session is closed or when "Exit();" is sent.
yacas --rootdir [directory] |
yacas --dlldir [directory] |
yacas --archive [file] |
Yacas has an experimental system where files can be compressed into one file, and accessed through this command line option. The advantages are:
An additional savings is due to the fact that the script files are stripped from white spaces and comments, making them smaller and faster loading.
To prepare the compressed library archive, run ./configure with the command line option --enable-archive.
The result should be the archive file scripts.dat. Then launch Yacas with the command line option --archive scripts.dat, with the file scripts.dat in the current directory.
The reason that the scripts.dat file is not built automatically is that it is not tested, at this time, that the build process works on all platforms. (Right now it works on Unix, MacOSX, and Win32.)
Alternatively, configure Yacas with
./configure --enable-archive |
When an archive is present, Yacas will try to load it before it looks for scripts from the library directories. Typing
make archivetest -f makefile.compressor |
The currently supported compression schemes are uncompressed and compressed with minilzo. Script file stripping (removing whitespace and comments) may be disabled by editing compressor.cpp (variable strip_script).
yacas --disable-compiled-plugins |
Disable loading of compiled scripts, in favor of scripts themselves. This is useful when developing the scripts that need to be compiled in the end, or when the scripts have not been compiled yet.
yacas --stacksize |
For a function that would take 4 arguments and has one return value, there would be 5 places reserved on this stack, and the function could call itself recursively 10000 steps deep.
This differs from the MaxEvalDepth mechanism. The MaxEvalDepth mechanism allows one to specify the number of separate stack frames (number of calls, nested), instead of the number of arguments pushed on the stack. MaxEvalDepth was introduced to protect the normal C++ stack.