function C = complex(A,B)
%COMPLEX Construct complex codistributed array from real and imaginary parts
%   C = COMPLEX(A,B)
%   
%   Example:
%   spmd
%       N = 1000;
%       D1 = 3*codistributed.ones(N);
%       D2 = 4*codistributed.ones(N);
%       E = complex(D1,D2)
%   end
%   
%   See also COMPLEX, CODISTRIBUTED, CODISTRIBUTED/ONES.


%   Copyright 2006-2021 The MathWorks, Inc.

% If only one input and it's already complex there's nothing to do.
if nargin == 1 && ~isreal(A)
    C = A;
    return;
end

if nargin == 2
    distributedutil.CodistParser.verifyNonCodistributedInputs({A, B});
end

if nargin>1 && ~iIsValidInputForComplex(A)
    error(message('MATLAB:complex:realPartInputNonNumericOrNonReal'));
elseif nargin > 1 && ~iIsValidInputForComplex(B)
    error(message('MATLAB:complex:imagPartInputNonNumericOrNonReal'));
end

if nargin == 1
    C = codistributed.pElementwiseUnaryOp(@complex, A);
else
    C = codistributed.pElementwiseOp(@complex, A, B);
end

%%%subfunction

function flag = iIsValidInputForComplex(A)
% Check if A is a valid input for real or imaginary part of COMPLEX
flag = isreal(A) && isnumeric(A) || islogical(A) || ischar(A);
