Ask Your Question
1

How do diff(y,x) and diff(y)/diff(x) differ?

asked 2021-12-14 02:55:00 +0100

AlexKnyazev gravatar image

updated 2021-12-14 19:03:04 +0100

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/tuto...

edit retag flag offensive close merge delete

Comments

2

See the chapter 2 of the free book Computational Mathematics with SageMath, it's an excellent introduction to the sage symbolics.

cav_rt gravatar imagecav_rt ( 2021-12-14 20:45:44 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2021-12-14 10:32:39 +0100

Emmanuel Charpentier gravatar image

updated 2021-12-21 11:52:59 +0100

Sage'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
edit flag offensive delete link more

Comments

Useful insight, thank you. I updated the question accordingly.

Given object "x", how to tell whether it's a "function" or "expression" ?

AlexKnyazev gravatar imageAlexKnyazev ( 2021-12-14 19:03:49 +0100 )edit

See the edit...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-01-09 07:47:18 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2021-12-14 02:49:30 +0100

Seen: 784 times

Last updated: Dec 21 '21