Re: A pattern matching problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg72546] Re: [mg72533] A pattern matching problem*From*: János <janos.lobb at yale.edu>*Date*: Sat, 6 Jan 2007 03:46:32 -0500 (EST)*References*: <200701050706.CAA10424@smc.vnet.net>

On Jan 5, 2007, at 2:06 AM, carlos at colorado.edu wrote: > Here is an interesting challenge in pattern matching. Suppose > you are given an algebraic-differential expression exemplified by > > r = u[t+h]-2*u[t]+u[t-h]+a^2*u'[t+h/2]+4*u'[t-h/4]+ > c*u''[t+alfa*h]/12; > > Here u[t] is a function of time t, assumed infinitely differentiable, > h is a time interval, and primes denote derivatives wrt t. > Relation r==0 is called a delay-differential equation, and is the > basic stuff in delayed automatic control (h is the signal "lag"). > > The function name u and the lag h are always symbolic. > Function u and its derivatives appear linearly in r, while > h always appears linearly in arguments. > Coefficients of h may be numeric or symbolic. > Coefficients of u & derivatives may be numeric or symbolic. > > The challenge: given r, get the coefficients of h as a 2D list, > row-ordered by derivative order. Zero coefficients may be omitted. > For the above r, it should return > > {{1,-1},{1/2,-1/4},{alfa}} > > Envisioned module invocation: clist=LagCoefficients[r,u,t,h,m] > with m=max u-derivative order to be considered. Skeleton: > > LagCoefficients[r_,u_,t_,h_,m_]:=Module[ {clist={}}, > ?????? > Return[clist]]; > > Any ideas for ????? Here is a newbie idea without too much pattern matching, because I still have trouble with that :) In[1]:= r = u[t + h] - 2*u[t] + u[t - h] + a^2*Derivative[1][u][ t + h/2] + 4*Derivative[1][u][ t - h/4] + c*(Derivative[2][u][ t + alfa*h]/12); In[2]:= (#1[[All,2]] & ) /@ Split[Inner[List, Head /@ Extract[ DeleteCases[List @@ r, x_ /; MemberQ[x, h, {0, Infinity}] == False], Most /@ Position[DeleteCases[ List @@ r, x_ /; MemberQ[x, h, {0, Infinity}] == False], u]], (Replace[#1, h -> 1] & ) /@ (First[#1] & ) /@ Extract[r, Most /@ Position[r, h]], List], #1[[1]] == #2[[1]] & ] Out[2]= {{-1, 1}, {-(1/4), 1/2}, {alfa}} I am sure the gurus will give you a less convoluted one :) János ---------------------------------------------- Trying to argue with a politician is like lifting up the head of a corpse. (S. Lem: His Master Voice)

**References**:**A pattern matching problem***From:*carlos@colorado.edu