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.Sun, 09 Jan 2022 07:47:18 +0100How do diff(y,x) and diff(y)/diff(x) differ?https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/Hello,
Consider the following Sage code:
x = var('x')
y = 2*x
diff(y,x) #gives 2 as expected
diff(x,y) #expected 1/2, complains: "argument symb must be a symbol"
This can be resolved via (kind-off) Leibniz's notation approach:
x = var('x')
y = 2*x
diff(y)/diff(x) #gives 2 as expected
diff(x)/diff(y) #gives 1/2 as expected
Example above illustrates issue I keep having (Sage expressions of type diff(x,y) giving error where there exist a derivative)
Questions are:
1. Does example below shows "limitations" of what Sage can compute ?
2. Is there a difference between diff(y)/diff(x) and diff(y,x) ?
3. Is there a way to help Sage "recognize" that diff(x,y) = 1/2 ? If yes, how?
P.S. (If 3 is "Yes") This seems very basic problem, yet I was unable to find any related examples online.
Is there a reference that explains described behavior o diff?
**Update** after **Emmanuel Charpentier**'s answer
It appears that I am conflicting symbolic "function" and "expression" types you mentioned, i.e. I expect `diff(x)` to return the total differential of f(x)=x. And it seems `diff(...)` does dive differential when defining x using "function" syntax (although both `x(x)=x` and `var('x')` cases have the same "<'...Expression'>" type).
x = var('x')
diff(x) #same as diff(x,x), as explained by Emmanuel Charpentier
type(x) #<class 'sage.symbolic.expression.Expression'>
x(x) = x
type(x) #<class 'sage.symbolic.expression.Expression'>
diff(x) #gives differential "x |--> 1", what I assumed it does for single argument diff()
#"function" approach works for multivirable where "expression" gave error
myCos(x,y) = cos(x*y)
diff(myCos) #gives differential "(x, y) |--> (-y*sin(x*y), -x*sin(x*y))"
My last remaining question is:
Given object "x", how to tell whether it's a "function" or "expression" ?
P.S. There is good reading on the "expression vs. function" business in docs.
https://doc.sagemath.org/html/en/tutorial/tour_functions.htmlTue, 14 Dec 2021 02:49:30 +0100https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/Comment by cav_rt for <p>Hello, </p>
<p>Consider the following Sage code:</p>
<pre><code>x = var('x')
y = 2*x
diff(y,x) #gives 2 as expected
diff(x,y) #expected 1/2, complains: "argument symb must be a symbol"
</code></pre>
<p>This can be resolved via (kind-off) Leibniz's notation approach:</p>
<pre><code>x = var('x')
y = 2*x
diff(y)/diff(x) #gives 2 as expected
diff(x)/diff(y) #gives 1/2 as expected
</code></pre>
<p>Example above illustrates issue I keep having (Sage expressions of type diff(x,y) giving error where there exist a derivative)</p>
<p>Questions are:</p>
<ol>
<li><p>Does example below shows "limitations" of what Sage can compute ?</p></li>
<li><p>Is there a difference between diff(y)/diff(x) and diff(y,x) ?</p></li>
<li><p>Is there a way to help Sage "recognize" that diff(x,y) = 1/2 ? If yes, how?</p></li>
</ol>
<p>P.S. (If 3 is "Yes") This seems very basic problem, yet I was unable to find any related examples online.
Is there a reference that explains described behavior o diff?</p>
<p><strong>Update</strong> after <strong>Emmanuel Charpentier</strong>'s answer</p>
<p>It appears that I am conflicting symbolic "function" and "expression" types you mentioned, i.e. I expect <code>diff(x)</code> to return the total differential of f(x)=x. And it seems <code>diff(...)</code> does dive differential when defining x using "function" syntax (although both <code>x(x)=x</code> and <code>var('x')</code> cases have the same "<'...Expression'>" type).</p>
<pre><code>x = var('x')
diff(x) #same as diff(x,x), as explained by Emmanuel Charpentier
type(x) #<class 'sage.symbolic.expression.Expression'>
x(x) = x
type(x) #<class 'sage.symbolic.expression.Expression'>
diff(x) #gives differential "x |--> 1", what I assumed it does for single argument diff()
#"function" approach works for multivirable where "expression" gave error
myCos(x,y) = cos(x*y)
diff(myCos) #gives differential "(x, y) |--> (-y*sin(x*y), -x*sin(x*y))"
</code></pre>
<p>My last remaining question is:</p>
<p>Given object "x", how to tell whether it's a "function" or "expression" ?</p>
<p>P.S. There is good reading on the "expression vs. function" business in docs.
<a href="https://doc.sagemath.org/html/en/tutorial/tour_functions.html">https://doc.sagemath.org/html/en/tuto...</a></p>
https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60263#post-id-60263See the chapter 2 of the free book [Computational Mathematics with SageMath](https://www.sagemath.org/sagebook/english.html), it's an excellent introduction to the sage symbolics.Tue, 14 Dec 2021 20:45:44 +0100https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60263#post-id-60263Answer by Emmanuel Charpentier for <p>Hello, </p>
<p>Consider the following Sage code:</p>
<pre><code>x = var('x')
y = 2*x
diff(y,x) #gives 2 as expected
diff(x,y) #expected 1/2, complains: "argument symb must be a symbol"
</code></pre>
<p>This can be resolved via (kind-off) Leibniz's notation approach:</p>
<pre><code>x = var('x')
y = 2*x
diff(y)/diff(x) #gives 2 as expected
diff(x)/diff(y) #gives 1/2 as expected
</code></pre>
<p>Example above illustrates issue I keep having (Sage expressions of type diff(x,y) giving error where there exist a derivative)</p>
<p>Questions are:</p>
<ol>
<li><p>Does example below shows "limitations" of what Sage can compute ?</p></li>
<li><p>Is there a difference between diff(y)/diff(x) and diff(y,x) ?</p></li>
<li><p>Is there a way to help Sage "recognize" that diff(x,y) = 1/2 ? If yes, how?</p></li>
</ol>
<p>P.S. (If 3 is "Yes") This seems very basic problem, yet I was unable to find any related examples online.
Is there a reference that explains described behavior o diff?</p>
<p><strong>Update</strong> after <strong>Emmanuel Charpentier</strong>'s answer</p>
<p>It appears that I am conflicting symbolic "function" and "expression" types you mentioned, i.e. I expect <code>diff(x)</code> to return the total differential of f(x)=x. And it seems <code>diff(...)</code> does dive differential when defining x using "function" syntax (although both <code>x(x)=x</code> and <code>var('x')</code> cases have the same "<'...Expression'>" type).</p>
<pre><code>x = var('x')
diff(x) #same as diff(x,x), as explained by Emmanuel Charpentier
type(x) #<class 'sage.symbolic.expression.Expression'>
x(x) = x
type(x) #<class 'sage.symbolic.expression.Expression'>
diff(x) #gives differential "x |--> 1", what I assumed it does for single argument diff()
#"function" approach works for multivirable where "expression" gave error
myCos(x,y) = cos(x*y)
diff(myCos) #gives differential "(x, y) |--> (-y*sin(x*y), -x*sin(x*y))"
</code></pre>
<p>My last remaining question is:</p>
<p>Given object "x", how to tell whether it's a "function" or "expression" ?</p>
<p>P.S. There is good reading on the "expression vs. function" business in docs.
<a href="https://doc.sagemath.org/html/en/tutorial/tour_functions.html">https://doc.sagemath.org/html/en/tuto...</a></p>
https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?answer=60250#post-id-60250Sage's hell is somehow paved with its authors' good intentions...
`diff(...)` should denote the derivative of an expression (its first argument)
*with respect to a variable* which should be given as its second argument.
Here, this second argument is missing, so, instead of failing, Sage attempts
to guess it by establishing the list of its variables, which turns out to be
a singleton; it uses this unique variable as the derivation variable. As in:
sage: diff(cos(x))
-sin(x)
But this shortcut fails if the expression has more than one variable :
sage: diff(cos(x*y))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[ Snip...]
ValueError: No differentiation variable specified.
Morality: always specify your differentiation variable,
notwithstanding Sage's attempt to save your bacon...
BTW: regarding your expectations:
After running `y = 2*x`, the Python variable `y` has the value `2*x`,
which is a symbolic expression; as a consequence, `y` is neither
a symbolic variable nor a function. To define `y` as a symbolic
variable, use `var("y")`; to define `y` as a function, use `y(x) = 2*x`.
**EDIT :**
> Given object "x", how to tell whether it's a "function" or "expression" ?
This one deserved its own question, not an addendum...
Compare :
sage: ex=sin(x)
sage: f(x)=sin(x)
sage: ex.is_callable()
False
sage: f.is_callable()
True
Tue, 14 Dec 2021 10:32:39 +0100https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?answer=60250#post-id-60250Comment by Emmanuel Charpentier for <p>Sage's hell is somehow paved with its authors' good intentions...</p>
<p><code>diff(...)</code> should denote the derivative of an expression (its first argument)
<em>with respect to a variable</em> which should be given as its second argument.</p>
<p>Here, this second argument is missing, so, instead of failing, Sage attempts
to guess it by establishing the list of its variables, which turns out to be
a singleton; it uses this unique variable as the derivation variable. As in:</p>
<pre><code>sage: diff(cos(x))
-sin(x)
</code></pre>
<p>But this shortcut fails if the expression has more than one variable :</p>
<pre><code>sage: diff(cos(x*y))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
</code></pre>
<p>[ Snip...]</p>
<pre><code>ValueError: No differentiation variable specified.
</code></pre>
<p>Morality: always specify your differentiation variable,
notwithstanding Sage's attempt to save your bacon...</p>
<p>BTW: regarding your expectations:</p>
<p>After running <code>y = 2*x</code>, the Python variable <code>y</code> has the value <code>2*x</code>,
which is a symbolic expression; as a consequence, <code>y</code> is neither
a symbolic variable nor a function. To define <code>y</code> as a symbolic
variable, use <code>var("y")</code>; to define <code>y</code> as a function, use <code>y(x) = 2*x</code>.</p>
<p><strong>EDIT :</strong></p>
<blockquote>
<p>Given object "x", how to tell whether it's a "function" or "expression" ?</p>
</blockquote>
<p>This one deserved its own question, not an addendum...</p>
<p>Compare :</p>
<pre><code>sage: ex=sin(x)
sage: f(x)=sin(x)
sage: ex.is_callable()
False
sage: f.is_callable()
True
</code></pre>
https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60593#post-id-60593See the edit...Sun, 09 Jan 2022 07:47:18 +0100https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60593#post-id-60593Comment by AlexKnyazev for <p>Sage's hell is somehow paved with its authors' good intentions...</p>
<p><code>diff(...)</code> should denote the derivative of an expression (its first argument)
<em>with respect to a variable</em> which should be given as its second argument.</p>
<p>Here, this second argument is missing, so, instead of failing, Sage attempts
to guess it by establishing the list of its variables, which turns out to be
a singleton; it uses this unique variable as the derivation variable. As in:</p>
<pre><code>sage: diff(cos(x))
-sin(x)
</code></pre>
<p>But this shortcut fails if the expression has more than one variable :</p>
<pre><code>sage: diff(cos(x*y))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
</code></pre>
<p>[ Snip...]</p>
<pre><code>ValueError: No differentiation variable specified.
</code></pre>
<p>Morality: always specify your differentiation variable,
notwithstanding Sage's attempt to save your bacon...</p>
<p>BTW: regarding your expectations:</p>
<p>After running <code>y = 2*x</code>, the Python variable <code>y</code> has the value <code>2*x</code>,
which is a symbolic expression; as a consequence, <code>y</code> is neither
a symbolic variable nor a function. To define <code>y</code> as a symbolic
variable, use <code>var("y")</code>; to define <code>y</code> as a function, use <code>y(x) = 2*x</code>.</p>
<p><strong>EDIT :</strong></p>
<blockquote>
<p>Given object "x", how to tell whether it's a "function" or "expression" ?</p>
</blockquote>
<p>This one deserved its own question, not an addendum...</p>
<p>Compare :</p>
<pre><code>sage: ex=sin(x)
sage: f(x)=sin(x)
sage: ex.is_callable()
False
sage: f.is_callable()
True
</code></pre>
https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60262#post-id-60262Useful insight, thank you. I updated the question accordingly.
Given object "x", how to tell whether it's a "function" or "expression" ?Tue, 14 Dec 2021 19:03:49 +0100https://ask.sagemath.org/question/60248/how-do-diffyx-and-diffydiffx-differ/?comment=60262#post-id-60262