yacas -c |
yacas -f |
yacas -p |
yacas -t |
yacas {filename} |
yacas -v |
yacas -d |
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> |
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 instance, 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 |
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; |
Apply( {{x,y},x+y} , {2,3} ); |
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.
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 re-defined too. 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 constructions. 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; |
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; |
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. Tll this is accomplished by the following three lines:
IsIntegerGreaterThanZero(_n) <-- IsInteger(n) And n>0; 10 # f(0) <-- 1; 20 # f(n_IsIntegerGreaterThanZero) <-- n*f(n-1); |
The operator <-- defines a rule to be applied to a specific function. 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 then just 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 are actually another 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.
Additional predicates can be specified too. IsIntegerGreaterThanZero could also have been defined as:
10 # IsIntegerGreaterThanZero(n_IsInteger)_(n>0) <-- True; 20 # IsIntegerGreaterThanZero(_n) <-- False; |
The left hand side of a rule has the form
"pattern _ postpredicate <-- replacement".
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.
Some more examples of rules:
_x + 0 <-- x; _x - _x <-- 0; ArcSin(Sin(_x)) <-- x; |
There is a slightly more complex and general way of defining rules using the RuleBase() and Rule() functions, but the "... # ... <-- ..." construct is usually sufficiently flexible and much more readable.
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)); |
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 in stead of <-- which defines a global rule.
The /: operator traverses an expression much like Subst does: top down, trying to apply the rules from the begin of the list of rules to the end of the list of rules. If the rules cannot be applied to a sub-expression, it will try the sub expressions of the expression being analyzed.
It might be necessary sometimes to use the /:: operator, which repeatedly applies the /: operator until the result doesn't change any more. Caution is required, since rules can contradict eachother, and that could result in an infinite loop. To detect this situation, just use /: repeatedly on the expression. The repetitive nature should become apparent.