forked from aiavenak/matlab_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpower2.m
201 lines (200 loc) · 5.8 KB
/
mpower2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
% mpower2 - Evaluate matrix to a power for integer power
%*************************************************************************************
%
% MATLAB (R) is a trademark of The Mathworks (R) Corporation
%
% Function: mpower2
% Filename: mpower2.m
% Programmer: James Tursa
% Version: 1.1
% Date: November 11, 2009
% Copyright: (c) 2009 by James Tursa, All Rights Reserved
%
% This code uses the BSD License:
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
% mpower2 evaluates matrix to a power for an integer power faster
% than the MATLAB built-in function mpower. The speed improvement
% apparently comes from the fact that mpower does an unnecessary
% matrix multiply as part of the algorithm startup, whereas mpower2
% only does necessary matrix multiplies. e.g.,
%
% >> A = rand(2000);
% >> tic;A^1;toc
% Elapsed time is 6.047194 seconds.
% >> tic;mpower2(A,1);toc
% Elapsed time is 0.001882 seconds.
%
% For sparse matrices, mpower does not do the unnecessary matrix
% multiply. However, in this case mpower2 is apparently more
% memory efficient. e.g.,
%
% >> A = sprand(5000,5000,.01);
% >> tic;A^1;toc
% Elapsed time is 0.038530 seconds.
% >> tic;mpower2(A,1);toc
% Elapsed time is 0.036335 seconds.
% >> tic;A^2;toc
% Elapsed time is 3.248792 seconds.
% >> tic;mpower2(A,2);toc
% Elapsed time is 2.160705 seconds.
% >> tic;A^3;toc
% Elapsed time is 10.005085 seconds.
% >> tic;mpower2(A,3);toc
% Elapsed time is 10.020719 seconds.
% >> tic;A^4;toc
% ??? Error using ==> mpower
% Out of memory. Type HELP MEMORY for your options.
% >> tic;mpower2(A,4);toc
% Elapsed time is 133.682037 seconds.
%
% Y = mpower2(X,P), is X to the power P for integer P. The power
% is computed by repeated squaring. If the integer is negative,
% X is inverted first. X must be a square matrix.
%
% Class support for inputs X, P:
% float: double, single
%
% Caution: Since mpower2 does not do the unnecessary startup matrix
% multiply that mpower does, the end result may not match mpower exactly.
% But the answer will be just as accurate.
%
% Change Log:
% Nov 11, 2009: Updated for sparse matrix input --> sparse result
%
%**************************************************************************
function Y = mpower2(X,p)
%\
% Check the arguments
%/
if( nargin ~= 2 )
error('MATLAB:mpower2:InvalidNumberOfArgs','Need two input arguments.');
end
if( nargout > 1 )
error('MATLAB:mpower2:TooManyOutputArgs','Too many output arguments.');
end
classname = superiorfloat(p,X);
if( numel(p) ~= 1 )
error('MATLAB:mpower2:InvalidP','P must be a scalar.');
end
if( ~isreal(p) )
error('MATLAB:mpower2:InvalidP','P must be real.');
end
if( floor(p) ~= p )
error('MATLAB:mpower2:InvalidP','P must be an integer.');
end
z = size(X);
if( length(z) > 2 || z(1) ~= z(2) )
error('MATLAB:mpower2:NonSquareMatrix','Matrix must be square.');
end
if( isempty(X) )
if( issparse(X) )
Y = sparse(z(1),z(2));
else
Y = zeros(z,classname);
end
return
end
%\
% Special cases use custom code that is faster than binary decomposition
%/
if( p == 0 )
if( issparse(X) )
Y = diag(sparse(ones(z(1),1)));
else
Y = eye(z,classname);
end
return
end
if( p < 0 )
X = inv(X);
p = abs(p);
end
if( p == 1 )
Y = X;
return
elseif( p == 2 )
Y = X * X;
return
elseif( p == 3 )
Y = X * X * X;
return
elseif( p == 15 )
X2 = X * X;
Y = X * X2;
Y = Y * X2;
Y = Y * Y * Y;
return
elseif( p == 31 )
X2 = X * X;
Y = X * X2;
Y = Y * X2;
Y = Y * Y;
Y = Y * Y * Y * X;
return
elseif( p == 63 )
Y = X * X;
X3 = X * Y;
Y = X3 * Y;
Y = Y * Y;
Y = Y * Y;
Y = Y * Y * Y * X3;
return
end
%\
% Get the binary decomposition of the power as a row of binary characters.
%/
bp = dec2bin(p);
%\
% Loop through the bit positions from least significant to most significant
%/
Y = [];
P = X;
zz = size(bp,2);
for n=zz:-1:1
%\
% If the bit position is 1, then we need to apply the current power of X
% to the result. P is the current power of X, i.e. P = X^(2^(zz-n))
%/
if( bp(n) == '1' )
if( isempty(Y) )
Y = P;
else
Y = Y * P;
end
end
%\
% Square the current power of X, but not if it is the last index in the
% loop because in that case it won't be used or needed. For some reason,
% P * P is a lot faster than P^2.
%/
if( n ~= 1 )
P = P * P;
end
end
if( isa(Y,'double') && isequal(classname,'single') )
Y = single(Y);
end
return
end