Dear Forum,
in his message, Michael smith asked:
Subject: Calling functions, printing records and matrix calculations ----------------------------------------------------------------------------- A couple of suggestions, and a question.LISP 'apply function:
>Since LISP seems to have been an influence in the design of the GAP
>language, I wondering whether there might be a (hidden?) function
>equivalent to the LISP 'apply function. This function takes as arguments
>a function F and a list L and its evaluation is equivalent to a call to F
>with arguments given by the elements of L.>i.e. ApplyFunction(F,[a,b,c]) is equivalent to F(a,b,c)
Supposing, that there always is an fixed upper limit to the number of arguments,
one could use a hack like :
ApplyFunction := function( func, list ) local l; l:=Length(list); if l=0 then func(); if l=1 then func(l[1]); elif l=2 then func(l[1],l[2]); . . . fi; end;
He continues:
> I need to calculate a number of products of some matrices and add these
> products together. What is the best way of doing this? The simplest
> method, Sum([1..n], i -> A[i]*B[i]), uses much more space than necessary.
> It should be possible to do all this using only two extra matrices, one
> to accumulate the sum, the other to store each product. Can this be done?
When looking at the code of 'Sum', it seems, that indeed only two
intermediate matrices should be needed, as basically a loop evaluates
sum:=sum+func(i);
Are you sure, that GAP *needs* the additional memory, or that this memory
only gets allocated? (The memory manager tends to allocate more memory than
actually needed.) If this is the case, starting GAP with option -m limit,
where limit is a bound for the necessary memory should do the task.
Alexander Hulpke