Sebastian Ritterbusch
Rascal is based on an extremely modular approach; depending on the user's needs, various powerful modules can be compiled into the system. Thus Rascal can be a light-weight tool as well as a very powerful computational system. This user documentation is created during compilation and describes the included features.
Invoking the program leads to a prompt where you can enter expressions. The expressions or definitions are being evaluated as soon you press return. A semicolon at the end suppresses any output. As an example:
7
>5+2;
>
>MyVariable2*MyVariable2
49
>
The user can also define functions of one variable (n-ary functions can be implemented using vectors or matrices as arguments). The names follow the same rules as the names of variables:
>MyNiceFunction17(2)
14
>EvalAt12(F)=F(12);
>EvalAt12(sqr)
144
Conditional expressions can be realized using the C-style ``?:''-operator:
17
>abs(x)=x>0?x:-x;
>abs(-18)
18
>abs(12)
12
>
You can exit Rascal by entering ``quit'' followed by a return.
If Rascal was compiled using the ``libreadline'', then you may use the cursor-keys to flip back to commands and results from before, as well as benefit of command completition using the ``tab''-key.
Furthermore the different modules predefine functions, which are documented in the following sections.
Using double precision floating-point numbers overcomes this problem, but
rounding errors may occur. A number is being interpreted as a floating-point
number when there is a decimal-point within or at the end of the number
and/or an exponent. As an example 1.234e+12 represents
. Be warned that there is no exact representation for 0.1 and
many many other numbers in binary floating-point representations.
All operands like +,-,*,/,^ ,% are defined for integers and doubles, together with standard functions sin, cos, tan, cot, asin, acos, atan, acot, sinh, cosh, tanh, coth, asinh, acosh, atanh, acoth, log, exp, sqrt, sqr. A postfix ! computes the factorial of the operand (currently just in integer).
The values can be compared using the operators ==,!=,<,<=,>,>=, logical expression can be connected using &,| and the logical negation ~ .
Besides strings can be used as "evaluation variable": As all the standard operators and functions are also defined for strings, one can determine the expression Rascal would evaluate if the argument only had been a real value.
Strings are enclosed in quotes:
>f(f("y"))
"y*y+2*(y)*y*y+2*(y)+2*(y*y+2*(y))"
>strlen(f(f("y")))
49
But the operators ==,!= are still used to compare two strings. The strlen function returns the length of the string.
In Rascal vectors are just matrices with either just one row or one column. You can enter a matrix by using brackets, where values within a line are separated by spaces, lines are separated by semicolons. matrices of same size can be added and subtracted using the usual operators:
[1 2;3 4;5 6]
>B=[-3 -4;2 7;2 9]
[-3 -4;2 7;2 9]
>A+B
[-2 -2;5 11;7 15]
>A-B
[4 6;1 -3;3 -3]
>rows(A)
3
>cols(A)
2
>size(A)
3
Scalars can be multiplied to the matrices and two matrices can be multiplied if the number of columns of the first matches the number of rows of the second. Dividing by a matrix means multiplying with the inverse, of course this is only defined for quadratic matrices. The functions rows, cols, size return the number of rows, columns and maximum of the two values of a matrix or vector.
Types using the matrix inversion must be able to be compared to integers. If there is no inverse, an empty matrix is being returned. You may compute the determinant using the det function.
[11]
>[1;2]*[3 4]
[3 4;6 8]
>A=[1 3;4 13];
>det(A)
1
>1/A
[13 -3;-4 1]
>[2 0;0 3]/A
[26 -6;-12 3]
>
The cells of matrices can be of any type; here an example for an integer, double, matrix, string matrix:
>A(1,2)
2.340000
The summation of matrices and scalars is defined as the addition between the matrix and the identity of same size times the scalar. Vector valued functions can be defined easily, also with multiple arguments.
>f(8)
[64;10]
>f([1 2;3 4])
[[7 10;15 22];[3 2;3 6]]
>f[1 2;3 4]
[[7 10;15 22];[3 2;3 6]]
>g(x,y,z)=[x+y*z x*z];
>g(12,-5,2)
[2 24]
>
The support for loops is experimental- it is very likely that the syntax will change in future releases.
Rascal supports C(++)-style loops to a certain extend. The syntax is
for( <initialization> ; <condition> ; <expression 1> ) <expression 2>
First the initialization expression is being evaluated- this should be used to initialize the variable used for the loop. If the given condition is true then expression 2 and 1 are evaluated (in that order) until the condition is false after the evaluation of 1. Have a look at this example:
>for(x=1;x<10;x++) sum=sum+x;
>sum
4950
The above example computes the sum of the numbers between 0 and 10. If you would like to see the intermediate results, leave out the semicolon at the end of the for statement:
1
3
6
10
15
21
28
36
45
In contrast to C(++) there is no "comma-operator" in rascal yet, thus you cannot use it in for-statements. Sorry.
You can interrupt running computations in Rascal by sending a SIGINT signal- just press CTRL-C. Rascal will then try to stop all computations. If you press CTRL-C again before Rascal was able to get back to the interactive shell, Rascal will terminate.
If you press CTRL-C while you are in the interactive shell, Rascal will beep you and terminate if you repeat your signal.
Thus if you "for"-statement doesn't seem to terminate or you were trying to compute the factorial of "100000", just press CTRL-C and you won't lose you work done before.
complex(4,2)
>i=complex(0,1);
>(5+3*i)*(2+i)
(7+4*i)
>
The real and imaginary part of a complex number can be accessed like the cells of vectors: The first cell is the real, the second the imaginary value. Additionally like matrices complex values can consist of all different types of data. The "transpose" of a complex value is the complex conjugate.
>f(complex("x",4))
complex("x*x+4","4*(x)")
>(2+3*complex(0,1))'
complex(2,-3)
The following standard functions are defined for complex: exp, log, pow, sqrt, sin, cos, tan, cot, asin, acos, atan, acot, sinh, cosh, tanh, coth, asinh, acoth, atanh, acoth. The function arg computes the argument, the function abs the absolute value. The standard functions sqrt, pow for integers and doubles are being overridden by the corresponding complex versions.
The log represents the main value of the natural logarithm, the pow(a,b) or a^b is evaluated as
, the sqrt(a)
as
and the rest is determined based on these functions and the
pendants on the real axis.
One could also define a complex out of matrices, but this is not advisable as this easily gets confusing and Rascal prefers complex values within matrices. You should also be aware that naively plugging interval-datatypes into a generic complex datatype will not yield verified results: As an example the arg function has to be treated differently, and most other standard functions depend on this.
This module introduces polynomials in one variable to Rascal. You may create polynomials by the "poly"-function, but it is very useful to assign the variable X the monome 1*X^ 1. Then you can easily enter the polynomials.
>p=2*X+X*X+4
X^ 2+2*X+4
>p*p
X^ 4+4*X^ 3+12*X^ 2+16*X+16
You can evaluate the polynomials like functions, as well as compute the derivate polynomial.
12
>p'
2*X+2
>p'(2)
6
With this arithmetics you may easily compute powers of polynomial expressions or compute shifted polynomials:
X^ 4-12*X^ 3+54*X^ 2-108*X+81
>q(X+2)
X^ 4-4*X^ 3+6*X^ 2-4*X+1
For polynomials the evaluation, derivation, addition, substraction, equality, inequality and powering by an integer are defined.
As an example
with
:
>f(taylor[2 1])
taylor[4 -3]
>X(u)=taylor[u 1];
>f(X(0))
taylor[-2 -3]
>
Together with the string arithmetic this module can also be used to determine formulas for the derivatives:
>X(u)=taylor[u 1];
>f(X("z"))
taylor["(z+2)/(z-1)" "(1-(z+2)/(z-1))/(z-1)"]
>f'
"(1-(x+2)/(x-1))/(x-1)"
As you can see there is a short-cut to this functionality, but you can only compute the first derivatives, for higher order derivatives you need to use taylor by yourself.
Besides the basic operations +,-,*,/ the following standard functions are defined: sqrt, sqr, exp, log, pow, sin, cos, tan, cot, asin, acos, atan, acot, sinh, cosh, tanh, coth, asinh, acosh, atanh, acoth. Of course the datatype you put into the taylor arithmetic should have defined these functions as well. The cells can be accessed the same way like the cells of vectors.
It is also possible to define a function which computes the derivative of an arbitrary function at an arbitrary point:
>D[sqr;5]
10
As the derivative of "square" is two times the argument, this result is correct.
933262...0000
>2^278
485667...4544
The results above were shortened.
The division between long integers and integers results in an integer division, the remainder can be computed using the modulo operator.
The function longrandom(n) creates a long pseudo random number with about 7n to 8n digits.
-3/7
>A=[1 2;3 4];
>1/A
[-2 1;3/2 -1/2]
>1.0/A
[-2 1;1.5 -0.5]
>
As you can see any operation with a double will round the fraction to the next floating-point number. The fractions are normalized in each step, only the numerator can be negative.
The standard-operators and functions should be defined for fractions, thus the user shouldn't feel any difference between fractions and doubles, but that the latter is less accurate.
As the underlying datatype are long integers, fractions can get as large as much time or memory you have:
126765...5377/126765...5376
Again above numbers were shortened.
>plot[f;-10;10;0.1];
This will plot the function f from -10 to 10, computing values with stepsize 0.1.
Parametric plots are done as follows:
>pplot[spiral,0,2,0.1];
Last but not least rascal can do surface plots:
>splot[g;-2;2;-3;3;.2;.1];
The output of plots may be saved in postscript files using the plotOutput function:
>splot[g;-2;2;-3;3;.2;.1];
This is very basic plotting support and it is likely that the syntax will change in later versions.
Send questions, ideas, hints and congratulations to rascal@ritterbusch.de.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.47)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split 0 -nonavigation -transparent -math -math_parsing -dir rascal_html -mkdir -image_type gif rascal.tex
The translation was initiated by Sebastian Ritterbusch on 2002-05-14