function [U,S,V] = svd(A,varargin)
%MATLAB Code Generation Library Function

%   Copyright 2002-2017 The MathWorks, Inc.
%#codegen

coder.columnMajor;
coder.internal.prefer_const(varargin);
eml_invariant(nargin > 0,'MATLAB:minrhs');
eml_invariant(isa(A,'float'), ...
    'Coder:toolbox:unsupportedClass','svd',class(A));
allFiniteA = ~coder.internal.anyNonFinite(A);
if nargout == 1
    if allFiniteA
        U = coder.internal.svd(A,varargin{:});
    else
        % Do not issue an error, rather issue NaN results.
        % coder.internal.error('MATLAB:svd:matrixWithNaNInf');
        A1 = zeros(size(A),'like',A);
        U1 = coder.internal.svd(A1,varargin{:});
        U = coder.internal.nan(size(U1,1),size(U1,2),'like',U1);
    end
else
    if allFiniteA
        [U,s,V] = coder.internal.svd(A,varargin{:});
    else
        % Do not issue an error, rather issue NaN results.
        % coder.internal.error('MATLAB:svd:matrixWithNaNInf');
        % Using U1, s1, and V1 makes it clear that the compiler does not
        % actually need to call coder.internal.svd, only propagate size
        % information to U, s, and V.
        A1 = zeros(size(A),'like',A);
        [U1,s1,V1] = coder.internal.svd(A1,varargin{:});
        U = coder.internal.nan(size(U1,1),size(U1,2),'like',A);
        s = coder.internal.nan(size(s1,1),size(s1,2),'like',real(A));
        V = coder.internal.nan(size(V1,1),size(V1,2),'like',A);
    end
    % Create S matrix from singular value vector.
    S = zeros(size(U,2),size(V,2),'like',real(A));
    n = coder.internal.indexInt(numel(s));
    for k = 0:n-1
        S(k+1,k+1) = s(k+1);
    end
end
