gusucode.com > symbolic工具箱matlab源码程序 > symbolic/syms.m

    function syms(varargin)
%SYMS   Short-cut for constructing symbolic variables.
%   SYMS arg1 arg2 ...
%   is short-hand notation for creating symbolic variables
%      arg1 = sym('arg1');
%      arg2 = sym('arg2'); ...
%   or, if the argument has the form f(x1,x2,...), for
%   creating symbolic variables
%      x1 = sym('x1');
%      x2 = sym('x2');
%      ...
%      f = symfun(sym('f(x1,x2,...)'), [x1, x2, ...]);
%   The outputs are created in the current workspace.
%
%   SYMS  ... ASSUMPTION
%   additionally puts an assumption on the variables created.
%   The ASSUMPTION can be 'real', 'rational', 'integer', or 'positive'.
%   SYMS  ... clear
%   clears any assumptions on the variables created, including those
%   made with the ASSUME command.
%
%   SYMS({symvar1, symfun1,  ...})
%   is equal to the call SYMS 'symvar1' 'symfun1'  ...
%
%   SYMS({symvar1, symfun1, ...}, ASSUMPTION)
%   is equal to the call SYMS 'symvar1' 'symfun1'  ... ASSUMPTION
%
%   SYMS([symvar1, symvar2,  ...])
%   is equal to the call SYMS 'symvar1' 'symvar2'  ...
%
%   SYMS([symvar1, symvar2, ...], ASSUMPTION)
%   is equal to the call SYMS 'symvar1' 'symvar2'  ... ASSUMPTION
%
%   Each input argument must begin with a letter and must contain only
%   alphanumeric characters.
%
%   By itself, SYMS lists the symbolic objects in the workspace.
%
%   Example 1:
%      syms x beta real
%   is equivalent to:
%      x = sym('x','real');
%      beta = sym('beta','real');
%
%   To clear the symbolic objects x and beta of 'real' or 'positive' status, type
%      syms x beta clear
%
%   Example 2:
%      syms x(t) a
%   is equivalent to:
%      a = sym('a');
%      t = sym('t');
%      x = symfun(sym('x(t)'), [t]);
%
%   Example 3:
%      syms({sym('u'), sym('v'), sym('w')})
%   is equivalent to:
%      syms u v w
%
%   Example 4:
%      syms({symfun('u(t)',sym('t')), symfun('v(t)',sym('t')), symfun('w(t)',sym('t'))})
%   is equivalent to:
%      syms u(t) v(t) w(t)
%
%   Example 5:
%      syms({sym('u'), sym('v'), sym('w')}, 'real')
%   is equivalent to:
%      syms u v w real
%
%   Example 6:
%      syms([sym('u'), sym('v'), sym('w')])
%   is equivalent to:
%      syms u v w
%
%   See also SYM, SYMFUN.
%   Deprecated API:
%   The 'unreal' keyword can be used instead of 'clear'.

%   Copyright 1993-2016 The MathWorks, Inc.

numberOfArgs = nargin; 

% the following flags can be used as last argument:
flags = {'real','clear','positive','rational','integer'};

% Convert sym objects and cell arrays to a cell array of chars
% and proceed as usual.
if numberOfArgs > 0
    firstArg = varargin{1};
    if isa(firstArg, 'sym') || iscell(firstArg)
        if nargin > 2 
            error(message('symbolic:misc:IncorrectNumberOfArguments')); 
        end
        if nargin == 2 && ~any(strcmp(varargin{2}, flags))
            error(message('symbolic:sym:InvalidAssumption2')); 
        end
        % strip first argument
        if isempty(firstArg) 
            if numberOfArgs == 2 
                % varargin must be a cell
                varargin = varargin(2);
            end
            numberOfArgs = numberOfArgs-1;
        else
            if isa(firstArg, 'sym') 
               help = arrayfun(@char, firstArg(:), 'UniformOutput', false); 
            else
               help = cellfun(@char, firstArg(:), 'UniformOutput', false); 
            end
            if numberOfArgs > 1
                varargin = [help; varargin{2}];
            else
                varargin = help;
            end
        end
   end
end

if numberOfArgs < 1
    w = evalin('caller','whos');
    clsnames = {w.class};
    k = strcmp('sym',clsnames) | strcmp('symfun',clsnames);
    disp(' ')
    disp({w(k).name})
    disp(' ')
    return
end

if any(strcmp(varargin{end}, flags))
    control = varargin{end};
    args = varargin(1:end-1);
elseif strcmp(varargin{end}, 'unreal')
    control = 'clear';
    warning(message('symbolic:sym:DeprecateUnreal'));
    args = varargin(1:end-1);
else
    control = '';
    args = varargin;
end

% check whether caller workspace equals base workspace
calledFromBase = mupadmex('', 8);

toDefine = sym(zeros(1, 0));
defined = sym(zeros(1, length(args)));
for k=1:length(args)
    x = args{k};
    if isvarname(x) && ~any(strcmp(x, flags))
        xsym = sym(x);
        % check whether syms is called from another function and 
        % x already exists and is not overshadowed by the caller
         varexists = evalin('caller', ['exist(''' x ''')']);
        if ~calledFromBase && ...
                 varexists >= 2 && varexists ~= 7
            error(message('symbolic:sym:SymsCannotOvershadow', x));
        end
        assignin('caller', x, xsym);
        if ~isempty(control)
            assume(xsym, control);
        end
        defined(k) = xsym;
    elseif isempty(find(x == '(', 1))
        error(message('symbolic:sym:errmsg1'));
    else
        % If a bracket occurs, handle this as a symfun declaration
        [name, vars] = symfun.parseString(x);
        if any(strcmp(name, flags)) 
            error(message('symbolic:sym:errmsg1'));
        end    
        xsym = symfun(x, [vars{:}]);
        % as a side-effect, define all variables that occur as arguments
        toDefine = [toDefine vars{:}]; %#ok<AGROW>.
        defined(k) = sym(name);
        varexists = evalin('caller', ['exist(''' name ''')']);
        if ~calledFromBase && ...
                varexists >= 2 && varexists ~= 7
             error(message('symbolic:sym:SymsCannotOvershadow', name));
        end
        % define the symfun
        assignin('caller', name, xsym);
        % assumptions cannot pertain to symfuns
        if ~isempty(control)
            warning(message('symbolic:sym:VariableExpected', x));
        end 
    end
end

% in the end, define all variables that have occurred only as arguments
for ysym = setdiff(toDefine, defined)
    y = char(ysym);
    if any(strcmp(y, flags))
        error(message('symbolic:sym:errmsg1'));
    end
    varexists = evalin('caller', ['exist(''' y ''')']);
    if ~calledFromBase && ... 
            varexists >= 2 && varexists ~= 7
        error(message('symbolic:sym:SymsCannotOvershadow', y));
    end 
    assignin('caller', y, ysym);
end