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
![]() |
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]; aIf 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.
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.