function r = limit(f,x,a,dir)
%LIMIT    Limit of an expression.
%   LIMIT(F,x,a) takes the limit of the symbolic expression F as x -> a.
%   LIMIT(F,a) uses findsym(F) as the independent variable.
%   LIMIT(F) uses a = 0 as the limit point.
%   LIMIT(F,x,a,'right') or LIMIT(F,x,a,'left') specify the direction
%   of a one-sided limit.
%
%   Examples:
%     syms x a t h;
%
%     limit(sin(x)/x)                 returns   1
%     limit((x-2)/(x^2-4),2)          returns   1/4
%     limit((1+2*t/x)^(3*x),x,inf)    returns   exp(6*t)
%     limit(1/x,x,0,'right')          returns   inf
%     limit(1/x,x,0,'left')           returns   -inf
%     limit((sin(x+h)-sin(x))/h,h,0)  returns   cos(x)
%     v = [(1 + a/x)^x, exp(-x)];
%     limit(v,x,inf,'left')           returns   [exp(a),  0]

%   Copyright (c) 1993-98 by The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 1997/11/29 01:05:50 $

f = sym(f);

% Default direction is along the real line.
% Default x is findsym(f,1).
% Default a is 0.
switch nargin
   case 1, 
     a = '0';
     x = findsym(f,1);
     dir = 'real';
   case 2, 
     a = x;
     x = findsym(f,1);
     dir = 'real';
   case 3, 
     dir = 'real';
   case 4, 
end
if any(size(x) > 1)
   error('Variable must be a scalar.')
end
if any(size(a) > 1) 
   error('Limit point must be a scalar.')
end
a = sym(a);
x = sym(x);

r = maple('map','limit',f,[x.s '=' a.s ',' dir]);
k = strmatch('undefined',char(r.s));
if ~isempty(k), r(k).s = 'NaN'; end
