Why Does Mathematica Output Unexpected Results with Derivative Rules?

  • Context: Mathematica 
  • Thread starter Thread starter unih
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion centers around unexpected results produced by Mathematica when applying derivative rules, specifically focusing on the use of replacement rules and their evaluation order. Participants explore the implications of using Rule versus RuleDelayed in the context of derivatives.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant expresses confusion over the output of derivative calculations in Mathematica, noting discrepancies in results based on different replacement rules.
  • Another participant explains that the unexpected results stem from using Rule instead of RuleDelayed, which causes the right-hand side of the rule to evaluate prematurely.
  • The explanation includes a suggestion to use RuleDelayed to achieve the desired outcomes, highlighting the importance of evaluation order in Mathematica.
  • Further options are proposed, such as using a different form of the replacement rule that accommodates multiple derivatives.
  • A comparison is made between the outputs of the two methods when a variable is defined, illustrating the differences in behavior based on the rule used.
  • Areas of Agreement / Disagreement

    Participants generally agree on the explanation regarding the use of Rule versus RuleDelayed, with one participant expressing gratitude for the clarification. No significant disagreements are noted.

    Contextual Notes

    The discussion does not resolve all potential nuances regarding the use of replacement rules in Mathematica, particularly in more complex scenarios or with different versions of the software.

    Who May Find This Useful

    Users of Mathematica, particularly those working with derivatives and replacement rules, may find this discussion relevant for understanding evaluation order and syntax differences.

unih
Messages
27
Reaction score
0
Hi!
Please help the idiot
In[] = D[p[x,t],x,t]/.{Derivative[n_,m_][p_][q__]->a^(n+m)}
Out[] = a2

In[] = D[p[x,t],x,t]/.{Derivative[n__][p_][q__]->a^Plus[n]}
Out[] = a

WTF? Why not a2?

In the simplest case
In[] = D[p[x,t],x,t]/.{Derivative[n__][p_][q__]->Plus[n]}
Out []=Sequence[1, 1]

In[] = D[p[x,t],x,t]/.{Derivative[n__][p_][q__]->Plus[n,1]}
Out []= 3
Works!

In[] = D[p[x,t],x,t]/.{Derivative[n__][p_][q__]->(Plus[n,1]-1)}
Out []= Sequence[1, 1]
What hell is going on?

Thank you very much!
 
Last edited:
Physics news on Phys.org
It's because you are using Rule instead of RuleDelayed.
This means that the right-hand-side of the rule gets evaluated before you want it to.
So Plus[n] gets turned into n before n gets replaced with anything. The rule then fires n gets replaced with Sequence[1,1] explaining your last line.

Everything works as you want if you use
Derivative[n__][p_][q__] :> a^Plus[n]
instead of you original
Derivative[n__][p_][q__] -> a^Plus[n]

Another option would be
Derivative[n_,m___][p_][q__] :> a^Plus[n,m]
which would still work in the case of
Derivative[n__][p_][q__] -> a^Plus[n]

The place where the use of Rule instead of RuleDelayed becomes really problematic is if a term on the RHS already has a value. For example, compare the output of
n = 5;
D[p[x, t], x, t] /. {Derivative[n__][p_][q__] -> a^Plus[n]}
with
n = 5;
D[p[x, t], x, t] /. {Derivative[n__][p_][q__] :> a^Plus[n]}
From version 6 and above, Mathematica has syntax highlighting, so you can tell whether a variable is defined, undefined or localized by its color (black, blue or green respectively).
 
Thank you VERY VERY MUCH! I have no words to tell how you helped me
 
Not a problem!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K