function y = rms(xIn,opt1,opt2)
%MATLAB Code Generation Toolbox Function

%   Copyright 2011-2023 The MathWorks, Inc.
%#codegen

coder.internal.assert(nargin > 0,'MATLAB:minrhs');
if isinteger(xIn)
    x = double(xIn);
else
    x = xIn;
end

% mean accepts an outtype flag, but rms does not.
if nargin > 1
    coder.internal.prefer_const(opt1);
    opt1IsNanflag = validateInput(opt1,false);
    if nargin > 2
        coder.internal.assert(~opt1IsNanflag,'MATLAB:rms:UnknownFlag');
        coder.internal.prefer_const(opt2);
        validateInput(opt2,true);
    end
end

% Compute rms
if isreal(x)
    if nargin==1
        y = sqrt(mean(x .* x));
    elseif nargin==2
        y = sqrt(mean(x .* x, opt1));
    else
        y = sqrt(mean(x .* x, opt1, opt2));
    end
else
    % The way we compute mag square yields better performance than calling
    % abs. 
    if nargin==1
        y = sqrt(mean(real(x) .* real(x) + imag(x) .* imag(x)));
    elseif nargin==2
        y = sqrt(mean(real(x) .* real(x) + imag(x) .* imag(x), opt1));
    else
        y = sqrt(mean(real(x) .* real(x) + imag(x) .* imag(x), opt1, opt2));
    end
end
end

function isNanflag = validateInput(name,checkNanflagOnly)
    coder.internal.prefer_const(name,checkNanflagOnly)
    if coder.internal.isVectorDim(name) || isnumeric(name)
        coder.internal.assert(coder.internal.isConst(name), 'Coder:toolbox:dimNotConst');
        coder.internal.assert(~checkNanflagOnly, 'MATLAB:rms:UnknownFlag');
        isNanflag = false;
    else
        coder.internal.assert(coder.internal.isConst(name), 'Coder:toolbox:FlagMustBeConst');
        if checkNanflagOnly
            errorid = 'MATLAB:rms:UnknownFlag';
        else
            errorid = 'MATLAB:rms:UnknownFlagOrDim';
        end
        coder.internal.assert(coder.internal.isTextRow(name), errorid);
        nanFlags = {'omitnan','omitmissing','includenan','includemissing'};
        tf = coder.const(feval('strncmpi', name, nanFlags, max(1,strlength(name))));
        isValid = coder.const(feval('any',tf));
        coder.internal.assert(isValid,errorid);
        isNanflag = true;
    end
end
