function f=pdetexpd(x,y,sd,u,ux,uy,t,f)
%PDETEXPD Evaluate an expression on triangles.

%       A. Nordmark 12-19-94.
%       Copyright (c) 1994-98 by The MathWorks, Inc.
%       $Revision: 1.7 $  $Date: 1997/11/25 17:06:32 $

if isstr(f),
  thack=0;
  if ~isieee & any(isnan(t))
    % PARABOLIC and HYPERBOLIC probes with time=NaN for
    % explicit time dependence. On non-IEEE machines we still need to
    % return NaN.
    % Try to cause an evaluation error if "t" is present in expression.
    thack=1;
    t=NaN*ones(1,size(x,2)+2);
  end
  ic=findstr(f,'!');
  nf=length(ic)+1;
  if nf>1,
    X=x;
    Y=y;
    SD=sd;
    U=u;
    UX=ux;
    UY=uy;
    nt=length(x);
    fff=zeros(1,nt);
    lf=length(f);
    ic=[0 ic lf+1];
    for ii=1:nf,
      ff=f(ic(ii)+1:ic(ii+1)-1);
      iii=find(SD==ii);
      x=X(iii);
      y=Y(iii);
      sd=SD(iii);
      if size(U),
        u=U(:,iii);
        ux=UX(:,iii);
        uy=UY(:,iii);
      end
      ok=1;
      ffold=ff;
      eval('ff=eval(ffold);','ok=0;');
      if thack & (~ok | (any(size(ff)-[1 1]) & any(size(ff)-size(x))))
        % Assume error was due to presence of "t", and return NaN.
        ok=1;
        f=NaN;
      end
      if ~ok
        errstr=sprintf( ...
          '%s\n\nIn expression:\n%s\nwhen evaluating pde coefficients in subdomain %d.\n', ...
          lasterr,ffold,ii);
        error(errstr);
      end
      if any(size(ff)-[1 1]) & any(size(ff)-size(x))
        errstr=sprintf( ...
          '%s\n\nIn expression:\n%s\nwhen evaluating pde coefficients in subdomain %d.\n', ...
          'Expression evaluates to wrong size',ffold,ii);
        error(errstr);
      end
      fff(iii)=ff.*ones(size(iii));
    end
    f=fff;
  else
    ok=1;
    fold=f;
    eval('f=eval(fold);','ok=0;');
    if thack & (~ok | (any(size(f)-[1 1]) & any(size(f)-size(x))))
      % Assume error was due to presence of "t", and return NaN.
      ok=1;
      f=NaN;
    end
    if ~ok
      errstr=sprintf( ...
        '%s\n\nIn expression:\n%s\nwhen evaluating pde coefficients.\n', ...
        lasterr,fold);
      error(errstr);
    end
    if any(size(f)-[1 1]) & any(size(f)-size(x))
      errstr=sprintf( ...
        '%s\n\nIn expression:\n%s\nwhen evaluating pde coefficients.\n', ...
        'Expression evaluates to wrong size',fold);
      error(errstr);
    end
  end
end

