eval() is only available for Function subclasses. For Expr subclasses, you have to define __new__, which unfortunately isn't as convenient as eval(). A basic version would look something like
def __new__(cls, m, n, l): m, n, l = _sympify(m), _sympify(n), _sympify(l) if n == 0: return S.Zero return Expr.__new__(cls, m, n, l) Note that you need to manually handle making sure inputs and outputs are SymPy types. I would suggest looking at the source code for Integral.__new__ and ExprWithLimits.__new__. Aaron Meurer On Tue, Jun 11, 2024 at 6:01 AM Michael Gfrerer <mhgfre...@gmail.com> wrote: > > Thanks for your answers. I was thinking of making a custom class, but wanted > to be sure I wasn't reinventing the wheel. > I made a first attempt to define a custom class, but I stumbled upon an issue > with eval and simplify. If the integrand is 0 the whole integral can be set > to 0. > So I tried the followiung code without success: > > import sympy as sp > class LebesgueIntegral(sp.Expr): > def _latex(self, printer, exp=1): > m, n, l = self.args > _m, _n, _l = printer._print(m), printer._print(n), printer._print(l) > return r'\int_{%s} %s \,d%s' % (_m, _n, _l) > > @classmethod > def eval(cls, m, n, l): > if n == 0: > return 0 > > def _eval_simplify(self, **kwargs): > if self.args[1] == 0: > return 0 > return self > > u = LebesgueIntegral(sp.Symbol('\Omega'), sp.sympify(0),sp.Symbol('x')) > print(sp.latex(u)) > print(sp.simplify(u)) > print(sp.simplify(2*u)) > > The above code gives me the output: > \int_{\Omega} 0 \,dx > 0 > 2*LebesgueIntegral(\Omega, 0, x) > > Why is the method eval not called in the first print statement? > I had a look at the simplify module but it is very hard for me to understand. > Is it intended that the simplify method of the class is not called in the > third print statement? > > Michael Gfrerer > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/4f46ffe4-e4ca-4d14-bcb5-2402574af17dn%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2B2PThqfQmu6uSk03aQOxA4HR3gOzvxZoOUB3fRdbBB3g%40mail.gmail.com.