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

Adding support for executing attribute/query calls without assigning to a prior variable #2337

Merged
merged 3 commits into from
Sep 27, 2023

Conversation

anutosh491
Copy link
Collaborator

@anutosh491 anutosh491 commented Sep 25, 2023

I realize that we couldn't access attributes or query methods without assigning the expression to a variable beforehand.
So this works

from sympy import pi, Symbol, sin, cos

def main0():
    x: S = Symbol("x")
    a: S = sin(x)
    print(a.diff(x))

main0()

But these cases don't

from sympy import pi, Symbol, sin, cos

def main0():
    x: S = Symbol("x")
    print(sin(x).diff(x))
    print(sin(x).diff(x).diff(x))

main0()

@anutosh491
Copy link
Collaborator Author

Converting to draft as few corner cases need to be checked and the symbolics_05.py test file needs to be updated with these cases.

@anutosh491 anutosh491 marked this pull request as draft September 25, 2023 06:17
@anutosh491 anutosh491 marked this pull request as ready for review September 26, 2023 04:39
@anutosh491
Copy link
Collaborator Author

anutosh491 commented Sep 26, 2023

The has attribute can be added to the set of attributes once #2336 is merged. We can first merge this and then rebase that pr on top of this I think !

@certik
Copy link
Contributor

certik commented Sep 26, 2023

Thanks! I need to think about if this is the right way to fix it. It seems to be adding symbolics stuff into the frontend at places where maybe it shouldn't belong. I'll get back to you.

@anutosh491
Copy link
Collaborator Author

anutosh491 commented Sep 26, 2023

Yeah I sometimes was suspecting if this is moving in the wrong place but then let's say we have pi.diff(x), the ast treats pi like any other name and if we run the program, it throws back the error saying the variable pi wasn't defined, hence I felt that we could try fixing this right from where the error spawns up which was handle_attributes.
So essentially there are 3 blocks

  1. For constants
  2. For binary opertors
  3. For functions

I think so we could stick to this but I am also not sure if we could do better !

@certik
Copy link
Contributor

certik commented Sep 26, 2023

@czgdp1807, @Thirumalai-Shaktivel, @Shaikh-Ubaid any ideas how to best support this? We want to support custom SymPy methods, but do not want to pollute the frontend at the wrong places. I think this is similar to custom NumPy methods that we support (or will support).

@Thirumalai-Shaktivel
Copy link
Collaborator

As Anutosh said, the constant like pi will be treated as variable and it is not declared in current scope, the same goes for function call as well.
So for that, I think:

  1. We can add a new node (let's say SymbolicItems or SymbolicObjects) and handle this in the Symbolic pass.
  2. We can add more symbols like SymbolicFunction, SymbolicConstant, SymbolicBinOp.

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to improve it. We need this feature, so let's merge this for now. We might need to improve it later, once we know how.

@certik certik merged commit a872c0b into lcompilers:main Sep 27, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants