hermann on Mon, 18 Nov 2024 15:34:18 +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 2024-11-18 15:12, Loïc Grenié wrote:
On Mon 18 Nov 2024 at 13:27, Hermann wrote:I used below code, is there something shorter? assert(isprime(trace(ps))); assert(isprime(vecsum([ps[i,#ps+1-i]|i<-[1..#ps]])));You can define a function atr (antitrace): atr(M)=my(j=#M+1);sum(i=1,#M,j--;M[i,j]); Hope this helps, Loïc
Interesting, I would have thought that your atr function would be faster than my vecsum solution, but it is slower ... 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)) ? ## *** last result: cpu time 996 ms, real time 996 ms. ? for(i=1,100000,vecsum([M[i,#M+1-i]|i<-[1..#M]])) ? ## *** last result: cpu time 597 ms, real time 598 ms. ? M~ is transpose matrix M. Is there some other operator in GP such that atr(M) == trace(op(M)) ? Regards, Hermann.