function A = inv(A)
%INV    Matrix inverse for codistributed arrays
%   X = INV(C) is the inverse of codistributed matrix C.
%   
%   
%   See also INV, CODISTRIBUTED, CODISTRIBUTED/RAND.


%   Copyright 2010-2022 The MathWorks, Inc.

narginchk(1, 1);

% Quick return if array is empty
if isempty(A)
    return;
end

% Validate argument
iValidateInputArg(A);

try
    A = A \ eye(size(A), "like", A);
catch ME
    throw(ME);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function iValidateInputArg(A)
    if ~isfloat(A)
        ME = MException(message('MATLAB:inv:inputType'));
        throwAsCaller(ME);
    end
    
    if ~ismatrix(A)
        ME = MException(message('MATLAB:inv:inputMustBe2D'));
        throwAsCaller(ME);
    end
    
    if size(A, 2) ~= size(A, 1)
        ME = MException(message('MATLAB:legacy_two_part:square'));
        throwAsCaller(ME);
    end
end
