Floating operations

%COMPLEXT Count of floating point operations as in book, Section 7.7.
%       This M-file produces Figure 7.5.

x = 1:  5;
% initiate counters
timeav = zeros(1, 5); timemv = zeros(1, 5);
timeam = zeros(1, 5); timemm = zeros(1, 5);
for n = 1 : 5
v1 = 1: n; v2 = n: -1: 1;   % generate two vectors
m1 = v1’*v2; m2 = v2’*v1;   % generate two matrices
flops(0)                    % set counter to zero
v1 + v2;
timeav(n) = flops;
flops(0)                    % set counter to zero
v1*v2′;
timemv(n) = flops;
flops(0)                    % set counter to zero
m1 + m2;
timeam(n) = flops;
flops(0)                    % set counter to zero
m1*m2;
timemm(n) = flops;
end
[ x’ timeav’ timemv’ timeam’ timemm’ ], pause
plot(x, timeav, x, timemv, x, timeam, x, timemm)
xlabel(‘n’), ylabel(‘number of flops’)
text(x(4), timeam(5), ‘ma’)
text(x(4), timemm(4), ‘mm’)
text(4.5, timemv(5), ‘mv’)