Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.26 KB | None | 0 0
  1. clc;
  2. clear all;
  3. close all;
  4. format long e;
  5.  
  6. % for n=1:8
  7. %     k = 2^n;
  8. %     A = rand(k,k);
  9. %     B = rand(k,k);
  10. % %     tic
  11. %     C = A * B;
  12. % %     toc
  13. % end
  14. %
  15. % for n=1:8
  16. %     k = 2^n;
  17. %     A = rand(k,k);
  18. %     B = rand(k,k);
  19. %     tic
  20. %     for i=1:length(A)
  21. %        for j=1:length(A)
  22. %            C(i,j) = 0;
  23. %            for x=1:length(A)
  24. %                C(i,j) = C(i,j) + A(i,x) * B(x,j);
  25. %            end
  26. %        end
  27. %     end
  28. %     toc
  29. % end
  30. for n=1:8
  31.     k = 2^n;
  32.     A = rand(k,k);
  33.     B = rand(k,k);
  34.     for r=0:4
  35.         R=2^r;
  36.         fprintf('n= %d, R=%d\n',n,R);
  37.         tic
  38.         C=strassen(A,B,R);
  39.         toc
  40.     end
  41. end
  42.  
  43.  
  44.  
  45. function C = strassen(A, B, R)
  46. if nargin < 3; end
  47.     n = length(A);
  48.     if n <= R
  49.        C = A*B;
  50.     else
  51.        m = n/2; i = 1:m; j = m+1:n;
  52.        P1 = strassen( A(i,i)+A(j,j), B(i,i)+B(j,j), R);
  53.        P2 = strassen( A(j,i)+A(j,j), B(i,i), R);
  54.        P3 = strassen( A(i,i), B(i,j)-B(j,j), R);
  55.        P4 = strassen( A(j,j), B(j,i)-B(i,i), R);
  56.        P5 = strassen( A(i,i)+A(i,j), B(j,j), R);
  57.        P6 = strassen( A(j,i)-A(i,i), B(i,i)+B(i,j), R);
  58.        P7 = strassen( A(i,j)-A(j,j), B(j,i)+B(j,j), R);
  59.        C = [ P1+P4-P5+P7  P3+P5;  P2+P4  P1+P3-P2+P6  ];
  60.     end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement