ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 28 Sep 2020 12:26:21 +0200derivative of order var('m') returns 0https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/ Hello,
In mathematica I can differentiate a function m times with respect to x, where m doesn't have an explicit value yet and get an expression that can be evaluated as below:
<pre>
#MATHMATICA CODE
in[0] g = x^2 + 3/2*(x^2 - 1)*x
f = D[g, {x, m}]
out[0]= ∂(x,m)(3/2x(-1+x^2))+Power(m,0)[x,2]
in[1] f /. {m -> 2}
out[1]= 2+9x
</pre>
What I believe to be the equivalent code in sage will only give me 0 instead of an expression which I could then evaluate by setting m=2:
<pre>
#Sage example 1
sage: g = x^2 + 3/2*(x^2 - 1)*x
sage: var('m')
sage: diff(g,x,m)
0
</pre>
I know it is possible to differentiate a variable number of times due to the following:
<pre>
#Sage example 2
sage: diff((x^2-1)^n,x,n)
2*(x^2 - 1)^(n - 1)*n*x*log(x^2 - 1) + 2*(x^2 - 1)^(n - 1)*x
</pre>
I think that example 2 works because I have x raised to the power n.
Is it possible to accomplish the equivalent of what I did in mathematica in sage (i.e. have example 1 not return 0)?
Alternatively if this is not possible what advice do you have for how I could work on making this functionality exist?
Thanks in advance.
Thu, 29 Oct 2015 04:34:28 +0100https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/Answer by eric_g for <p>Hello, <br/>
In mathematica I can differentiate a function m times with respect to x, where m doesn't have an explicit value yet and get an expression that can be evaluated as below:</p>
<pre>#MATHMATICA CODE
in[0] g = x^2 + 3/2*(x^2 - 1)*x
f = D[g, {x, m}]
out[0]= ∂(x,m)(3/2x(-1+x^2))+Power(m,0)[x,2]
in[1] f /. {m -> 2}
out[1]= 2+9x
</pre>
<p>What I believe to be the equivalent code in sage will only give me 0 instead of an expression which I could then evaluate by setting m=2:</p>
<pre>#Sage example 1
sage: g = x^2 + 3/2*(x^2 - 1)*x
sage: var('m')
sage: diff(g,x,m)
0
</pre>
<p>I know it is possible to differentiate a variable number of times due to the following:</p>
<pre>#Sage example 2
sage: diff((x^2-1)^n,x,n)
2*(x^2 - 1)^(n - 1)*n*x*log(x^2 - 1) + 2*(x^2 - 1)^(n - 1)*x
</pre>
<p>I think that example 2 works because I have x raised to the power n.</p>
<p>Is it possible to accomplish the equivalent of what I did in mathematica in sage (i.e. have example 1 not return 0)?
Alternatively if this is not possible what advice do you have for how I could work on making this functionality exist?</p>
<p>Thanks in advance.</p>
https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?answer=30340#post-id-30340Hi,
In Sage, the syntax `diff(g,x,m)` where `x` and `m` are two symbolic variables, does not stand for the `m`-th derivative of `g` with respect to `x`, but for the second order partial derivative of `g` with one derivative taken with respect to `x` and the other one with respect to `m`, i.e. in LaTeX notation \frac{\partial^2 g}{\partial x \partial m}. This explains why example 1 returns 0 (`m` does not appear in `g`), while example 2 returns something nonzero (`(x^2-1)^n` is a function of both `x` and `n`).
If, instead of a symbolic variable, the third argument of `diff` is a non-negative integer `m`, then the outcome is the `m`-th derivative of `g` with respect to `x`: for instance
sage: g = x^2 + 3/2*(x^2 - 1)*x
sage: m = 3 # m is now an integer, not a symbolic variable
sage: diff(g, x, m)
9
Thu, 29 Oct 2015 14:27:09 +0100https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?answer=30340#post-id-30340Comment by vdelecroix for <p>Hi,</p>
<p>In Sage, the syntax <code>diff(g,x,m)</code> where <code>x</code> and <code>m</code> are two symbolic variables, does not stand for the <code>m</code>-th derivative of <code>g</code> with respect to <code>x</code>, but for the second order partial derivative of <code>g</code> with one derivative taken with respect to <code>x</code> and the other one with respect to <code>m</code>, i.e. in LaTeX notation \frac{\partial^2 g}{\partial x \partial m}. This explains why example 1 returns 0 (<code>m</code> does not appear in <code>g</code>), while example 2 returns something nonzero (<code>(x^2-1)^n</code> is a function of both <code>x</code> and <code>n</code>). </p>
<p>If, instead of a symbolic variable, the third argument of <code>diff</code> is a non-negative integer <code>m</code>, then the outcome is the <code>m</code>-th derivative of <code>g</code> with respect to <code>x</code>: for instance</p>
<pre><code>sage: g = x^2 + 3/2*(x^2 - 1)*x
sage: m = 3 # m is now an integer, not a symbolic variable
sage: diff(g, x, m)
9
</code></pre>
https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?comment=30360#post-id-30360This definitely explains the behavior of the Sage *diff* function. However, how could we differentiate n-th time with n being symbolic?Fri, 30 Oct 2015 03:40:57 +0100https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?comment=30360#post-id-30360Comment by slelievre for <p>Hi,</p>
<p>In Sage, the syntax <code>diff(g,x,m)</code> where <code>x</code> and <code>m</code> are two symbolic variables, does not stand for the <code>m</code>-th derivative of <code>g</code> with respect to <code>x</code>, but for the second order partial derivative of <code>g</code> with one derivative taken with respect to <code>x</code> and the other one with respect to <code>m</code>, i.e. in LaTeX notation \frac{\partial^2 g}{\partial x \partial m}. This explains why example 1 returns 0 (<code>m</code> does not appear in <code>g</code>), while example 2 returns something nonzero (<code>(x^2-1)^n</code> is a function of both <code>x</code> and <code>n</code>). </p>
<p>If, instead of a symbolic variable, the third argument of <code>diff</code> is a non-negative integer <code>m</code>, then the outcome is the <code>m</code>-th derivative of <code>g</code> with respect to <code>x</code>: for instance</p>
<pre><code>sage: g = x^2 + 3/2*(x^2 - 1)*x
sage: m = 3 # m is now an integer, not a symbolic variable
sage: diff(g, x, m)
9
</code></pre>
https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?comment=53636#post-id-53636Differentiating `n` times, with `n` symbolic, means `n` could be
any nonnegative integer, but we don't want to specify which.
A bit like taking the `n`-th power of a matrix, with `n` symbolic.
Some functions are nice enough that there is a general formula for
their `n`-th derivative. For instance,
- If `f = exp(x)`, then `diff(f, x, n)` is `exp(x)`
- If `f = x^m` for some nonnegative integer `m`, then `diff(f, x, n)`
is `m!/(m-n)! x^(m-n)` for `0 <= n <= m`, and `0 for n > m`.Mon, 28 Sep 2020 12:26:21 +0200https://ask.sagemath.org/question/30330/derivative-of-order-varm-returns-0/?comment=53636#post-id-53636