Re: Problem dealing with Cases and MemberQ
- To: mathgroup at smc.vnet.net
- Subject: [mg68221] Re: Problem dealing with Cases and MemberQ
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Sat, 29 Jul 2006 01:00:55 -0400 (EDT)
- Organization: The University of Western Australia
- References: <ea72js$k6a$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <ea72js$k6a$1 at smc.vnet.net>, "Charlie Brummitt" <cbrummitt at wisc.edu> wrote: > I am trying to write code to detect whether a given (partial > differential) equation is dissipative, i.e. whether it contains > -u[x,t] or +D[u[x,t],{x,2}] or -D[u[x,t],{x,4}] or +D[u[x,t],{x,6}]. I assume that you are talking about the dissipative properties of the finite-difference scheme associated with the PDE, rather than of the PDE itself? So I would guess that the application is to automatically construct stable finite-difference schemes? > In other words, given an expression (for example, -0.1 u[x,t] + > u[x,t]^2 + u[x,t] D[u[x,t],{x,2}] ) I would like to determine > whether the expression contains any of the following: -U, +Uxx, > -Uxxxx, +Uxxxxxx --- with the specified signs. (Note that here I used > the shorthand U = u[x,t], Uxx = D[u[x,t],{x,2}], etc.) > > I am having trouble determining whether a given variable (say U) has a > negative sign in front of it. It does not matter whether that U is > also multiplied by a real number (say 0.1) or by another variable (say > Ux). Could anyone please help me write the code to do this? But what if -U is multiplied by -U (just U^2)? And how should you handle -U Uxx ? > Here is a method I have tried, to moderate success: > > This method successfully determines whether a particular term has a > plus or minus sign on it when the term has a real coefficient, but it > fails when the term in the equation is multiplied by another variable > (e.g., Ux). > > Here is essentially what the code does: I do Cases[equation, term] to > find the cases of the term I'm looking for, then I divide the > resulting list by the term to just get the coefficient that's in front > of each case. Then I map the appropriate sign function (Positive or > Negative, depending on which term I'm searching for -- e.g. Negative > for -U, Positive for +Uxx, etc.). Then I use MemberQ on the result to > find out whether any of the values of the resulting list is True. > > However, this method fails when the term in the equation is multiplied > by a different variable. This is because when I divide the list > generated by Cases by the term I'm searching for, I am still left with > any other variables that were multiplied by the term. For example, if > - U*Ux is in the equation, after dividing by U I am left with -Ux, and > when I apply Negative to this quantity it does not evaluate to True > (even though I want it to!) because it doesn't know whether Ux is > positive or negative. How might I fix this? > > Here is the actual code: > > containsDissipative[eq_, term_, sign_] := MemberQ[Map[sign, Cases[eq, > _*term]/term], True]; > > > If you are so inclined, here is a good equation to use as a test equation: > Plus[Times[-0.001`, u[x, t]], Times[10, Power[u[x, t], 2]], Times[-1, > u[x, t], Derivative[1, 0][u][x, t]]] > Then flipping the signs on the leading U term and the U*Ux term is a > good way to test the code. > > Might there be another way using Select? Here's a slightly more complicated equation: de = 10 u[x, t]^2 - Derivative[1, 0][u][x, t]*u[x, t] - 0.001 u[x, t] - Derivative[2, 0][u][x, t] Derivative[3, 0][u][x, t]^2 Turn it into a list, ld = List @@ de Extract the overall sign, sg = Sign[First /@ ld] and locate the even derivatives: Union[ Cases[#,u[x,t] | Derivative[n_ /; EvenQ[n],_][u][x,t],Infinity]]& /@ ld This should allow you to select the terms that you want. Also, perhaps the following links are of interest? http://library.wolfram.com/infocenter/Conferences/5775/ http://library.wolfram.com/infocenter/Conferences/4960/ Cheers, Paul _______________________________________________________________________ Paul Abbott Phone: 61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul