Addition-chain exponentiation: Difference between revisions

→‎{{header|MATLAB}} / {{header|Octave}}: Addition chain exponentiation,
(Deleted J implementation as there's another one now and this was non-optimal and only sometimes better than binary chain)
(→‎{{header|MATLAB}} / {{header|Octave}}: Addition chain exponentiation,)
Line 178:
93 9 10
...</lang>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
 
I assume that Matlab and Octave have about such optimization already included. On a single core of an "Pentium(R) Dual-Core CPU E5200 @ 2.50GHz", the computation of 100000 repetitions of the matrix exponentiation A^27182 and A^31415 take about 2 and 2.2 seconds, resp.
 
<lang Matlab>x = sqrt(.5);
A = [x,0,x,0,0,0;0,x,0,x,0,0; 0,x,0,-x,0,0;-x,0,x,0,0,0;0,0,0,0,0,1; 0,0,0,0,1,0];A
t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,
t = cputime(); for k=1:100000,x2=A^31415;end; cputime()-t,x2, </lang>
 
Output:
<pre>Octave3.4.3> t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,
ans = 1.9900
x1 =
 
-0.50000 -0.50000 -0.50000 0.50000 0.00000 0.00000
0.50000 -0.50000 -0.50000 -0.50000 0.00000 0.00000
-0.50000 -0.50000 0.50000 -0.50000 0.00000 0.00000
0.50000 -0.50000 0.50000 0.50000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 1.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
 
Octave3.4.3> t = cputime(); for k=1:100000,x2=A^31415;end; cputime()-t,x2,
ans = 2.2000
x2 =
 
0.70711 0.00000 0.00000 -0.70711 0.00000 0.00000
0.00000 0.70711 0.70711 0.00000 0.00000 0.00000
0.70711 0.00000 0.00000 0.70711 0.00000 0.00000
0.00000 0.70711 -0.70711 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.00000 0.00000 1.00000 0.00000
</pre>
Anonymous user