function [] = p1()

%part_a();
part_b();

end

function [] = part_a()
   m=12; n=20; b=2; k=5;
   A = rand(m,n);
   Gc = randn(n,k);
   Gr = randn(m,k);
   Yc = zeros(m,k);
   Yr = zeros(n,k);

   for i = 1:(n/b)
      ind = (i-1)*b + (1:b);
      A_slice = A(:,ind);
      Yc = Yc + A_slice*Gc(ind,:);
      %???????????????????????????????????
      Yr(ind,:) = Yr(ind,:) + A_slice'*Gr;
   end
   
   fprintf(1,'Error in Yc = %12.3e\n',max(max(abs(Yc - A *Gc))))
   fprintf(1,'Error in Yr = %12.3e\n',max(max(abs(Yr - A'*Gr))))

end

function [] = part_b()
   m=12; n=20; b=3; k=5;
   A = rand(m,n);
   Gc = randn(n,k);
   Gr = randn(m,k);
   Yc = zeros(m,k);
   Yr = zeros(n,k);
    
   for i = 1:floor(n/b)
      ind = (i-1)*b + (1:b);
      A_slice = A(:,ind);
      Yc = Yc + A_slice*Gc(ind,:);
      %???????????????????????????????????
      Yr(ind,:) = Yr(ind,:) + A_slice'*Gr;
   end
   
   % clean up remaining inds
   ind = (b*floor(n/b)+1):n;
   A_slice = A(:,ind);
   Yc = Yc + A_slice*Gc(ind,:);
   %???????????????????????????????????
   Yr(ind,:) = Yr(ind,:) + A_slice'*Gr;
   
   fprintf(1,'Error in Yc = %12.3e\n',max(max(abs(Yc - A *Gc))))
   fprintf(1,'Error in Yr = %12.3e\n',max(max(abs(Yr - A'*Gr))))

end
