       Re: pattern matching against the Dt function?

• To: mathgroup at smc.vnet.net
• Subject: [mg85270] Re: pattern matching against the Dt function?
• From: "David Park" <djmpark at comcast.net>
• Date: Tue, 5 Feb 2008 05:58:33 -0500 (EST)
• References: <fo6h46\$r20\$1@smc.vnet.net>

```Adam,

It is because Mathematica normally evaluates the left hand side of rules. So
look what happens:

Dt[n_] -> n //InputForm
Dt[n]*Derivative[1, 0][Pattern][n, _] -> n

In any case, it is not uncommon that one doesn't what to evaluate the left
hand side of the rule and the solution is simply to use HoldPattern.

{f[a], Dt[b]} /. HoldPattern[Dt[n_]] -> n
{f[a], b}

If a pattern does not match, the first thing to do is look at the InputForm
or FullForm of the pattern and then of the object you are trying to match.
That will usually expose the problem.

The fact that the D rule seemed to work is just quixotic. I'm not certain
why Mathematica allows a one argument of D, which is undefined, but again
look at the evaluated forms:

{f[a], D[b]}
{f[a], b}

D[n_] -> n
n_ -> n

{f[a], D[b]} /. D[n_] -> n
{f[a], b}

Probably what happened here is that Mathematica simply replaced the entire
expression by itself. D[b] went to b because of the evaluation of the
expression and not because of the rule.

--
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

news:fo6h46\$r20\$1 at smc.vnet.net...
> Hello,
>
> I'm having trouble matching the Dt function with a pattern, even though
> it works for all other functions I've tried.
>
> (*It doesn't match Dt[b] here.*)
> In:= {f[a], Dt[b]} /. Dt[n_]->n
> Out= {f[a], Dt[b]}
>
> (*But it matches f[a] with no problem.*)
> In:= {f[a], Dt[b]} /. f[n_]->n
> Out= {a, Dt[b]}
>
> (*In a process of elimination, I tried another built-in function, D, and
> it worked fine.*)
> In:= {f[a], D[b]} /. D[n_]->n
> Out= {f[a], b}
>
> (*I tried another function more than one character long, Sin, and that
> works.*)
> In:= {f[a], Sin[b]} /. Sin[n_]->n
> Out= {f[a], b}
>
> (*The full forms all seem to follow the same pattern.*)
> In:= Sin[b] // FullForm
> Out//FullForm= Sin[b]
>
> In:= Dt[b] // FullForm
> Out//FullForm= Dt[b]
>
> In:= f[b] // FullForm
> Out//FullForm= f[b]
>
> (*I thought it might be related to the evaluation of the Dt function, so
> I tried Holding it. No luck.*)
> In:= {f[a], Hold[Dt[b]]} /. Dt[n_]->n
> Out= {f[a], Hold[Dt[b]]}
>
> (*However, it has no problem matching f[a] in a Hold.*)
> In:= {Hold[f[a]], Dt[b]} /. f[n_]->n
> Out= {Hold[a], Dt[b]}
>
> (*I tried looking at the attributes to find out if there was something
> special about the Dt function, but it doesn't seem like it...*)
> In:= Attributes[Sin]
> Out= {Listable, NumericFunction, Protected}
>
> In:= Attributes[D]
>
> In:= Attributes[Dt]
> Out= {Protected}
>
> I've read every section on pattern matching in the documentation center,
> and I'm at a complete loss to explain why I can't seem to match the Dt
> function with the Dt[n_] pattern when I can match these other functions.
> I know I can use the _Dt pattern to match it, but then I don't get
> control over matching the arguments...
>
> Thank you.
>
> Very curious,