hermann on Mon, 18 Nov 2024 18:30:00 +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 16:59, Loïc Grenié wrote:

trace(op(~M)) is slower than atr2, but I finally got it:

op(~M)=Mat([M[-i,]~|i<-[-#M..-1]]);

op(M)=Mat(Vecrev(M));

  but I expect it to be slower than the sum (too many copies).

Thank you, I have used Vecrev() before, but did not think about it.
And yes and no regarding slower:
While "trace(op(~M))" is slower, moving "trace()" into "op()"
makes it slightly faster than atr2 from Bill:

? atr2
(~M)->sum(i=1,#M,M[i,#M+1-i])
? for(i=1,100000,atr2(~M))
cpu time = 615 ms, real time = 614 ms.
? op(~M)=trace(Mat(Vecrev(M)));
? for(i=1,100000,op(~M))
cpu time = 589 ms, real time = 591 ms.
?


Regards,

Hermann.