Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Expression substitution interface inconsistency #23320

Closed
rwst opened this issue Jun 23, 2017 · 8 comments
Closed

Fix Expression substitution interface inconsistency #23320

rwst opened this issue Jun 23, 2017 · 8 comments

Comments

@rwst
Copy link

rwst commented Jun 23, 2017

From tomchor on #78

...
>>> a1=var('a_1')
>>> E=x^a1
>>> E(a1=1)

 a₁
x  
>>> E(a_1=1)
x
...
>>> E.subs({a1:1})
x

Component: symbolics

Issue created by migration from https://trac.sagemath.org/ticket/23320

@rwst rwst added this to the sage-8.1 milestone Jun 23, 2017
@tscrim
Copy link
Collaborator

tscrim commented Jun 23, 2017

comment:1

I disagree. I would say this is a misuse and tomchor is backwards on what is a representation and what is a variable. a1 is the representation (i.e., a pointer) in !Python/Sage of the variable a_1 that is used by the symbolic expression. One reason why we should keep things the way they are is

a1 = var('x')
a2 = var('x')
f = (a1 + e^a2)

is a function in x and cannot be evaluated separately by a1 and a2.

@tscrim tscrim removed this from the sage-8.1 milestone Jun 23, 2017
@rwst

This comment has been minimized.

@rwst
Copy link
Author

rwst commented Jun 24, 2017

comment:2

I agree with your explanation. But note that this ticket is about the inconsistency between

sage: E.subs({a1:1})
x
sage: E(a1=1)
x^a_1

It happens because the dict key is immediately replaced but the kwds dict key in a1=1 is not:

sage: {a1:1}
{a_1: 1}

@rwst rwst added this to the sage-8.1 milestone Jun 24, 2017
@tscrim
Copy link
Collaborator

tscrim commented Jun 24, 2017

comment:3

There is no way around that either because it points to the "correct" variable and because they key of the dict is the variable, not a string. So by the time you are doing the substitution, it is evaluated to 'a_1'. It is the same problem if I called the Python variable x. It comes down to understanding the difference between a python variable and the actual symbolic variable. IMO, this is the correct behavior.

@tscrim tscrim removed this from the sage-8.1 milestone Jun 24, 2017
@rwst
Copy link
Author

rwst commented Jun 24, 2017

comment:4

You are sure that the entry a1 in the kwds dict that is created with E(a1=1) cannot be replaced with a_1, in order to make the behaviour consistent? I disagree. The dict entry there is a string, and the problem is 'just' to get the object that is pointed to. This knowledge is somewhere in the ipython shell, isn't it?

@tscrim
Copy link
Collaborator

tscrim commented Jun 24, 2017

comment:5

Replying to @rwst:

You are sure that the entry a1 in the kwds dict that is created with E(a1=1) cannot be replaced with a_1, in order to make the behaviour consistent? I disagree. The dict entry there is a string, and the problem is 'just' to get the object that is pointed to. This knowledge is somewhere in the ipython shell, isn't it?

Yes, I am sure. In the kwds dict, the input is the string 'a1', not the variable a1. We could search the Python namespace for a variable named a1. However, this has a number of shortcomings: the Python a1 could be anything, or worse, someone did a1 = var('x') and then the behavior is extremely unexpected (albeit a slightly contrived of an example). Also, how would you differentiate if someone wanted to use variables a1 and a_1? What about if it is defined as a local variable in a function?

I believe that anytime we have to go find a variable in all Python variables (which is very [relatively] expensive) gives off a very strong code smell. In my mind, the variable is a_1, not the Python pointer a1, and we should not move away from that as it is a huge can of worms.

@rwst
Copy link
Author

rwst commented Jun 24, 2017

comment:7

IMHO for this inherent inconsistency the usage of ex.subs(a=b) should be discouraged in favor of ex.subs(a==b).

@embray
Copy link
Contributor

embray commented Jul 13, 2017

comment:8

Closing tickets in the sage-duplicate/invalid/wontfix module with positive_review (i.e. someone has confirmed they should be closed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants