Reconstructing a symbolic expression with derivative from the string representation

42 views
Skip to first unread message

Eric Gourgoulhon

unread,
Feb 22, 2017, 8:27:31 AM2/22/17
to sage-support
Hi,

I am puzzled by the following code in sage 7.5:

sage: df = diff(function('f')(x), x); df
diff
(f(x), x)
sage
: repr(df)
'diff(f(x), x)'
sage
: df1 = SR(repr(df)); df1
diff
(f(x), x)

df and df1 look the same and we even have

sage: bool(df1 == df)
True

However, they are not identical:

sage: latex(df)
\frac{\partial}{\partial x}f\left(x\right)
sage
: latex(df1)
{\rm diff}\left(f\left(x\right), x\right)

sage: df.operator()
D
[0](f)
sage
: df1.operator()
diff

sage: diff(df, x)
diff
(f(x), x, x)
sage
: diff(df1, x)
D
[0](diff)(f(x), x)*diff(f(x), x) + D[1](diff)(f(x), x)

Any thoughts? Shouldn't SR(repr(df)) always give back df? If not, how to recover df from repr(df) in the current case?

Eric.

Nils Bruin

unread,
Feb 22, 2017, 1:11:50 PM2/22/17
to sage-support
Yes, that is problematic. This explains it:

sage: df = diff(function('f')(x), x); df
diff
(f(x), x)
sage
: repr(df)
'diff(f(x), x)'
sage
: df1 = SR(repr(df));
df1

so the second result is what you get from

function('diff')(f(x),x)

I suspect the only reason why df1 behaves a bit like df is because of name clashes (in maxima for instance)

Clearly, SR('diff(...)') operates in a scope where "diff" isn't bound to the toplevel "diff".

Ralf Stephan

unread,
Feb 23, 2017, 1:49:11 AM2/23/17
to sage-support
On Wednesday, February 22, 2017 at 7:11:50 PM UTC+1, Nils Bruin wrote:
Clearly, SR('diff(...)') operates in a scope where "diff" isn't bound to the toplevel "diff".

Yes, there is no symbolic diff function and so it does not appear
in the dictionary for translating strings to expressions. It has other
consequences too so I consider writing a dummy function like
Function_sum a good idea.

Regards,

Nils Bruin

unread,
Feb 23, 2017, 2:47:50 AM2/23/17
to sage-support
On Wednesday, February 22, 2017 at 10:49:11 PM UTC-8, Ralf Stephan wrote:
Yes, there is no symbolic diff function and so it does not appear
in the dictionary for translating strings to expressions. It has other
consequences too so I consider writing a dummy function like
Function_sum a good idea.
 
I agree. In fact, this printing has only been in place since https://trac.sagemath.org/ticket/21286 . Before we'd get D[0](f)(x). That's in fact also not so hard to parse. With something along the lines of:

from sage.symbolic.operators import FDerivativeOperator
class Doperator(object):
  def __getitem__(self,L):
    if not(isinstance(L,tuple)):
      L=(L,)
    return lambda f: FDerivativeOperator(f,L)
D=Doperator()
 
in place, we can just write D[0](sin)(x) .

If you're going to make diff(...) parseable, perhaps we can make the above as well. Whether we can bind this to the toplevel letter D is another matter.
Reply all
Reply to author
Forward
0 new messages