function D = sprandsym(varargin)
%codistributed.SPRANDSYM Sparse random symmetric codistributed matrix.
%   D = SPRANDSYM(S)
%   D = codistributed.SPRANDSYM(S)
%   D = codistributed.SPRANDSYM(N,DENSITY)
%   
%   Optional arguments to CODISTRIBUTED.SPRANDSYM must be specified after 
%   the size and density arguments, and in the following order:
%   
%     CODISTR - A codistributor object specifying the distribution scheme of
%     the resulting array. If omitted, the array is distributed using the
%     default distribution scheme.
%   
%     'noCommunication' - Specifies that no communication is to be performed
%     when constructing the array, skipping some error checking steps.
%   
%   Example:
%   spmd
%       D = codistributed.sprandsym(1000, 0.01);
%   end
%   
%   See also SPRANDSYM, codistributed


%   Copyright 2016-2019 The MathWorks, Inc.

% We currently only support:
% sprandsym(A)
% sprandsym(n, density [, codistr] [, 'noCommunication'])

% Must have 1 arg or 2-4 args
narginchk(1, 4);

% Check for codistributor. We will pass the trailing args directly to
% sprand which will check them and correctly distribute the output.
[args, allowCommunication] = distributedutil.CodistParser.extractCommFlag(varargin);
[args, codistr] = distributedutil.CodistParser.extractCodistributor(args);
if allowCommunication
    codistArgs = {codistr};
else
    codistArgs = {codistr, 'noCommunication'};
end

if numel(args)>1
    % For (m,density) syntax, neither can be distributed
    args = distributedutil.CodistParser.gatherElements(args);
    
    % Use the error checking in CodistParser to check the input size.
    allowNegativeSizes = false;
    distributedutil.CodistParser.parseArraySizes(args(1), 'sprandsym', allowNegativeSizes);
end

% May need to pass codistributor into rand functions
randFcn = @(varargin) codistributed.rand(varargin{:}, codistArgs{:});
randnFcn = @(varargin) codistributed.randn(varargin{:}, codistArgs{:});

% Call the shared implementation
D = parallel.internal.flowthrough.sprandsymCommon(randFcn, randnFcn, args{:});

end % sprandsym.