function [X,rcond] = pageinv(X)
%PAGEINV Page-wise matrix inverse for codistributed array
%   Y = PAGEINV(X)
%   [Y,RC] = PAGEINV(X)
%   
%   See also PAGEINV, CODISTRIBUTED.
%   


%   Copyright 2021 The MathWorks, Inc.

% If we have only one page and don't need the second output, avoid
% redistributing by calling INV directly.
if ismatrix(X) && nargout<2
    % INV warns, PAGEINV does not!
    w = warning("off", "MATLAB:singularMatrix");
    warnObj = onCleanup(@() warning(w));
    X = inv(X);
    return
end

if nargout == 1
    X = pagefun(@inv, X);
else
    [X,rcond] = pagefun(@inv, X);
end 

end