function [V,D,W] = eigHermitianStandard(A,VECTOR,BALANCE)
%MATLAB Code Generation Private Function

%   Solve the standard Hermitian eigenvalue problem.

%   Copyright 2005-2020 The MathWorks, Inc.
%#codegen

coder.internal.prefer_const(VECTOR,BALANCE);
% Using SCHUR guarantees that the eigenvector matrix is orthogonal,
% which doesn't always happen with repeated eigenvalues using the false
% algorithm. We also use SCHUR with only one output just to treat
% Hermitian inputs consistently.
if isreal(A)
    SCHURTYPE = 'real';
else
    SCHURTYPE = 'complex';
end
if nargout <= 1
    if VECTOR
        T = schur(A,SCHURTYPE);
        V = mainDiagZeroImag(T);
    else
        V = schur(A,SCHURTYPE);
        V = diagDiagUpperHessNoImag(V);
    end
else
    if VECTOR
        [V,T] = schur(A,SCHURTYPE);
        D = mainDiagZeroImag(T);
    else
        [V,D] = schur(A,SCHURTYPE);
        D = diagDiagUpperHessNoImag(D);
    end
    if nargout >= 3
        W = V;
    end
end

%--------------------------------------------------------------------------

function d = mainDiagZeroImag(D)
% Returns the main diagonal of the matrix D with the imaginary parts set to
% zero.
n = coder.internal.indexInt(size(D,1));
d = coder.nullcopy(zeros(n,1,'like',D));
for k = 1:n
    d(k) = real(D(k,k));
end

%--------------------------------------------------------------------------

function D = diagDiagUpperHessNoImag(D)
% Sets all the off diagonal elements of a matrix D to zero, assuming that D
% is already in upper Hessenberg form. Sets imaginary parts on the main
% diagonal to zero.
n = coder.internal.indexInt(size(D,1));
D(1,1) = real(D(1,1));
for j = 2:n
    D(j,j) = real(D(j,j));
    D(j,j-1) = 0;
    for i = 1:j-1
        D(i,j) = 0;
    end
end

%--------------------------------------------------------------------------
