function A = mldivide(A, B)
%\ Backslash or left matrix divide for codistributed arrays
%   X = A \ B
%   X = MLDIVIDE(A,B)
%   
%   A and B must be codistributed array objects with underlying type single or double.
%   
%   The MATLAB MLDIVIDE function prints a warning message if A is
%   badly scaled, nearly singular, or rank deficient.  codistributed/MLDIVIDE 
%   cannot check for this condition; therefore, it is unable to warn
%   if there is a violation. You should be aware of this possibility
%   and take action to avoid it.
%   
%   
%   See also MLDIVIDE, CODISTRIBUTED, CODISTRIBUTED/RAND.


%   Copyright 2006-2021 The MathWorks, Inc.

narginchk(2, 2);

distributedutil.CodistParser.verifyNonCodistributedInputs({A, B});

% Scalar case
if isscalar(A) || (isscalar(B) && ~ismatrix(A))
    A = codistributed.pElementwiseOp(@ldivide, A, B);
    return;
end

if iIsUnsupported(A) || iIsUnsupported(B)
    error(message('parallel:distributed:MldivideNotSupported'));
end

transposeFirstMatrix = distributedutil.TransposeFlags.NO_TRANSPOSE;
isMrdivide = false;
try
    [A, rCond] = hMldivideAndMrdivide(transposeFirstMatrix,...
        A, B, isMrdivide);
catch ME
    throw(ME);
end

if ~isempty(rCond)
    printRcondWarning(rCond, superiorfloat(A));
end
end

%------------------------------------
function tf = iIsUnsupported(D)
tf = ~ismatrix(D) || ~(isfloat(D) || ischar(D) || islogical(D));
end
