function obj = reshape(obj,varargin)
%RESHAPE Reshape array.
%   B = RESHAPE(A, M, N)
%   B = RESHAPE(A, M, N, P, ...)
%   B = RESHAPE(A, [M, N, P, ...])
%
%   The output B has no dimension labels, even if the input A has them.
%   
%   See also RESHAPE, DLARRAY.

%   Copyright 2019-2022 The MathWorks, Inc.

isObjDlarray = isa(obj,'dlarray');
if ~isObjDlarray
    % There must be a dlarray elsewhere in the input list.  Extract all the
    % inputs as user-facing numeric array types and re-dispatch the call.
    for ii=1:numel(varargin)
        arg = varargin{ii};
        if isobject(arg) && isa(arg, 'dlarray')
            varargin{ii} = gather(extractdata(arg));
        end
    end
    obj = reshape(obj, varargin{:});
    return
end

% Check for any dlarrays in the input list and extract permuted values, but
% allow internal data types so that we do not lose tracing.
for ii=1:numel(varargin)
    arg = varargin{ii};
    if isobject(arg) && isa(arg, 'dlarray')
        arg = permuteExplicitly(arg);
        varargin{ii} = arg.FormattedData.Data;
    end
end

obj = permuteExplicitly(obj);
[fdobj, objdata] = extractData(obj.FormattedData);

objdata = reshape(objdata, varargin{:});

fdobj.DimensionMetadata = '';
obj.FormattedData = insertData(fdobj, objdata);
end
