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 <port> |
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 sequenial 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).
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 <size> |
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.
yacas --execute <expression> |
user% ./yacas -pc --execute '[Echo("answer ",D(x)Sin(x));Exit();]' answer Cos(x) user% |
The script yacas_client reads Yacas commands from the standard input and passes them to the running "Yacas server"; it then waits 2 seconds and prints whatever output Yacas produced up to this time. Usage may look like this:
8:20pm Unix>echo "x:=3" | yacas_client Starting server. [editvi] [gnuplot] True; To exit Yacas, enter Exit(); or quit or Ctrl-c. Type ?? for help. Or type ?function for help on a function. Type 'restart' to restart Yacas. To see example commands, keep typing Example(); In> x:=3 Out> 3; In> 8:21pm Unix>echo "x:=3+x" | yacas_client In> x:=3+x Out> 6; In> 8:23pm Unix>yacas_client -stop In> quit Quitting... Server stopped. 8:23pm Unix> |
Persistence of the session means that Yacas remembered the value of x between invocations of yacas_client. If there is not enough time for Yacas to produce output within 2 seconds, the output will be displayed the next time you call yacas_client.
The "Yacas server" is started automatically when first used and can be stopped either by quitting Yacas or by an explicit option yacas_client -stop, in which case yacas_client does not read standard input.
The script yacas_client reads standard input and writes to standard output, so it can be used via remote shell execution. For example, if an account "user" on a remote computer "remote.host" is accessible through ssh, then yacas_client can be used remotely, like this:
echo "x:=2;" | \ ssh user@remote.host yacas_client |
On a given host computer running the "Yacas server", each user currently may have only one persistent Yacas session.
A variable can be declared local to a compound statement block by the function Local(var1, var2,...).
Sin({a,b,c}); |
In> Listify(a+b*(c+d)); Out> {+,a,b*(c+d)}; In> UnList({Atom("+"),x,1}); Out> x+1; |
Pure functions are the equivalent of "lambda expressions" of LISP; in other words, they are Yacas expressions representing bodies of functions. They are currently implemented using lists and the operator Apply(). The following line:
Apply( {{x,y},x+y} , {2,3} ); |
However, regardless of presentation, internally all functions and operators are equal and merely take a certain number of arguments. The user may define or redefine any operators with either "normal" names such as "A" or names made of one or more of the special symbols + - * / = ` ~ : ! @ # $ ^ & * _ | < > and declare them to be infix, postfix, or prefix operators, as well as normal or bodied functions. (The symbol % is reserved for the result of the previous expression; the decimal point . is special and cannot be part of an operator's name unless the whole name consists entirely of points, e.g. the ".." operator.) Some of these operators and combinations are already defined in Yacas's script library, for instance the "syntactic sugar" operators such as := or <--, but they can be in principle redefined or erased. These "special" operators are in no way special to the system, except for their syntax.
All infix, prefix, postfix operators and bodied functions are assigned a precedence (an integer number); infix operators in addition have a left and a right precedence. All this will only affect the syntax of input and could be arranged for the user's convenience. For example, the infix operator "*" has precedence smaller than that of the infix operator "+", and therefore an expression such as a+b*c is interpreted in the conventional way, i.e. a+(b*c).
An important caveat is to make sure you always type a space between any symbols that could make up an operator. For instance, after you define a new function "@@(x):=x^2;" expressions such as "a:=@@(b);" typed without spaces will cause an error unless you also define the operator ":=@@". This is because the parser will not stop at ":=" when trying to make sense of that expression. The correct way to deal with this is to insert a space: "a:= @@(b)". Similarly, a!+3 will cause an error unless you define "!+", so use a space: a! +3.
Let's now have a hands-on primer for these syntactic features. Suppose we wanted to define a function F(x,y)=x/y+y/x. We could use the standard syntax:
In> F(a,b) := a/b + b/a; Out> True; |
In> Infix("xx", OpPrecedence("/")); Out> True; In> a xx b := a/b + b/a; Out> True; In> 3 xx 2 + 1; Out> 19/6; |
We have chosen the name "xx" just to show that we don't need to use the special characters in the infix operator's name. However we must define this operator as infix before using it in expressions, or we'd get syntax errors.
Finally, we might decide to be completely flexible with this important function and also define it as a mathematical operator ##. First we define ## as a "bodied" function and then proceed as before:
In> Bodied("##", OpPrecedence("/")); Out> True; In> ##(a) b := a xx b; Out> True; In> ##(1) 3 + 2; Out> 16/3; |
We have used the name ## but we could have used any other name such as xx or F or even _-+@+-_. Apart from possibly confusing yourself, it doesn't matter what you call the functions you define.
There is currently one limitation in Yacas: once a function name is declared as infix (prefix, postfix) or bodied, it will always be interpreted that way. If we declare f to be "bodied", we may later define different functions named f with different numbers of arguments, however all of these functions must be "bodied".
One simple application of pattern-matching rules is to define new functions. (This is actually the only way Yacas can learn about new functions.) As an example, let's define a function f that will evaluate factorials of non-negative integers. We'll first define a predicate to check whether our argument is indeed a non-negative integer, and we'll use this predicate and the obvious recursion f(n)=n*f(n-1) to evaluate the factorial. All this is accomplished by the following three lines:
10 # f(0) <-- 1; 20 # f(n_IsIntegerGreaterThanZero) <-- n*f(n-1); IsIntegerGreaterThanZero(_n) <-- IsInteger(n) And n>0; |
We have first defined two "simplification rules" for a new function f(). Then we realized that we need to define a predicate IsIntegerGreaterThanZero(). A predicate equivalent to IsIntegerGreaterThanZero() is actually already defined in the standard library and it's called IsPositiveInteger, so it was not necessary, strictly speaking, to define our own predicate to do the same thing; we did it here just for illustration.
The first two lines recursively define a factorial function f(n)=n*(n-1)*...*1. The rules are given precedence values 10 and 20, so the first rule will be applied first. Incidentally, the factorial is also defined in the standard library as a postfix operator "!" and it is bound to an internal routine much faster than the recursion in our example.
The operator <-- defines a rule to be applied to a specific function. (The <-- operation cannot be applied to an atom.) The _n in the rule for IsIntegerGreaterThanZero() specifies that any object which happens to be the argument of that predicate is matched and assigned to the local variable n. The expression to the right of <-- can use n (without the underscore) as a variable.
Now we consider the rules for the function f. The first rule just specifies that f(0) should be replaced by 1 in any expression. The second rule is a little more involved. n_IsIntegerGreaterThanZero is a match for the argument of f, with the proviso that the predicate IsIntegerGreaterThanZero(n) should return True, otherwise the pattern is not matched. The underscore operator is to be used only on the left hand side of the rule operator <--.
There is another, slightly longer but equivalent way of writing the second rule:
20 # f(_n)_(IsIntegerGreaterThanZero(n)) <-- n*f(n-1); |
Precedence values for rules are given by a number followed by the # operator. This number determines the ordering of precedence for the pattern matching rules, with 0 the lowest allowed precedence value, i.e. rules with precedence 0 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. In our example, the rule f(0) <-- 1 must be applied earlier than the recursive rule, or else the recursion will never terminate. But as long as there are no other rules concerning the function f, the assignment of numbers 10 and 20 is arbitrary, and they could have been 500 and 501 just as well.
Predicates can be combined: for example, IsIntegerGreaterThanZero() could also have been defined as:
10 # IsIntegerGreaterThanZero(n_IsInteger) _(n>0) <-- True; 20 # IsIntegerGreaterThanZero(_n) <-- False; |
In the above example, the (n>0) clause is added after the pattern and allows the pattern to match only if this predicate return True. This is a useful syntax for defining rules with complicated predicates. There is no difference between the rules F(n_IsPositiveInteger)<--... and F(_n)_(IsPositiveInteger(n)) <-- ... except that the first syntax is a little more concise.
The left hand side of a rule expression has the following form:
precedence # pattern _ postpredicate <-- replacement.
The optional precedence must be a positive integer.
10 # _x + 0 <-- x; 20 # _x - _x <-- 0; ArcSin(Sin(_x)) <-- x; |
Yacas will first try to match the pattern as a template. Names preceded or followed by an underscore can match any one object: a number, a function, a list, etc. Yacas will assign the relevant variables as local variables within the rule, and try the predicates as stated in the pattern. The post-predicate (defined after the pattern) is tried after all these matched. As an example, the simplification rule _x - _x <--0 specifies that the two objects at left and at right of the minus sign should be the same.
There is a slightly more complex and general way of defining rules using the functions Rule(), RuleBase() and RulePattern(). However, the standard library defines the "... # ... <--..." construct which is much more readable and usually sufficiently flexible.
In> Sin(x)*Ln(a*b) Out> Sin(x)*Ln(a*b); In> % /: { Ln(_x*_y) <- Ln(x)+Ln(y) } Out> Sin(x)*(Ln(a)+Ln(b)); |
A whole list of simplification rules can be built up in the list, and they will be applied to the expression on the left hand side of /:.
The forms the patterns can have are one of:
pattern <- replacement pattern , replacement pattern , postpredicate , replacement |
Note that for these local rules, <- should be used instead of <--. The latter would be used to define a "global" rule.
The /: operator traverses an expression much like Subst() does, i.e. top down, trying to apply the rules from the beginning of the list of rules to the end of the list of rules. If no rules can be applied to the whole expression, it will try the sub-expressions of the expression being analyzed.
It might be sometimes necessary to use the /:: operator, which repeatedly applies the /: operator until the result does not change any more. Caution is required, since rules can contradict each other, and that could result in an infinite loop. To detect this situation, just use /: repeatedly on the expression. The repetitive nature should become apparent.