function C = hMldivideTranspose(A, transposeA, B, transposeB)
%HMLDIVIDETRANSPOSE matrix left-division with transposed inputs
%   
%   HMLDIVIDETRANSPOSE(A, transA, B, transB) performs a matrix left-division
%   of A and B applying transposes to A and B as appropriate. The two transpose
%   flags can have the following values:
%      0  No transpose
%      1  Non-conjugate transpose
%      2  Conjugate transpose
%   
%   Example:
%   spmd
%       A = codistributed([ 1 2; 3 4; 5 6 ]);
%       B = codistributed([ 7 8 9; 10 11 12 ]);
%       C = hMldivideTranspose(A, 0, B, 1); % C is 3x2 
%   end
%   
%   See also: codistributed/mldivide, codistributed/transpose, codistributed/ctranspose, codistributed.
%   


%   Copyright 2014-2020 The MathWorks, Inc.
 
% Modify B if necessary
B = iMaybeTranspose(B, transposeB);

% Deal with the scalar case
if isscalar(A) || (isscalar(B) && ~ismatrix(A))
    % Modify A if necessary
    A = iMaybeTranspose(A, transposeA);
    C = codistributed.pElementwiseOp(@ldivide, A, B);
    return;
end

isMrdivide = false;
% Pass transposeA flag into pMldivideAndMrdivide
try
    [C, rCond] = hMldivideAndMrdivide(transposeA, A, B, isMrdivide);
catch ME
    throw(ME);
end

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

function X = iMaybeTranspose(X, t)
% Perform the appropriate transpose (if any)
if t==distributedutil.TransposeFlags.TRANSPOSE
    X = transpose(X);
elseif t==distributedutil.TransposeFlags.CONJUGATE_TRANSPOSE
    X = ctranspose(X);
end
end