FullForm | print an expression in LISP-format |
Echo | high-level printing routine |
PrettyForm | print an expression nicely with ASCII art |
EvalFormula | print an evaluation nicely with ASCII art |
TeXForm | export expressions to LaTeX |
CForm | export expression to C++ code |
IsCFormable | check possibility to export expression to C++ code |
Write | low-level printing routine |
WriteString | low-level printing routine for strings |
Space | print one or more spaces |
NewLine | print one or more newline characters |
FromFile | connect current input to a file |
FromString | connect current input to a string |
ToFile | connect current output to a file |
ToString | connect current output to a string |
Read | read an expression from current input |
ToStdout | select initial output stream for output |
ReadCmdLineString | read an expression from command line and return in string |
LispRead, LispReadListed | read expressions in LISP syntax |
ReadToken | read a token from current input |
Load | evaluate all expressions in a file |
Use | load a file, but not twice |
DefLoad | load a .def file |
FindFile | find a file in the current path |
PatchLoad | execute commands between <? and ?> in file |
Nl | the newline character |
V, Verbose | set verbose output mode |
Plot2D | adaptive two-dimensional plotting |
Plot3DS | three-dimensional (surface) plotting |
XmlExplodeTag | convert XML strings to tag objects |
DefaultTokenizer, XmlTokenizer, CTokenizer | select a syntax for parsing the input |
OMForm, OMParse | convert expressions to and from OpenMath |
FullForm(expr) |
This can be useful if you want to study the internal representation of a certain expression.
In> FullForm(a+b+c); (+ (+ a b )c ) Out> a+b+c; In> FullForm(2*I*b^2); (* (Complex 0 2 )(^ b 2 )) Out> Complex(0,2)*b^2; |
The first example shows how the expression a+b+c is internally represented. In the second example, 2*I is first evaluated to Complex(0,2) before the expression is printed.
Echo(item) Echo(list) Echo(item,item,item,...) |
list -- a list of items to be printed
If there is one argument, and it is a list, Echo will print all the entries in the list subsequently to the current output, followed by a newline. Any strings in the list are printed without quotation marks. All other entries are followed by a space.
Echo can be called with a variable number of arguments, they will all be printed, followed by a newline.
Echo always returns True.
In> Echo(5+3); 8 Out> True; In> Echo({"The square of two is ", 2*2}); The square of two is 4 Out> True; In> Echo("The square of two is ", 2*2); The square of two is 4 Out> True; |
Note that one must use the second calling format if one wishes to print a list:
In> Echo({a,b,c}); a b c Out> True; In> Echo({{a,b,c}}); {a,b,c} Out> True; |
PrettyForm(expr) |
In> Taylor(x,0,9)Sin(x) Out> x-x^3/6+x^5/120-x^7/5040+x^9/362880; In> PrettyForm(%) 3 5 7 9 x x x x x - -- + --- - ---- + ------ 6 120 5040 362880 Out> True; |
EvalFormula(expr) |
In> EvalFormula(Taylor(x,0,7)Sin(x)) 3 5 x x Taylor( x , 0 , 5 , Sin( x ) ) = x - -- + --- 6 120 |
TeXForm(expr) |
In> TeXForm(Sin(a1)+2*Cos(b1)) Out> "$\sin a_{1} + 2 \cos b_{1}$"; |
CForm(expr) |
In> CForm(Sin(a1)+2*Cos(b1)); Out> "sin(a1) + 2 * cos(b1)"; |
IsCFormable(expr) IsCFormable(expr, funclist) |
funclist -- list of "allowed" function atoms
A Yacas expression is considered exportable if it contains only functions that can be translated into C++ (e.g. UnList cannot be exported). All variables and constants are considered exportable.
The verbose option prints names of functions that are not exportable.
The second calling format of IsCFormable can be used to "allow" certain function names that will be available in the C++ code.
In> IsCFormable(Sin(a1)+2*Cos(b1)) Out> True; In> V(IsCFormable(1+func123(b1))) IsCFormable: Info: unexportable function(s): func123 Out> False; |
In> IsCFormable(1+func123(b1), {func123}) Out> True; |
Write(expr, ...) |
In> Write(1); 1Out> True; In> Write(1,2); 1 2Out> True; |
Write does not write a newline, so the Out> prompt immediately follows the output of Write.
WriteString(string) |
In> Write("Hello, world!"); "Hello, world!"Out> True; In> WriteString("Hello, world!"); Hello, world!Out> True; |
This example clearly shows the difference between Write and WriteString. Note that Write and WriteString do not write a newline, so the Out> prompt immediately follows the output.
Space() Space(nr) |
In> Space(5); Out> True; |
NewLine() NewLine(nr) |
In> NewLine(); Out> True; |
FromFile(name) body |
body - expression to be evaluated
2 + 5; |
Then we can have the following dialogue:
In> FromFile("foo") res := Read(); Out> 2+5; In> FromFile("foo") res := ReadToken(); Out> 2; |
FromString(str) body; |
body -- expression to be evaluated
In> FromString("2+5; this is never read") \ res := Read(); Out> 2+5; In> FromString("2+5; this is never read") \ res := Eval(Read()); Out> 7; |
ToFile(name) body |
body -- expression to be evaluated
If the file is opened again, the old contents will be overwritten. This is a limitation of ToFile: one cannot append to a file that has already been created.
In> ToFile("expr1.c") WriteString( CForm(Sqrt(x-y)*Sin(x)) ); Out> True; |
sqrt(x-y)*sin(x) |
As another example, take a look at the following command:
In> [ Echo("Result:"); \ PrettyForm(Taylor(x,0,9) Sin(x)); ]; Result: 3 5 7 9 x x x x x - -- + --- - ---- + ------ 6 120 5040 362880 Out> True; |
Now suppose one wants to send the output of this command to a file. This can be achieved as follows:
In> ToFile("out") [ Echo("Result:"); \ PrettyForm(Taylor(x,0,9) Sin(x)); ]; Out> True; |
After this command the file out contains:
Result: 3 5 7 9 x x x x x - -- + --- - ---- + ------ 6 120 5040 362880 |
ToString() body |
In> str := ToString() [ WriteString( \ "The square of 8 is "); Write(8^2); ]; Out> "The square of 8 is 64"; |
Read() |
In> FromString("2+5;") Read(); Out> 2+5; In> FromString("") Read(); Out> EndOfFile; |
ToStdout() body |
In> ToString()[Echo("aaaa");ToStdout()Echo("bbbb");]; bbbb Out> "aaaa " |
ReadCmdLineString(prompt) |
The result is returned in a string, so it still needs to be parsed.
This function will typically be used in situations where one wants a custom read-eval-print loop.
In> ReEvPr() := \ In> While(True) [ \ In> PrettyForm(Deriv(x) \ In> FromString(ReadCmdLineString("Deriv> "):";")Read()); \ In> ]; Out> True; |
Then one can invoke the command, from which the following interaction might follow:
In> ReEvPr() Deriv> Sin(a^2*x/b) / 2 \ | a * x | 2 Cos| ------ | * a * b \ b / ---------------------- 2 b Deriv> Sin(x) Cos( x ) Deriv> |
LispRead() LispReadListed() |
The Yacas expression a+b is written in the LISP syntax as (+ a b). The advantage of this syntax is that it is less ambiguous than the infix operator grammar that Yacas uses by default.
The function LispReadListed reads a LISP expression and returns it in a list, instead of the form usual to Yacas (expressions). The result can be thought of as applying Listify to LispRead. The function LispReadListed is more useful for reading arbitrary LISP expressions, because the first object in a list can be itself a list (this is never the case for Yacas expressions where the first object in a list is always a function atom).
In> FromString("(+ a b)") LispRead(); Out> a+b; In> FromString("(List (Sin x) (- (Cos x)))") \ LispRead(); Out> {Sin(x),-Cos(x)}; In> FromString("(+ a b)")LispRead() Out> a+b; In> FromString("(+ a b)")LispReadListed() Out> {+,a,b}; |
ReadToken() |
A token is for computer languages what a word is for human languages: it is the smallest unit in which a command can be divided, so that the semantics (that is the meaning) of the command is in some sense a combination of the semantics of the tokens. Hence a := foo consists of three tokens, namely a, :=, and foo.
The parsing of the string depends on the syntax of the language. The part of the kernel that does the parsing is the "tokenizer". Yacas can parse its own syntax (the default tokenizer) or it can be instructed to parse XML or C++ syntax using the directives DefaultTokenizer, XmlTokenizer, or CTokenizer. Setting a tokenizer is a global action that affects all ReadToken calls.
In> FromString("a := Sin(x)") While \ ((tok := ReadToken()) != EndOfFile) \ Echo(tok); a := Sin ( x ) Out> True; |
We can read some junk too:
In> FromString("-$3")ReadToken(); Out> -$; |
Load(name) |
Use(name) |
The purpose of this function is to make sure that the file will at least have been loaded, but is not loaded twice.
DefLoad(name) |
FindFile(name) |
FindFile("") returns the name of the default directory (the first one on the search path).
PatchLoad(name) |
This is similar to the way PHP works. You can have a static text file with dynamic content generated by Yacas.
Nl() |
Note that the second letter in the name of this command is a lower case L (from "line").
In> WriteString("First line" : Nl() : "Second line" : Nl()); First line Second line Out> True; |
V(expression) |
The function is currently implemented using a global variable Verbose which can be set to True or False.
In> OldSolve({x+2==0},{x}) Out> {{-2}}; In> V(OldSolve({x+2==0},{x})) Entering OldSolve From x+2==0 it follows that x = -2 x+2==0 simplifies to True Leaving OldSolve Out> {{-2}}; |
Plot2D(f(x)) Plot2D(f(x), a:b) Plot2D(f(x), a:b, option=value) Plot2D(f(x), a:b, option=value, ...) Plot2D(list, ...) |
list -- list of functions to plot
a, b -- numbers, plotting range in the x coordinate
option -- atom, option name
value -- atom, number or string (value of option)
The function parameter f(x) must evaluate to a Yacas expression containing at most one variable. (The variable does not have to be called x.) Also, N(f(x)) must evaluate to a real (not complex) numerical value when given a numerical value of the argument x. If the function f(x) does not satisfy these requirements, an error is raised.
Several functions may be specified as a list and they do not have to depend on the same variable, for example, {f(x), g(y)}. The functions will be plotted on the same graph using the same coordinate ranges.
If you have defined a function which accepts a number but does not accept an undefined variable, Plot2D will fail to plot it. Use NFunction to overcome this difficulty.
Data files are created in a temporary directory /tmp/plot.tmp/ unless otherwise requested. File names and other information is printed if the Verbose option is switched on using V().
The current algorithm uses Newton-Cotes quadratures and some heuristics for error estimation (see The Yacas book of algorithms, Chapter 3, Section 1 ). The initial grid of points+1 points is refined between any grid points a, b if the integral Integrate(x,a,b)f(x) is not approximated to the given precision by the existing grid.
Default plotting range is -5:5. Range can also be specified as x= -5:5 (note the mandatory space separating "=" and "-"); currently the variable name x is ignored in this case.
Options are of the form option=value. Currently supported option names are: "points", "precision", "depth", "output", "filename", "yrange". Option values are either numbers or special unevaluated atoms such as data. If you need to use the names of these atoms in your script, strings can be used (e.g. output="gnuplot"). Several option/value pairs may be specified (the function Plot2D has a variable number of arguments).
Other options may be supported in the future.
The current implementation can deal with a singularity within the plotting range only if the function f(x) returns Infinity, -Infinity or Undefined at the singularity. If the function f(x) generates a numerical error and fails at a singularity, Plot2D will fail if one of the grid points falls on the singularity. (All grid points are generated by bisection so in principle the endpoints and the points parameter could be chosen to avoid numerical singularities.)
*WIN32
On the Wind*ws platforms, there are some limitations:
In> Plot2D({(2+x)/(2-x),Exp(y)},-1.8:1.5) Out> True; In> V(Plot2D(Sin(1/x),0:1, depth=4, \ In> precision=0.001, output=plotutils)) Plot2D: using 65 points for function Sin(1/x) Plot2D'datafile: created file '/tmp/plot.tmp/data1' Out> True; |
If a function takes a long time to evaluate, one can use MakeFunctionPlugin (a Unix-specific tool) to compile the function and plot it faster:
In> f(x) := Cos( (Abs(Pi/x))^1.5 ) Out> True; In> Time( Plot2D(f(x),0:1) ) 94.93 seconds taken Out> True; In> MakeFunctionPlugin("f1", f(x)) Function f1(x) loaded from plugins.tmp/ libf1_plugin_cc.so Out> True; In> Time( Plot2D(f1(x),0:1) ) 6.97 seconds taken Out> True; |
Plot3DS(f(x,y)) Plot3DS(f(x,y), a:b, c:d) Plot3DS(f(x,y), a:b, c:d, option=value) Plot3DS(f(x,y), a:b, c:d, option=value, ...) Plot3DS(list, ...) |
list -- list of functions to plot
a, b, c, d -- numbers, plotting ranges in the x and y coordinates
option -- atom, option name
value -- atom, number or string (value of option)
The function parameter f(x,y) must evaluate to a Yacas expression containing at most two variables. (The variables do not have to be called x and y.) Also, N(f(x,y)) must evaluate to a real (not complex) numerical value when given numerical values of the arguments x, y. If the function f(x,y) does not satisfy these requirements, an error is raised.
Several functions may be specified as a list but they have to depend on the same symbolic variables, for example, {f(x,y), g(y,x)}, but not {f(x,y), g(a,b)}. The functions will be plotted on the same graph using the same coordinate ranges.
If you have defined a function which accepts a number but does not accept an undefined variable, Plot3DS will fail to plot it. Use NFunction to overcome this difficulty.
Data files are created in a temporary directory /tmp/plot.tmp/ unless otherwise requested. File names and other information is printed if the Verbose option is switched on using V().
The current algorithm uses Newton-Cotes cubatures and some heuristics for error estimation (see The Yacas book of algorithms, Chapter 3, Section 1 ). The initial rectangular grid of xpoints+1*ypoints+1 points is refined within any rectangle where the integral of f(x,y) is not approximated to the given precision by the existing grid.
Default plotting range is -5:5 in both coordinates. A range can also be specified with a variable name, e.g. x= -5:5 (note the mandatory space separating "=" and "-"). The variable name x should be the same as that used in the function f(x,y). If ranges are not given with variable names, the first variable encountered in the function f(x,y) is associated with the first of the two ranges.
Options are of the form option=value. Currently supported option names are "points", "xpoints", "ypoints", "precision", "depth", "output", "filename", "xrange", "yrange", "zrange". Option values are either numbers or special unevaluated atoms such as data. If you need to use the names of these atoms in your script, strings can be used (e.g. output="gnuplot"). Several option/value pairs may be specified (the function Plot3DS has a variable number of arguments).
Other options may be supported in the future.
The current implementation can deal with a singularity within the plotting range only if the function f(x,y) returns Infinity, -Infinity or Undefined at the singularity. If the function f(x,y) generates a numerical error and fails at a singularity, Plot3DS will fail only if one of the grid points falls on the singularity. (All grid points are generated by bisection so in principle the endpoints and the xpoints, ypoints parameters could be chosen to avoid numerical singularities.)
The filename option is optional if using graphical backends, but can be used to specify the location of the created data file.
*WIN32
Same limitations as Plot2D.
In> Plot3DS(a*b^2) Out> True; In> V(Plot3DS(Sin(x)*Cos(y),x=0:20, y=0:20,depth=3)) CachedConstant: Info: constant Pi is being recalculated at precision 10 CachedConstant: Info: constant Pi is being recalculated at precision 11 Plot3DS: using 1699 points for function Sin(x)*Cos(y) Plot3DS: max. used 8 subdivisions for Sin(x)*Cos(y) Plot3DS'datafile: created file '/tmp/plot.tmp/data1' Out> True; |
XmlExplodeTag(xmltext) |
The following subset of XML syntax is supported currently:
The tag options take the form paramname="value".
If given an XML tag, XmlExplodeTag returns a structure of the form XmlTag(name,params,type). In the returned object, name is the (capitalized) tag name, params is an assoc list with the options (key fields capitalized), and type can be either "Open", "Close" or "OpenClose".
If given a plain text string, the same string is returned.
In> XmlExplodeTag("some plain text") Out> "some plain text"; In> XmlExplodeTag("<a name=\"blah blah\" align=\"left\">") Out> XmlTag("A",{{"ALIGN","left"}, {"NAME","blah blah"}},"Open"); In> XmlExplodeTag("</p>") Out> XmlTag("P",{},"Close"); In> XmlExplodeTag("<br/>") Out> XmlTag("BR",{},"OpenClose"); |
DefaultTokenizer() XmlTokenizer() CTokenizer() |
The Yacas environment currently supports some experimental tokenizers for various syntaxes. DefaultTokenizer switches to the tokenizer used for default Yacas syntax. XmlTokenizer switches to an XML syntax and CTokenizer to a C syntax. Note that setting the tokenizer is a global side effect. One typically needs to switch back to the default tokenizer when finished reading the special syntax.
Care needs to be taken when kernel errors are raised during a non-default tokenizer operation (as with any global change in the environment). Errors need to be caught with the TrapError function. The error handler code should re-instate the default tokenizer, or else the user will be unable to continue the session (everything a user types will be parsed using a non-default tokenizer).
When reading XML syntax, the supported formats are the same as those of XmlExplodeTag. The parser does not validate anything in the XML input. After an XML token has been read in, it can be converted into an Yacas expression with XmlExplodeTag. Note that when reading XML, any plain text between tags is returned as one token. Any malformed XML will be treated as plain text.
CTokenizer is an implementation of a C/C++ tokenizer. It is still work in progress.
In> [XmlTokenizer(); q:=ReadToken(); \ DefaultTokenizer();q;] <a>Out> <a>; |
Note that:
OMForm(expression) OMParse() |
In> str:=ToString()OMForm(2+Sin(a*3)) Out> "<OMOBJ> <OMA> <OMS cd="arith1" name="plus"/> <OMI>2</OMI> <OMA> <OMS cd="transc1" name="sin"/> <OMA> <OMS cd="arith1" name="times"/> <OMV name="a"/> <OMI>3</OMI> </OMA> </OMA> </OMA> </OMOBJ> "; In> FromString(str)OMParse() Out> 2+Sin(a*3); |