# Let's learn some more

##

Using Yacas from the console

###

Command-line options

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:

- filename ... (read and execute a file or several files)
- `-c` (omit line prompts)
- `-d` (print default directory)
- `-v` (print version information)
- `-f` (execute standard input as one statement)
- `-p` (do not use terminal capabilities)
- `-t` (enable extra history features)
- `--archive` filename (use a given library archive file)
- `--dlldir` directory (specify default directory for plugins)
- `--init` filename (use a given initial file)
- `--patchload` (use `PatchLoad` to load files)
- `--read-eval-print` expression (call this expression for the read-eval-print loop)
- `--rootdir` directory (specify default directory for scripts)
- `--server` port (start `Yacas` as a network server on given port)
- `--verbose-debug` (turn on showing some additional debugging information on screen)
- `--disable-compiled-plugins` (disable loading of compiled plugins, loading the script versions instead)
- `--stacksize` size (change size of stack arguments are stored on)

Options can be combined, for example

    yacas -pc filename

will read and execute the file `filename` non-interactively without using terminal capabilities and without printing prompts.

Here is a more detailed description of the command-line 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. (This is the same information as returned by `Version()`.)

    yacas -d

Prints the path to the `Yacas` default library directory (this information is compiled into the `Yacas` executable) and exits.

    yacas --patchload

Will load every file on the command line with the `PatchLoad` command instead of the normal `Load` command, 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. By default it loads the file `yacasinit.ys` from the scripts directory. Thus for customization one has two options: write a `~/.yacasrc` file with initialization code (as it is loaded after the initialization script is loaded), or write a custom initialization script that first uses `yacasinit.ys` and adds some extra custom code.

    yacas --read-eval-print [expression]

Call `expression` for the read-eval-print loop. The default read-eval-print loop is implemented in the initialization script `yacasinit.ys` as the function `REP`. The default behavior is therefore equivalent to `--read-eval-print` `REP`.

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>

On some platforms server mode can be enabled at build time by passing the flag `--enable-server` to the `./configure` script. `Yacas` then allows you to pass the flag `--server` with a port number behind it, and the `Yacas` executable will listen to the socket behind that port instead of waiting for user input on the console.

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

and then invoke `telnet` in another window, for example:

    telnet 127.0.0.1 9734

Then type a line of Yacas input and hit Enter. The result will be one line that you will get back from the Yacas server.

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]

Tells the system where to find the library scripts. Here, `directory` is a path that is passed to `DefaultDirectory`. It is also possible to give a list of directories, separated by a colon, e.g. `yacas --rootdir scripts/:morescripts/`. Note that it is not necessary to append a trailing slash to the directory names.

    yacas --dlldir [directory]

Tells the system where to find the plugins. Here `directory` is a path that is passed to `DllDirectory`. It is also possible to give a list of directories separated by a colon. The default value is specified at compile time, usually `/usr/local/lib/yacas`.

    yacas --archive [file]

Use a compressed archive instead of the script library.

`Yacas` has an experimental system where files can be compressed into one file, and accessed through this command line option. The advantages are:

- Smaller disk/memory use (useful if `Yacas` is used on small hand-held computers).
- No problems with directory path separators: "`path`/`file`" will always resolve to the right file, no matter what platform (read: Windows) it runs on.
- The start-up time of the program might improve a little, since a smaller file is loaded from disk (disk access being slow), and then decompressed in memory, which might be a lot faster than loading from disk.

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

and the archive file `scripts.dat` will be created in the `ramscripts/` subdirectory.

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

in the `ramscripts/` directory runs all the test scripts using the archived files.

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>

Yacas maintains an internal stack for arguments. For nested function calls, all arguments currently used are on this stack. The size of this stack is 50000 be default.

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.

###

Client/server usage

In addition to the interactive console sessions, a remote persistent session facility is provided through the script `yacas_client`. (This is currently only supported on Unix platforms.) 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 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.

##

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 would 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, 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;

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 expression has the following 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;

The last rule has no explicit precedence specified in it (the precedence will be assigned automatically by the system).

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

##

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 such as `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.