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

ENH: mark @unevaluated arguments as non-sympy #380

Merged
merged 4 commits into from
Dec 21, 2023

Conversation

redeboer
Copy link
Member

Major improvement to the @unevaluated decorator that makes it behave more like @dataclass. Arguments are now converted Field objects so that they can get properties. One of those implemented here is the 'sympified' specification (switched on by default). A new argument() field specifier (the equivalent of field() allows indicating that some arguments should not be sympified. This allows for constructs like the following:

>>> class Transformation:
...     def __call__(self, x: sp.Basic, y: sp.Basic) -> sp.Expr: ...
...
>>> @unevaluated
... class MyExpr(sp.Expr):
...     x: Any
...     y: Any
...     functor: Callable = argument(sympify=False)
...
...     def evaluate(self) -> sp.Expr:
...         return self.functor(self.x, self.y)
...
>>> expr = MyExpr(0, y=3.14, functor=Transformation)
>>> isinstance(expr.x, sp.Integer)
True
>>> isinstance(expr.y, sp.Float)
True
>>> expr.functor is Transformation

and is especially useful for defining classes like EnergyDependentWidth, which require a PhaseSpaceFactor protocol.

@redeboer redeboer added the ⚙️ Enhancement Improvements and optimizations of existing features label Dec 21, 2023
@redeboer redeboer self-assigned this Dec 21, 2023
@redeboer redeboer enabled auto-merge (squash) December 21, 2023 20:15
@redeboer redeboer merged commit e341327 into main Dec 21, 2023
23 checks passed
@redeboer redeboer deleted the dataclass-implementation branch December 21, 2023 20:16
redeboer added a commit that referenced this pull request Dec 22, 2023
* ENH: implement `unevaluated` with `dataclasses`
* ENH: implement `__slots__` for non-sympy arguments
* ENH: implement `pickle` support
* ENH: do not overwrite `_eval_subs` if no non-sympy attributes
@redeboer redeboer added this to the 0.14.8 milestone Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚙️ Enhancement Improvements and optimizations of existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant