function Y = mldivide(A,B)
%MATLAB Code Generation Library Function

%   Copyright 2002-2018 The MathWorks, Inc.
%#codegen
coder.allowpcode('plain');
eml_invariant(nargin >= 2,'MATLAB:minrhs');
if eml_is_const(isscalar(A)) && isscalar(A)
    coder.inline('always');
    Y = B ./ A;
else
    eml_invariant(isa(A,'float') || ischar(A) || islogical(A), ...
        'Coder:toolbox:unsupportedClass','mldivide',class(A));
    eml_invariant(isa(B,'float') || ischar(B) || islogical(B), ...
        'Coder:toolbox:unsupportedClass','mldivide',class(B));
    eml_invariant(size(B,1) == size(A,1), ...
        'MATLAB:dimagree');
    eml_invariant(ismatrix(A) && ismatrix(B), ...
        'MATLAB:mldivide:inputsMustBe2D');
    Y = mldiv(A,B);
end

%-------------------------------------------------------------------------------

function Y = mldiv(A,B)
if ischar(A) || islogical(A)
    Y = mldiv(double(A),B);
elseif ischar(B) || islogical(B)
    Y = mldiv(A,double(B));
elseif isempty(A) || isempty(B)
    Y = eml_expand(coder.internal.scalarEg(A,B),[size(A,2),size(B,2)]);
elseif size(A,1) == size(A,2) && ( ...
        ~coder.internal.isConst(size(B,1)) || ...
        ~coder.internal.isConst(size(A,2)) || ...
        size(B,1) == size(A,2))
    % The addendum after the check for square A above is there to avoid a
    % spurious compilation error when size(A,1) is variable but size(A,2)
    % and size(B,1) are constant. The generated code will not reflect the
    % addendum, but its presence prevents coder.internal.lusolve(A,B) from
    % being compiled when there is a known compile-time size inconsistency.
    % See g1787152.
    Y = coder.internal.lusolve(A,B);
else
    Y = coder.internal.qrsolve(A,B);
end

%-------------------------------------------------------------------------------
