Skip to content

Commit

Permalink
DOC: explain usage of argument() function
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Feb 12, 2024
1 parent 6262c7c commit 183c092
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/usage/sympy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,72 @@
"Math(aslatex({e: e.doit() for e in exprs}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, instance attributes are converted ['sympified'](https://docs.sympy.org/latest/modules/core.html#module-sympy.core.sympify). To avoid this behavior, use the {func}`.argument` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import Callable\n",
"\n",
"from ampform.sympy import argument\n",
"\n",
"\n",
"class Transformation:\n",
" def __init__(self, power: int) -> None:\n",
" self.power = power\n",
"\n",
" def __call__(self, x: sp.Basic, y: sp.Basic) -> sp.Expr:\n",
" return x + y**self.power\n",
"\n",
"\n",
"@unevaluated\n",
"class MyExpr(sp.Expr):\n",
" x: Any\n",
" y: Any\n",
" functor: Callable = argument(sympify=False)\n",
"\n",
" def evaluate(self) -> sp.Expr:\n",
" return self.functor(self.x, self.y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the `functor` attribute has not been sympified (there is no SymPy equivalent for a callable object), but the `functor` can be called in the `evaluate()`/`doit()` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a, b, k = sp.symbols(\"a b k\")\n",
"expr = MyExpr(a, y=b, functor=Transformation(power=k))\n",
"assert expr.x is a\n",
"assert expr.y is b\n",
"assert not isinstance(expr.functor, sp.Basic)\n",
"Math(aslatex({expr: expr.doit()}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
":::{tip}\n",
"An example where this is used, is in the {class}`.EnergyDependentWidth` class, where we do not want to sympify the {attr}`~.EnergyDependentWidth.phsp_factor` protocol.\n",
":::"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 183c092

Please sign in to comment.