function J = activationSigmoidJacobian(x,outputMode)
%sigmoid   The Jacobian of sigmoid.
% 
% A codegen compliant implementation of the Jacobian of sigmoid. 
% Intended for use with single and double arrays. Does not support batches.
%
% Optional Arguments
% J = sigmoid(x,outputMode)   supports outputMode="full" or
%                             "diagonal". The default is "full". In
%                             the "diagonal" case only the diagonal of the
%                             Jacobian is returned.

%   Copyright 2022 The MathWorks, Inc.

%#codegen

if nargin < 2
    outputMode = "full";
end

% Use the same identity as SigmoidOp
% d(sigmoid) = exp(-x) / (1+exp(-x))^2 
%            = sigmoid(x) * exp(-x)/(1+exp(-x)) 
%            = sigmoid(x) * (1-sigmoid(x))
tmp = exp(-x)./(1+exp(-x)).^2;
if strcmp(outputMode,"full")
    J = diag(tmp);
else
    J = tmp;
end
end