Next: Linked Lists Up: Data Structures Previous: Arrays

Records

Maple doesn't explicitly have a record data structure like Pascal's record or C's struct. By a record data structure we mean a data structure for keeping together a heterogeneous collection of objects, i.e. a set of objects not necessarily of the same type.

An example of a place where you would like a record data structure would be in choosing a data structure to represent a quarternion. A quarternion is a number of the form where are real numbers. To represent a quarternion, we need to store only the four quantities and . Another example would be a data structure to represent the factorization of a polynomial in . The factorization of looks like

where each of the factors is monic and irreducible. We need to store the factors and the exponents and the unit .

There are several possibilities for representing records in Maple. The simplest, and most obvious, is to use a list. I.e. we would represent the quarternion as the list , and the factorization of as the list where is a list of lists of the form . We use subscripts to refer to a component of the data structure, e.g. the unit part of a factorization would be given by a[1]. You can use the macro facility to define an identifier to be equal to a constant if you prefer to use a symbol to reference a component as shown in the following example.


> a := [-1/2,[[x+1,2],[x-1,1]]];

                     a := [-1/2, [[x + 1, 2], [x - 1, 1]]]

> macro(unit=1,factors=2,base=1,exponent=2);
> a[unit];
                                      -1/2

> a[factors][1][base];
                                     x + 1

> a[unit]*a[factors][1][base]^a[factors][1][exponent]*a[2][2][1]^a[2][2][2];

                                          2
                             - 1/2 (x + 1)  (x - 1)

A second possibility for representing records in Maple is to use a function call. I.e. we could represent as QUARTERNION(i,j,k,l). An advantage of this representation is that you we can tell Maple how to do various operations on functions. We will go into details later. Here we shall only mention that you can define how to pretty print a function. The example below will show what we mean


> QUARTERNION(2,3,0,1);

                             QUARTERNION(2, 3, 0, 1)

> `print/QUARTERNION` := proc(a,b,c,d)  a + b*'i' + c*'j' + d*'k' end:
> QUARTERNION(2,3,0,1);

                                  2 + 3 i + k

Here we have defined a printing procedure or subroutine. This routine is called once for each different QUARTERNION function in a result from Maple prior to displaying the result. Note the use of quotes in the procedure, because we want to see the identifiers , and in the output, and not the value of the variables which we might be using for something else.

A third possibility for representing a record is to think of it as a multivariate polynomial in the field names, and to store the values in the coefficients. This is quite useful when the fields are numerical and you wish to be able to do arithmetic on the fields. For example, we could represent a quarternion as a polynomial in the variables as in the output representation above. I.e.


> z1 := 2 + 3*i + k;

                               z1 := 2 + 3 i + k

> z2 := 2 - 3*i + 2*j + 2*k;

                           z2 := 2 - 3 i + 2 j + 2 k

> coeff(z1,i); # the coefficient in i

                                       3

> z1 + z2;

                                 4 + 3 k + 2 j

Although this looks nice, we don't recommend using the names , or because you will use them for for loop variables!


bondaren@thsun1.jinr.ru