www.gusucode.com > symbolic工具箱matlab源码程序 > symbolic/@sym/rewrite.m

    function R = rewrite(S,T)
%REWRITE    Rewrite symbolic expressions S in terms of target T.
%   REWRITE(S,T) rewrites the symbolic expression S in terms of
%     the target T. The target T is specified as one of the strings
%     'exp', 'log', 'sincos', 'sin', 'cos', 'tan', 'cot',
%     'sinhcosh', 'sinh', 'cosh', 'tanh', 'coth', 'asin',
%     'acos', 'atan', 'acot',  'asinh', 'acosh', 'atanh',
%     'acoth', 'sqrt', 'heaviside', 'piecewise'.
%
%   REWRITE(S,'exp') rewrites all trigonometric and hyperbolic
%     functions in terms of EXP. Furthermore, the inverse functions
%     are rewritten in terms of LOG.
%
%   REWRITE(S,'log') rewrites the functions ASIN, ACOS, ATAN, ACOT,
%     ASINH, ACOSH, ATANH, ACOTH in terms of LOG.
%
%   REWRITE(S,'sincos') rewrites the functions TAN, COT, EXP, SINH,
%     COSH, TANH and COTH in terms of SIN and COS.
%
%   REWRITE(S,'sin') does the same as REWRITE(S,'sincos'). In addition
%     to that, cos(x)^2 is rewritten as 1-sin(x)^2.
%
%   REWRITE(S,'cos') does the same as REWRITE(S,'sincos'). In addition
%     to that, sin(x)^2 is rewritten as 1-cos(x)^2.
%
%   REWRITE(S,'tan') rewrites the functions SIN, COS, COT, EXP, SINH,
%     COSH, TANH, COTH in terms of TAN.
%
%   REWRITE(S,'cot') rewrites the functions SIN, COS, EXP, SINH,
%     COSH, TANH, COTH in terms of COT.
%
%   REWRITE(S,'sinhcosh') rewrites the functions EXP, TANH, COTH, SIN,
%     COS, TAN and COT in terms of SINH and COSH.
%
%   REWRITE(S,'sinh') does the same as REWRITE(S,'sinhcosh'). In addition
%     to that, cosh(x)^2 is rewritten as sinh(x)^2 + 1.
%
%   REWRITE(S,'cosh') does the same as REWRITE(S,'sinhcosh'). In addition
%     to that, sinh(x)^2 is rewritten as cosh(x)^2 - 1.
%
%   REWRITE(S,'tanh') rewrites the functions SIN, COS, COT, EXP, SINH,
%     COSH, COTH in terms of TANH.
%
%   REWRITE(S,'coth') rewrites the functions SIN, COS, COT, EXP, SINH,
%     COSH, TANH in terms of COTH.
%
%   REWRITE(S,'asin') rewrites the functions LOG, ACOS, ATAN, ACOT,
%     ASINH, ACOSH, ATANH, ACOTH in terms of ASIN.
%
%   REWRITE(S,'acos') rewrites the functions LOG, ASIN, ATAN, ACOT,
%     ASINH, ACOSH, ATANH, ACOTH in terms of ACOS.
%
%   REWRITE(S,'atan') rewrites the functions LOG, ASIN, ACOS, ACOT,
%     ASINH, ACOSH, ATANH, ACOTH in terms of ATAN.
%
%   REWRITE(S,'acot') rewrites the functions LOG, ASIN, ACOS, ATAN,
%     ASINH, ACOSH, ATANH, ACOTH in terms of ACOT.
%
%   REWRITE(S,'asinh') rewrites the functions LOG, ASIN, ACOS, ATAN, ACOT,
%     ACOSH, ATANH, ACOTH in terms of ASINH.
%
%   REWRITE(S,'acosh') rewrites the functions LOG, ASIN, ACOS, ATAN, ACOT,
%     ASINH, ATANH, ACOTH in terms of ACOSH.
%
%   REWRITE(S,'atanh') rewrites the functions LOG, ASIN, ACOS, ATAN, ACOT,
%     ASINH, ACOSH, ACOTH in terms of ATANH.
%
%   REWRITE(S,'acoth') rewrites the functions LOG, ASIN, ACOS, ATAN, ACOT,
%     ASINH, ACOSH, ATANH in terms of ACOTH.
%
%   REWRITE(S,'sqrt') rewrites complex absolute values abs(x+i*y) as
%     sqrt(x^2 + y^2) if x and y are real symbolic expressions.
%
%   REWRITE(S,'heaviside') rewrites the function SIGN in terms of
%     HEAVISIDE.
%
%   REWRITE(S,'piecewise') rewrites the functions ABS, SIGN, and HEAVISIDE
%   in terms of PIECEWISE.
%
%   Examples:
%      syms x;
%
%      rewrite(sin(x),'exp') returns i/(2*exp(x*i)) - (exp(x*i)*i)/2
%
%      rewrite(i/(2*exp(x*i))-(exp(x*i)*i)/2,'sincos') returns sin(x)
%
%      rewrite(asinh(x),'log') returns log(x + (x^2 + 1)^(1/2))
%
%      rewrite((1/exp(x*i))*(1/2+i/2)+exp(x*i)*(1/2+(-i/2)),...
%               'sincos') returns cos(x) + sin(x)
%
%      rewrite(tan(x),'sin') returns -sin(x)/(2*sin(x/2)^2 - 1)
%
%      rewrite(tan(x),'sincos') returns sin(x)/cos(x)
%
%      rewrite(sym('sign(x)'),'heaviside') returns 2*heaviside(x) - 1
%
%      syms x y real;
%      rewrite(abs(x+1i*y),'sqrt') returns (x^2 + y^2)^(1/2)
%
%   See also SYM/SIMPLIFY, SYM/FACTOR, SYM/COLLECT,
%   SYM/SIMPLIFYFRACTION.

%   Copyright 2011-2016 The MathWorks, Inc.

p = inputParser;
p.addRequired('S', @(x) isa(x,'sym'));
p.addRequired('T', @(x) any(strcmp(x, { ...
                   'sincos', 'sin', 'cos', 'tan', 'cot', ...
                   'sinhcosh', 'sinh', 'cosh', 'tanh', 'coth', ...
                   'asin', 'acos', 'atan', 'acot', ...
                   'asinh', 'acosh', 'atanh', 'acoth', ...
                   'exp', 'log', 'sqrt', 'heaviside', 'piecewise'})));
p.parse(S,T);

S = privResolveArgs(S);
S = S{1};

% T consists of 3 characters at least
% T starts with an 'a' only if T is in {'asin', 'acos', 'atan', 'acot', asinh', 'acosh', 'atanh', 'acoth'}
if T(1) == 'a'
    % Expand 'a' to 'arc'
    T = ['arc' T(2:end)];
end

Rsym = mupadmex('rewrite',S.s,T);
R = privResolveOutput(Rsym, S);
end