yacas -c |
yacas -f |
yacas -p |
yacas -t |
yacas [options] {filename} |
yacas -v |
yacas -d |
yacas --patchload |
yacas --init [file] |
yacas --rootdir [directory]/ |
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.
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, 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; |
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 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.
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.