Defining custom invertible functions

16 views
Skip to first unread message

Oscar Benjamin

unread,
Sep 12, 2018, 9:47:56 AM9/12/18
to sympy
Hi,

I was just looking at a way to solve ODEs algebraically and came up with the code below which almost works (just needs integration constants). I have a few questions though.

1. What is the right way to define an arbitrary invertible function and its inverse?

2. Is the code below abusing doit() or is that a reasonable way to use it?

3. Should I check for the inverses in __new__ or is there a better way to do that?

4. Does this represent a reasonable approach for something that could be implemented in dsolve?

5. How can I make a different integration constant each time I call intx.doit()?

class diffx(Function):
    def __new__(cls, expr):
        if isinstance(expr, intx):
            return expr.args[0]
        else:
            return super().__new__(cls, expr)
    def inverse(self):
        return intx

class intx(Function):
    def __new__(cls, expr):
        if isinstance(expr, diffx):
            return expr.args[0]
        else:
            return super().__new__(cls, expr)
    def inverse(self):
        return intx
    def doit(self):
        return Integral(self.args[0].doit(), x).doit()  # + Symbol('C')

eqn = diffx(x*diffx(f(x)))/x - exp(x)
soln,= solve(eqn, f(x))
print(soln.doit())

--
Oscar

Aaron Meurer

unread,
Sep 12, 2018, 3:22:05 PM9/12/18
to sy...@googlegroups.com
On Wed, Sep 12, 2018 at 7:47 AM, Oscar Benjamin
<oscar.j....@gmail.com> wrote:
> Hi,
>
> I was just looking at a way to solve ODEs algebraically and came up with the
> code below which almost works (just needs integration constants). I have a
> few questions though.
>
> 1. What is the right way to define an arbitrary invertible function and its
> inverse?

I believe so. There was an issue about being able to make undefined
functions invertible (I can't find it right now), but it isn't
implemented.

>
> 2. Is the code below abusing doit() or is that a reasonable way to use it?

It's fine. The only potential issue is that it will also evaluate any
other unevaluated subexpression.

>
> 3. Should I check for the inverses in __new__ or is there a better way to do
> that?

You shouldn't define __new__ on Function subclasses. Rather, define
the classmethod eval, which returns what the function should evaluate
to, or None if it shouldn't evaluate.

>
> 4. Does this represent a reasonable approach for something that could be
> implemented in dsolve?

As far as I know it should work. It might even solve weird ODEs like

>
> 5. How can I make a different integration constant each time I call
> intx.doit()?

I think there is a iterator in the dsolve module that gives new constants.

Aaron Meurer

>
> class diffx(Function):
> def __new__(cls, expr):
> if isinstance(expr, intx):
> return expr.args[0]
> else:
> return super().__new__(cls, expr)
> def inverse(self):
> return intx
>
> class intx(Function):
> def __new__(cls, expr):
> if isinstance(expr, diffx):
> return expr.args[0]
> else:
> return super().__new__(cls, expr)
> def inverse(self):
> return intx
> def doit(self):
> return Integral(self.args[0].doit(), x).doit() # + Symbol('C')
>
> eqn = diffx(x*diffx(f(x)))/x - exp(x)
> soln,= solve(eqn, f(x))
> print(soln.doit())
>
> --
> Oscar
>
> --
> 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+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/CAHVvXxS%2Brg9Jd4U%2BiHQH%3Df64MvJKxPB4RpO9t%3DLYfyGzESuyDA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages