function dZ = internal_crosschannelnormBackward(dZ, X, Z, windowSize, alpha, beta, k)
% internal_crosschannelnormBackward   Cross-channel normalization backpropagation
%
%   Inputs: 
%   dZ         - The derivative of the loss function with respect to the
%                output of crosschannelnorm in form of a (S)x(S)x(C)x(B)
%                gpuArray.
%   X          - The input feature maps in form of a (S)x(S)x(C)x(B)
%                gpuArray.
%   Z          - The output of crosschannelnorm.
%   windowSize - The number of maps to use for the normalization of each
%                element.
%   alpha      - Multiplier for the normalization term.
%   beta       - Exponent for the normalization term.
%   k          - Offset for the normalization term.
%
%   Output:
%   dX         - The derivative of the loss function with respect to the
%                input of crosschannelnorm in form of a (S)x(S)x(C)x(B)
%                gpuArray.

%   Copyright 2019-2021 The MathWorks, Inc.

if ~isreal(dZ)
    error(message('deep:dlarray:ComplexGradientOutput', 'crosschannelnorm'));
end

% Ensure all inputs have the same data type
[dZ, X, Z] = nnet.internal.cnngpu.castToSuperiorFloat(dZ, X, Z);

dZ = nnet.internal.cnngpu.localMapNormBackward2D(Z, dZ, X, windowSize, alpha, beta, k);

end
