Let's learn some more


Command-line options

The default operation of Yacas is to run in the interactive console mode. Yacas accepts several options that modify its operation. Options can be combined, for example, yacas -pc filename will execute a file non-interactively. Here is a summary of options:

yacas -c
Inhibit printing of prompts In> and Out>. Useful for non-interactive sessions.

yacas -f
Reads standard input as one file, but executes only the first statement in it. (You may want to use a statement block to have several statements executed.)

yacas -p
Does not use terminal capabilities, no fancy editing on the command line and no escape sequences printed. Useful for non-interactive sessions.

yacas -t
Enable some extra history recall functionality in console mode: after executing a command from the history list, the next unmodified command from the history list will be automatically entered on the command line.

yacas [options] {filename}
Reads and executes commands in the filename and exits. Equivalent to Load().

yacas -v
Prints version information and exits.

yacas -d
Prints the path to the Yacas library directory and exits.

yacas --patchload
Will load every file on the command line not with the normal Load command, but with PatchLoad. This is useful for generating html pages for a web site using the Yacas scripting language, much like you can do with the php scripting language.

yacas --init [file]
Tells the system to load file as the initialization file. Otherwise it loads the file yacasinit.ys from the scripts directory.

yacas --rootdir [directory]/
Tells the system where to find the library scripts. Here, directory/ is a path that is passed to DefaultDirectory, and file names will be simply appended to it, so you should make sure to include a forward or backward slash (depending on the platform) at the end.


Client/server usage

In addition to the console mode, an experimental persistent session facility is provided through the script yacas_client. By means of this script, the user can configure third-party applications to pass commands to a constantly running "Yacas server" and get output. The "Yacas server" is automatically started by yacas_client. It may run on a remote computer; in that case the user should have a user account on the remote computer and privileges to execute yacas_client there, as well as rsh or ssh access. The purpose of yacas_client is to enable users to pass commands to Yacas within a persistent session while running another application such as a text editor.

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 looks 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.


Compound statements

Multiple statements can be grouped together using the [ and ] brackets. The compound [a; b; c;]; evaluates a, then b, then c, and returns the result of evaluating c.

A variable can be declared local to a compound statement block by the function Local(var1, var2,...).


"Threading" of functions

Some functions in Yacas 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. E.g.

Sin({a,b,c});
will result in {Sin(a),Sin(b),Sin(c)}. This functionality is implemented for most normal analytic functions and arithmetic operators.


Functions as lists

Internally, Yacas represents all atomic expressions (numbers and variables) as strings and all compound expressions as lists, just like LISP. Try FullForm(a+b*c); and you will see the text "(+ a (* b c ))" appear on the screen. Also, any expression can be converted to a list by the function Listify() or back to an expression by the function UnList():

In> Listify(a+b*(c+d));
Out> {+,a,b*(c+d)};
In> UnList({Atom("+"),x,1});
Out> x+1;
Note that the first element of the list is the name of the function + which is equivalently represented as Atom("+") and that the subexpression b*(c+d) was not converted to list form.

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} );
would evaluate to 5. Here, {{x,y},x+y} is a list that is treated as a pure function by the operator Apply, the symbols x and y become local variables bound to the parameters passed, and x+y becomes the body of the function.


More on syntax

The syntax is handled by an infix grammar parser. Precedence of operations can be specified explicitly by parentheses ( ) when the normal precedence is not what you want. Most of the time you will enter expressions of the form Func(var1,var2), or using infix operators, a*(b+c), prefix operators: -x, or postfix operators: x++. The parser is case-sensitive and overall the syntax conventions resemble the C language. Last but not least there are the so called "bodied" functions, which unlike normal functions such as f(x,y) keep the last argument outside of the bracketed argument list: "f(x) y". This looks somewhat like a mathematical "operator" f(x) acting on y. A typical example is the function While which looks like "While (predicate) body;". The derivative operator D(x) is also defined as a "bodied" function. Note that if we defined a "bodied" function A with only one argument, we'd have to use it like this: "A() x;" and it would look a bit odd. In this case we could make "A" a prefix operator and then the syntax would become somewhat cleaner: "A x;"

However, regardess 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.) 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. These "special" operators are in no way special, except for their syntax.

All infix, prefix, and postfix operators and bodied functions can be assigned a precedence; 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.

The only 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);" Spaces are not required in situations such as "a:=-1", but this is so only because the operator :=- is actually defined in Yacas.

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;
and then use the function as F(1,2); We might also declare an equivalent infix operation, let's call it "xx", so that we could write simply 1 xx 2. Infix operators must have a precedence, so let's assign it the precedence of the usual division operator. The declaration goes as follows:

In> Infix("xx", OpPrecedence("/"));
Out> True;
In> a xx b := a/b + b/a;
Out> True;
In> 3 xx 2 + 1;
Out> 19/6;
Check the math and note how the precedence works!

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".


Writing simplification rules

Mathematical calculations require versatile transformations on symbolic quantities. Instead of trying to define all possible transformations, Yacas provides a simple and easy to use pattern matching scheme for manipulating expressions according to user-defined rules. Yacas itself is designed as a small core engine executing a large library of rules to match and replace patterns. Examples can be found in the library files "standard", "stdfuncs", "deriv" and "solve" that come with the Yacas distribution.

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);
The underscore after the function object denotes a "postpredicate" that should return True or else there is no match. This predicate may be a complicated expression involving several logical operations, unlike the simple checking of just one predicate in the (n_IsIntegerGreaterThanZero) construct. The postpredicate can also use the variable n (without the underscore). Equivalent ways of defining the same predicate in rule patterns are simply for convenience.

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;
The first rule specifies that if n is an integer, and is greater than zero, the result is True, and the second rule states that otherwise (when the rule with precedence 10 did not apply) the predicate returns 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 has the form;

precedence # pattern _ postpredicate <-- replacement.

The optional precedence must be a positive integer.

Some more examples of rules:

10 # _x + 0 <-- x;
20 # _x - _x <-- 0;
ArcSin(Sin(_x)) <-- x;

Yacas will first try to match the pattern like 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.


Local simplification rules

Sometimes you have an expression, and you want to use specific simplification rules on it that should not be universally applied. This can be done with the /: and the /:: operators. Suppose we have the expression containing things like Ln(a*b), and we want to change these into Ln(a)+Ln(b), the easiest way to do this is using the /: operator as follows:

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.