Bill Allombert on Mon, 18 Nov 2024 15:57:55 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Is there a function for sum of square matrix antidiagonal like "trace()" for main diagonal?


On Mon, Nov 18, 2024 at 03:34:10PM +0100, hermann@stamm-wilbrandt.de wrote:
> Interesting, I would have thought that your atr function would
> be faster than my vecsum solution, but it is slower ...

This is due to the use of global variables...
If you use \g1, you will see
  ***   Warning: compiler generates copy for `M'.

> pi@raspberrypi5:~/PrimeSquares $ gp -q
> ? M=readvec("15.gp")[1];
> ? #M
> 15
> ? atr(M)=my(j=#M+1);sum(i=1,#M,j--;M[i,j]);
> ? for(i=1,100000,atr(M))

Compare this:

atr(M)=my(j=#M+1);sum(i=1,#M,j--;M[i,j]);
for(i=1,100000,atr(M))
my(M=M);for(i=1,100000,atr(M))

atr2(M)=sum(i=1,#M,M[i,#M+1-i]);
my(M=M);for(i=1,100000,atr2(M))

or

atr(~M)=my(j=#M+1);sum(i=1,#M,j--;M[i,j]);
atr2(~M)=sum(i=1,#M,M[i,#M+1-i]);
for(i=1,100000,atr(~M))
for(i=1,100000,atr2(~M))

Cheers,
Bill.