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

Implement eq1.subs(eq2) #9

Closed
Davide-sd opened this issue Aug 25, 2022 · 4 comments · Fixed by #11
Closed

Implement eq1.subs(eq2) #9

Davide-sd opened this issue Aug 25, 2022 · 4 comments · Fixed by #11
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Davide-sd
Copy link
Contributor

Would it be possible to overwrite the subs method so that it can accept a single argument, specifically an object of type Equation? So, the left hand side represents the expression to substitute, the right hand side represents the new value.

@gutow
Copy link
Owner

gutow commented Aug 25, 2022

I have thought about this some and think it should be done. I have not dug into what I would have to override in the subs module to make it work. I think it may be as simple as checking for equations and parsing them before calling the normal subs modules.

This is not completely straightforward in an external package as it requires overriding things defined in the Expr class. This code already does some pretty substantial tweaking of the namespace to extend the FunctionClass to work with equations. It would all be much easier if this could be incorporated directly into sympy, but we have not made progress on making the decisions necessary to accept sympy/SymPEPs#1, which would roll them together.

It is on my radar, but relatively low priority compared to getting more tests built so this package can get to the stable stage and continuing to try to get a positive consensus on embedding this code in sympy.

If you or someone else can get this working, I would love to get a pull-request.

@gutow gutow added enhancement New feature or request help wanted Extra attention is needed labels Aug 25, 2022
@Davide-sd
Copy link
Contributor Author

If you or someone else can get this working, I would love to get a pull-request.

I might give it a try next week! :)

@gutow
Copy link
Owner

gutow commented Aug 25, 2022

I might give it a try next week! :)

Thank you.

@Davide-sd
Copy link
Contributor Author

I gave it a quick try, and it seems that the following could work:

    def subs(self, *args, **kwargs):
        new_args = args
        if (len(args) == 1) and isinstance(args[0], self.func):
            new_args = [{args[0].args[0]: args[0].args[1]}]
        return super().subs(*new_args, **kwargs)

While I'd really love the possibility of simply writing eq1.subs(eq2), there is a conceptual question that we should discuss first.

Currently, subs accepts these type of arguments (no matter the type of object we are modifying (expression, matrix, vector, ...)):

  • subs(old, new)
  • subs([(old1, new1), (old2, new2), ...])
  • subs({old1: new1, ...})

Do we really want to modify the signature to implement this new behavior? (I vote for yes :) )

Having played with the module on a few examples, I noticed that often I need to substitute multiple equations into a target equation. An alternative would be to create a function subs_dict(*eqns) that, given a list of equations it creates a substitution dictionary where each key represents the LHS and the value represents the RHS. Result: eq1.subs(subs_dict(eq2, eq3, ...))

Maybe we can implement both approaches...

@gutow gutow linked a pull request Aug 29, 2022 that will close this issue
@gutow gutow closed this as completed in #11 Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants