> < ^ Date: Tue, 25 Apr 1995 10:44:00 -0400
> < ^ From: Alexander Hulpke <hulpke@math.colostate.edu >
< ^ Subject: Re: Loop Efficiency

Dear GAP-Forum,

Walter Carlip wrote:

I have a problem which requires checking (something) for every
element of a vector space. The code . . .

I tried instead several ways to iterate elements of the vector space
for example

for a in field
  for b in field
    for c in field
      for d in field
        v := [a,b,c,d];
        (something)
      od;
    od;
  od;
od;

None of my attempts have been very satisfactory. I tested things
using a six-dimensional field over GF(7). Gap was barely able
store the elemetns of the field and (something) took about 12 seconds
to run on all vectors. With every iteration method I could think
of, Gap took considerably more than 2 minutes to run through the
list of vectors.

My question: Is there a quick way to iterate the elements of
a vector space or to ask Gap for the elements without gap having
to compute them all first?

If you don't need to keep the single vectors, then there is a very easy way
to speed up the procedure. Replace your code by:

v:=[1,2,3,4]; # only here allocate space for list
for a in field do
  v[1]:=a;
  for b in field do
    v[2]:=b;
    for c in field do
      v[3]:=c;
      for d in field do
        v[4]:=d;
        (something)
      od;
    od;
  od;
od;

This runs by magnitudes faster as new space for the list 'v' is not needed
to be allocated in every iteration. However in each iteration the old vector
'v' will be overwritten.
As a user might want to keep some (or all) of these vectors, the routines
provided by standard always create new lists to avoid otherwise possible
side-effects.

This (faster) code might be suitable for your purposes, but keep in mind,
that for storing certain vectors, you cannot use

Add(store,v);

as contents will be overwritten (the same space is used for creating other
vectors), but you have to use

Add(store,ShallowCopy(v));

To get an independent copy of the vector, which will not be overwritten
later.

In a slight disgression from the original question I'd also like to remark,
that we are currently planning to introduce a generic scheme for enumerating
algebraic objects. This would allow you to use something like a single for-loop
to run through all elements of an object ('Iterator').
Similarly, functions to create a bijection between the object and a (subset
of the) integers will be provided, assigning each object a number and each
number an object ('Enumerator'). Thus an iterator can be regarded as a loop
over a subset of the integers, automatically invocing an enumerating
function.
The use of enumerators also helps to store larger sets of elements, as
usually a number takes much less storage space than group elements. As
usual this gain in memory has to be paid with runtime for the convcersion
process.

In the case of vectorspaces iterators as well as enumerators
would again create new elements, leading to the same memory allocation
problem again.

Thanks,

You're welcome,

Alexander

-- Alexander Hulpke, Lehrstuhl D f"ur Mathematik, RWTH, 52056 Aachen,
ahulpke@bert.math.rwth-aachen.de ,
until April '95: Concordia University, Montreal, Canada
hulpke@abacus.concordia.ca


> < [top]