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

Robert F. Morse rm43 at evansville.edu
Tue Apr 5 19:42:27 BST 2005


mike newman wrote:

> 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;
> 

Here is a possibility:

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

    d := Length(A);

    if not flag then
        for i in [1..d] do for j in [1..d] do
             # various_functions(i, j, A[i][j])
             # same for both cases
        od; od;
    else
        for i in [1..d] do for j in [i..d] do
             # various_functions(i, j, A[i][j])
             # same for both cases
        od; od;
    fi;
end;

If your application is time critical this structure give exactly d^2
steps and exactly (d^2+d)/2 steps in the second case.

gap> abc([1..10000],true);
gap> time;
1156
gap> abc([1..10000],false);
gap> time;
2307


If you don't mind calling a function then

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

    d := Length(A);
    f := function(x) if flag then return x; else return 1; fi; end;

    for i in [1..d] do for j in [f(i)..d] do
         # various_functions(i, j, A[i][j])
         # same for both cases
    od; od;

end;

There is a a bit of overhead with the function call but I would say it
probably it is minimal.


gap> abc([1..10000],true);
gap> time;
1161
gap> abc([1..10000],false);
gap> time;
2315

Regards, Robert F. Morse






More information about the Forum mailing list