function [r,h] = simple(s)
%SIMPLE Search for simplest form of a symbolic expression or matrix.
%   SIMPLE(S) tries several different algebraic simplifications of
%   S, displays any which shorten the length of S's representation,
%   and returns the shortest. S is a SYM. If S is a matrix, the result
%   represents the shortest representation of the entire matrix, which is 
%   not necessarily the shortest representation of each individual element.
%
%   [R,HOW] = SIMPLE(S) does not display intermediate simplifications,
%   but returns the shortest found, as well as a string describing
%   the particular simplification. R is a SYM. HOW is a string.
%
%   Examples:
%
%      S                          R                  How
%
%      cos(x)^2+sin(x)^2          1                  combine(trig)
%      2*cos(x)^2-sin(x)^2        3*cos(x)^2-1       simplify
%      cos(x)^2-sin(x)^2          cos(2*x)           combine(trig)
%      cos(x)+(-sin(x)^2)^(1/2)   cos(x)+i*sin(x)    radsimp
%      cos(x)+i*sin(x)            exp(i*x)           convert(exp)
%      (x+1)*x*(x-1)              x^3-x              collect(x)
%      x^3+3*x^2+3*x+1            (x+1)^3            factor
%      cos(3*acos(x))             4*x^3-3*x          expand
%
%      syms x y positive
%      log(x) + log(y)            log(x*y)           combine
%
%   See also SIMPLIFY, FACTOR, EXPAND, COLLECT.

%   Copyright (c) 1993-98 by The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 1998/07/21 20:48:27 $

% Initialize output variables and print flag.
r = s;
h = [];
p = nargout == 0;
x = findsym(s,1);

% Try the different simplifications.
[r,h] = simpler('simplify',s,r,h,p);
[r,h] = simpler('radsimp',s,r,h,p);
[r,h] = simpler('combine',s,r,h,p,'trig');
[r,h] = simpler('factor',s,r,h,p);
[r,h] = simpler('expand',s,r,h,p);
[r,h] = simpler('combine',s,r,h,p);
[r,h] = simpler('convert',s,r,h,p,'exp');
[r,h] = simpler('convert',s,r,h,p,'sincos');
[r,h] = simpler('convert',s,r,h,p,'tan');
if ~isempty(x)
   [r,h] = simpler('collect',s,r,h,p,x);
end


function [r,h] = simpler(how,s,r,h,p,x)
%SIMPLER Used by SIMPLE to shorten expressions.
%   SIMPLER(HOW,S,R,H,P,X) applies method HOW with optional parameter X
%   to expression S, prints the result if P is nonzero, compares the
%   length of the result with expression R, which was obtained with
%   method H, and returns the shortest string and corresponding method.

if nargin < 6
   [t,err] = maple('map',how,s);
else
   [t,err] = maple('map',how,s,x);
end

if err, return, end

if nargin == 6
   how = [how '(' x ')'];
end

if p 
   disp(' ')
   disp([how ':'])
   disp(' ')
   disp(t)
end

if length(char(t)) <= length(char(r))
   r = t;
   h = how;
end
