function obj = vecnorm(obj, varargin)
%VECNORM Vector norm.
% Y = VECNORM(X)
% Y = VECNORM(X,p)
% Y = VECNORM(X,p,DIM)
% 
%   The output Y has the same dimension labels, if any, as the input X.
% 
%   See also VECNORM, DLARRAY.

%   Copyright 2023 The MathWorks, Inc.

if nargin > 1 && ~isa(obj, 'dlarray')
    varargin = extractDlarrayCell(varargin);
    obj = vecnorm(obj, varargin{:});
    return;
end

narginchk(1,3);

if nargin > 1
    p = varargin{1};
    if ischar(p) || isstring(p)
        try
            p = validatestring(p,"inf");
        catch e
        end
        % Ensure that only numeric values are used in the backward function
        % in VecnormOp.
        p = str2double(p);
    else
        [p,obj] = extractUnderlyingConstant(p,obj,{'deep:dlarray:AcceleratedPNotSupported'});
    end

else
    % Set p to default value
    p = 2;
end

if nargin > 2
    [obj, fdx, xdata, args] = helperReduction(obj, 'vecnorm', varargin{2});  
else
    [obj, fdx, xdata, args] = helperReduction(obj);  
end

xdata = vecnorm(xdata, p, args{:});

obj.FormattedData = insertData(fdx, xdata);
obj = dropTrailingODims(obj);
end