Mercurial > matrix-functions
comparison mftoolbox/power_binary.m @ 0:8f23314345f4 draft
Create local repository for matrix toolboxes. Step #0 done.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Wed, 06 May 2015 14:56:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8f23314345f4 |
---|---|
1 function X = power_binary(A,m) | |
2 %POWER_BINARY Power of matrix by binary powering (repeated squaring). | |
3 % X = POWER_BINARY(A,m) computes A^m for a square matrix A and a | |
4 % positive integer m, by binary powering. | |
5 | |
6 s = double(dec2bin(m)) - 48; % Binary representation of s in double array. | |
7 k = length(s); | |
8 | |
9 P = A; | |
10 i = k; | |
11 while s(i) == 0 | |
12 P = P^2; | |
13 i = i-1; | |
14 end | |
15 X = P; | |
16 for j = i-1:-1:1 | |
17 P = P^2; | |
18 if s(j) == 1 | |
19 X = X*P; | |
20 end | |
21 | |
22 end |