To define a function we specify the argument, variable and the
operation(s) performed on the argument. The software then tells us
what type of output the function gives. We can also ask the software
what type of input and output has been specified for the function
f. For example:
atlas> set f(int x)=x^2
Defined f: (int->int)
atlas> whattype f?
Overloaded instances of 'f'
int->int
atlas> f(2)
Value: 4
atlas>
We can also specify what the output will be by including it in the definition of the function rather than expecting the software to decide what it would be:
More functions are defined in the .at files and we can copy and paste more complicated functions from there.
In addition, for a quick search, recall that the atlas library within this doccumentation lists all the functions already available.
Arguments and Outputs
To find out about the usage of a function you type whattypename>?. atlas will list all the instances where that
function is used with the type of input and output for each case. For
example,:
atlas> whattype inverse ?
Overloaded instances of 'inverse'
mat->mat
atlas>
Gives you the argument and output for the function inverse. It
says that the function takes a matrix and produces another matrix.
Note: Remember that the command whattype without the question mark
gives a data type. To get atlas to give you an argument you need to
add the ?
atlas> whattype invert ?
Overloaded instances of 'invert'
mat->(mat,int)
atlas>
This says that the function invert takes a matrix and gives back a pair of a matrix and an integer (See the section on Matrices for more information).
Another command that tells us more about function usage for a given
input is the command @, which we use in the following
format. Suppose we want to know what the function inverse does to a
matrix. Then we type:
atlas>inverse@matValue:Functiondefinedatatlas-scripts/basic.at:254:4--256:74(M):let(inv,d)=invert@mat(M)inif=@(int,int)(d,1)theninvelseerror("Matrix not invertible over the integers")fiatlas>
Note that it also tells you in which .at file you can find the script
of the function
This is defined in basic, takes a pair of strings and concatenates them.
However, if you want to know what ‘+’ does to a matrix and an
integer you get:
atlas>+@(mat,int)Value:{+@(mat,int)}atlas>
This means this is a built-in function that you can find in the
atlas-functions.help file for information. But, in this case we
can also try it to see what it does:
This is an example of a forloop. Which tells the software that
for each v in A you return something.
Another example is:
atlas> v:=[1,2,3,4]
Value: [1,2,3,4]
atlas> for a in v do a^2 od
atlas>
Value: [1,4,9,16]
atlas> whattype $
type: [int]
atlas>
Which takes the elements of v, squares them and lists them as an
array of integers.
Now using this type of simple forloop we can, for example, define
the “flattening” function which takes the columns of a matrix and
writes them concatenated into a single row:
Again the command above is a loop. The first part
says that the function takes a matrix and outputs a vector. The
second part defines an empty vector rv. The third part is the loop that
says that for each vector v in the matrix A, append it to what you
have in rv. The last part says, do it for all the vectors in A and print the final result.
Note the use of the operation # here means append each v in A to the previous iteration.
You can find out more about this operation by typing whattype#? and by looking at the atlas-functions.help file:
There are several kinds of loops which are explained in the
atlas.help file. However, sometimes it is easier to look at some
of the scripts in the .at files and see how the loops are used to
define functions. In particular the basic.at file can be useful.