function P2Grade()
% Checks out HermSquare

% A small example to check correctness...
n = 4;
X = randn(n,n) + sqrt(-1)*randn(n,n);
X = X + X';
X_herm = triu(real(X)) + tril(imag(X));
% Square X two ways...
clc
% Explicitly...
Y = X*X;
% Your implemengtation...
Y_herm = HermSquare(X_herm)
% Compare
Y0_herm = triu(real(Y)) + tril(imag(Y))
fprintf('err = %10.2e\n', norm(Y_herm-Y0_herm))

% A larger example to compare with HermSquareCVL
n = 500;
X = randn(n,n) + i*randn(n,n);
X = X + X';
X_herm = triu(real(X)) + tril(imag(X));
% Your implementation...
tic
Y_hermYOU = HermSquare(X_herm);
tYOU = toc;
% My implementation...
tic
Y_hermCVL = HermSquareCVL(X_herm);
tCVL = toc;
fprintf('\ntYOU/tCVL = %5.2f error = %10.2e\n',tYOU/tCVL,norm(Y_hermYOU-Y_hermCVL))

function Y_herm = HermSquareCVL(X_herm)
% X_herm is the herm representation of an nxn Hermitian matrix X
% Y_herm is the herm representation of the square of X
[n,n] = size(X_herm);
Y_herm = zeros(n,n);
% If X = B+iC then Y = X*X = (B*B-C*C) + i*(B*C+C*B)
% The following nested loop code computes the (q,r) entry of BB-CC
% and the (r,q) entry of BC+CB...
for q = 1:n
u = [ X_herm(1:q-1,q)' X_herm(q,q:n)]; %q-th row of B
v = [ X_herm(q,1:q-1) 0 -X_herm(q+1:n,q)']; %q-th row of C
Y_herm(q,q) = u*u'+v*v';
for r=q+1:n
w = [ X_herm(1:r-1,r) ; X_herm(r,r:n)']; %r-th col of B
% The following computations are fine if negation is not
% regarded as a flop.
z = [-X_herm(r,1:r-1)';0; X_herm(r+1:n,r)]; %r-th col of C
% Compute (q,r) entry of B*B - C*C
Y_herm(q,r) = u*w - v*z;
% Compute the (r,q) entry of B*C+C*B
Y_herm(r,q) = -(w'*v')-(z'*u');
% If negation is to be regarded as a flop, then we can "bury" that
% overhead as follows
%%%%% ztilde = [ X_herm(r,1:r-1)';0; X_herm(r+1:n,r)];
%%%%% Y_herm(q,r) = u*w + v(1:r)*ztilde(1:r) - v(r+1:n)*ztilde(r+1:n);
%%%%% Y_herm(r,q) = -(w'*v')+ztilde(1:r)'*u(1:r)' - ztilde(r+1:n)'*u(r+1:n)';
end
end

% Here is the flop accounting if we disregard the negation overhead or use
% the "%%%%% code"

% For each (q,r) pair there are four length-n inner products.
% There are n^2/2 (q,r) pairs.
% A length-n inner product involves 2n flops.
% (n^2/2)*(2n)*4 = 4n^3.

% -2 pts if correct but you use more than a pair of nxn arrays.
% 1 point for a reasoned flop assessment