[GAP Forum] Identical objects that are indices (not lists) ?

Steve Linton sal at dcs.st-and.ac.uk
Thu Apr 7 21:26:01 BST 2005


Dear GAP Forum,

Mike Newman from QMUL asked:

> I am a new GAP user, and am wondering if it is possible to have the nifty
> feature of identical lists extend to simple objects (ie: variables that
> have no subobjects), or failing that, something like what in C might be
> called a pointer.
> 
> Specifically, I wish to operate on the elements of a matrix in two 
> slightly different cases:
> 1) operate only on those indices (i,j) where i<=j
> 2) operate on all indices (i,j)
> 
> So, my simplified GAP fantasy-code looks like:
> 
> abc := function(A,flag)
> 	local i,j,jj;
> 
> 	if flag then
> 		jj := PleaseMakeIdenticalCopy(i);	# ?!?
> 	else
> 		jj := 1;
> 	fi
> 
> 	for i in [1..d] do for j in [jj..d] do
> 		# various_functions(i, j, A[i][j])
> 		# same for both cases
> 	od; od;
> end;
> 
> Thus if flag=true, then jj and i would in fact be the same object 
> (initially unset, of course); otherwise, jj is just a variable whose value 
> is 1.
> 
> 
> Making jj be a pointer would work too, but I find no mention of pointers
> or anything similar in the GAP manual. Simply typing in two different
> versions of function will of course work, but seems dangerous from the
> point of view of maintenance (the body may change over time but always be
> independent of which case). I can think of other ways of accomplishing
> this with various degrees of (in)elegance, but the "identical object"
> scenario seems so perfect...
> 
> Is there some nice GAP way to accomplish this? For what it's worth, this
> is a somewhat time-critical section of code, so I would prefer not to
> introduce too large a decision structure if possible.


There is a no notion in GAP equivalent to a pointer to an integer, and I can't
see an easy way to implement what you want without either duplicating the
contents of the loops or checking flag or calling a function inside the outer
loop. 

Robert Morse has suggested a solution based on duplicating the loop
content and one based on a function. A slight variation of the
function-based soltuion which moves the check of flag outside the function is:

abc := function(A,flag)
    local i,j,d,f;

    d := Length(A);
    if flag then f := i->i; else f := i->1; fi;
    for i in [1..d] do for j in [f(i)..d] do


What I would recommend though is the simpler:

abc := function(A,flag)
	local i,j,jj;

	for i in [1..d] do 
  	  if flag then
		jj := i;
	   else
		jj := 1;
	   fi;

	   for j in [jj..d] do
	
Although this conditional is executed d times, the cost of it will be pretty 
small. If your "various functions" in the loop are at all non-trivial they will
certainly dominate the cost of the conditionals and loops.

	Steve



-- 
Steve Linton	School of Computer Science  &
      Centre for Interdisciplinary Research in Computational Algebra
	     University of St Andrews 	 Tel   +44 (1334) 463269
http://www.dcs.st-and.ac.uk/~sal	 Fax   +44 (1334) 463278   




More information about the Forum mailing list