next up previous contents
Next: Strings Up: Lists and Arrays Previous: Arrays

Matrices

An array with an equal number of elements in each dimension is called a matrix. We can test whether an array is a matrix in Darwin using the type function and type name matrix.

> type([[1,2],[3,4]], matrix);                # It is.
true
> type([1, [2, 3]], matrix);                  # It isn't.
false


  
Figure: An array of type matrix.The array has dimension two and there are an equal number of elements in both dimensions. square[1,1] = f, square[3, 4] = j, $\ldots$
\begin{figure}\centerline{\psfig{file=Diagrams/twod.ps,height=2in}}
\end{figure}

Matrices are used quite often in numerical and symbolic computation and we will use them in Chapters [*] - Point Accepted Mutations and Dayhoff Matrices and [*] - The Pairwise Comparison of Amino Acid Sequences extensively.

More generally, we can define arrays with any number of dimensions using this CreateArray command.

> cube := CreateArray(1..5, 1..5, 1..5, 'a');
cube := [[[a, a, a, a, a], [a, a, a, a, a], [a, a, a, a, a], 
[a, a, a, a, a], [a, a, a, a, a]], [[a, a, a, a, a], [a, a,
a, a, a], [a, a, a, a, a], [a, a, a, a, a], [a, a, a, a, a]], 
[[a, a, a, a, a], [a, a, a, a, a], [a, a, a, a, a], [a, a, a, 
a, a], [a, a, a, a, a]], [[a, a, a, a, a], [a, a, a, a, a], 
[a, a, a, a, a], [a, a, a, a, a], [a, a, a, a, a]], [[a, a, 
a, a, a], [a, a, a, a, a], [a, a, a, a, a], [a, a, a, a, a], 
[a, a, a, a, a]]]
> cube[5,5,5];
a
If you examine the initialized array that Darwin returns, you will notice that it is an array with five elements each of which consists of an array with five elements each of which consists of an array with five elements. The last argument 'a' specifies the initialization value for each element.


  
Figure: A three dimensional array. cube[3][3][1] = a, cube[5,5,5] = b, cube[1,5,3] = c, $\ldots$
\begin{figure}\centerline{\psfig{file=Diagrams/threed.ps,height=2in}}
\end{figure}

As is the case with type set, the data in an array need not be of homogeneous type. Darwin's allowance of heterogeneous data in these structures makes them extremely flexible and, in later chapters, we will use some form of them again and again in almost every routine we build.






There is sometimes confusion between the data types list, array and matrix in Darwin. In actuality, the type list coincides exactly with the type array and the terms may be used interchangeably. Only arrays with an equal number of elements in every dimension are of type matrix.



next up previous contents
Next: Strings Up: Lists and Arrays Previous: Arrays
Gaston Gonnet
1998-09-15