Why Does Mathematica Output Unexpected Results with Derivative Rules?

  • Mathematica
  • Thread starter unih
  • Start date
  • Tags
    Mathematica
In summary: Glad I could help. Keep in mind that using RuleDelayed (or :>) is generally safer than Rule (or ->) when dealing with patterns and replacements. It's a good thing to keep in mind when working with Mathematica.
  • #1
unih
27
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
  • #2
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).
 
  • #3
Thank you VERY VERY MUCH! I have no words to tell how you helped me
 
  • #4
Not a problem!
 
  • #5


Hello,

I am not familiar with Mathematica, but it seems like you are trying to use the Derivative function to find the derivative of p(x,t) with respect to x and t. The first two lines of your code show that you are using the rule that the derivative of p(x,t) with respect to x and t is equal to a^(n+m), where n and m are the order of the derivatives. This means that the output should be a^2, as you mentioned.

However, in the next two lines, you are using a different rule that the derivative of p(x,t) with respect to x and t is equal to a^Plus[n], where Plus[n] represents the sum of the orders of the derivatives. This means that the output should be a, as you mentioned.

In the last two lines, you are using a slightly modified rule where you are adding 1 to the sum of the orders of the derivatives. This means that the output should be a+1, which is equal to 3 in this case.

I cannot say for sure why the outputs are different in each case without further context or knowledge of Mathematica. But it seems like the rules you are using are not consistent, which could be causing the confusion. I would suggest checking the documentation or seeking help from a Mathematica expert to better understand how to use the Derivative function and rules in Mathematica.

I hope this helps. Good luck with your work!
 

Related to Why Does Mathematica Output Unexpected Results with Derivative Rules?

1. What is "Rule" in Mathematica?

"Rule" is a built-in function in Mathematica that allows you to replace specific elements in an expression with other elements. It is commonly used in pattern matching and rule-based transformations.

2. How do I use "Rule" in Mathematica?

To use "Rule" in Mathematica, you can use the notation "old -> new" where "old" represents the element you want to replace and "new" represents the element you want to replace it with. You can also use the shorthand notation "/." for multiple replacements.

3. Can I use "Rule" to replace multiple elements at once?

Yes, you can use the "Rule" function to replace multiple elements at once by using the shorthand notation "/." For example, "a -> b /. {a -> c, b -> d}" would replace both "a" and "b" with "c" and "d" respectively.

4. Are there any special cases or limitations when using "Rule" in Mathematica?

One limitation of "Rule" is that it only works with exact matches. This means that if you have a list of elements and you want to replace them with another list, the lengths of the two lists must be the same. Another limitation is that "Rule" only performs replacements at the first level of an expression, so it may not work as expected for nested lists or expressions.

5. Can I use "Rule" to define my own custom rules?

Yes, you can define your own custom rules using the "Rule" function in Mathematica. You can create your own patterns and specify the corresponding replacements to create your own rule-based transformations. This can be useful for simplifying complex expressions or performing specific tasks in your calculations.

Similar threads

Replies
2
Views
598
Replies
11
Views
2K
Replies
0
Views
2K
  • Differential Geometry
Replies
2
Views
614
Replies
5
Views
1K
  • High Energy, Nuclear, Particle Physics
Replies
6
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
947
  • High Energy, Nuclear, Particle Physics
Replies
5
Views
997
Replies
4
Views
2K
Replies
9
Views
3K
Back
Top