Sympy: Get the solutions of an ODE as Function objects

As a follow-up of Extract the two functions solutions of a homogeneous second order linear ODE, I want to get u1 and u2 as Function objects. This way, I could use them in generic expressions, e.g., definite/indefinite integrals in user-defined functions, etc. E.g., to calculate particular solutions of the ODE. A workaround is shown below, but I see it clumsy, and possibly fragile.

This is what I have

>>> print(u1)
>>> print(type(u1))
>>> print(u2)
>>> print(type(u2))

1/r
<class 'sympy.core.power.Pow'>
r
<class 'sympy.core.symbol.Symbol'>

I want to define

p = sym.Function('p', real=True)
def a1_par(u1, p):
    # From the standard form, g(r) = p'(r)
    R = sym.symbols('R', real=True, positive=True)
    g = sym.diff(p(R), R)
    # This would not work, since u1 is already a function of r
    # I need a Function object, so I can use u1(R) in the integrand
    #a1 = sym.integrate(u1*g, (R, 0, r)).doit()    # not useful
    a1 = sym.integrate(u1.subs(r,R)*g, (R, 0, r)).doit()    # workaround
    return a1

In fact, the parameter I am passing to a1_par is a function of r, not merely a Function object. If u1 were a function object, my integration line would read

    a1 = sym.integrate(u1(R)*g, (R, 0, r)).doit()

Moreover, the return value of a1_par is again a function of r. I would like to get a "handle" to the function (a Function object), so I can use it in other calculations.

Related:

  1. How to define a mathematical function in SymPy?

Upvotes: 0

Views: 49

Answers (0)

Related Questions