You seem to be quite a bit confused here.
First off, you are right that you aren't using nargs correctly. nargs
lists the number of arguments a function can take, not its signature.
However, you don't need to define a Function subclass at all. Function
subclasses let you define functions that are unevaluated, which
happens when eval() returns None. But if you never return None from
eval(), there's no point to having a Function subclass. That's the
same as just a normal Python function. Or, more simply, you can just
define an expression, like
mu, m_t, g, r, p_phi, p_r = sp.symbols('mu, m_t, g, r, p_phi, p_r')
h_Kepler_two_body_polar = p_r**2/(2*mu) + p_phi**2/(2*mu*r**2) - g*mu*m_t/r
H_Ktb_polar_lf = sp.lambdify(
[r, p_r, p_phi, (mu, m_t, g)],
h_Kepler_two_body_polar, 'numpy')
It's not necessary to make h_Kepler_two_body_polar into a function
unless you need this level of indirection. But note that you can
always replace symbols with numbers in an expression using subs(), so
strictly speaking this level of indirection should never be needed.
I'm not sure what you had r, p_phi, and p_r defined as, but they need
to be defined as symbols to use them as arguments to lambdify.
lambdify takes a symbolic expression as input and turns it into a
function based on the symbols in the expression (which are specified
by the first argument to lambdify()).
Aaron Meurer
On Mon, Sep 6, 2021 at 6:30 PM Audrius-St <[email protected]> wrote:
>
> Hello,
>
> Is it possible to lambdify the following:
>
> # test code
> mu, m_t, g = sp.symbols('mu, m_t, g')
>
> class h_Kepler_two_body_polar(sp.Function):
> nargs = (1, 2, 3, (4, 5, 6)) # Is this correct?
>
> @classmethod
> def eval(cls, r, p_r, p_phi, *args):
> mu = args[0]
> m_t = args[1]
> g = args[2]
>
> h = p_r**2/(2*mu) + p_phi**2/(2*mu*r**2) - g*mu*m_t/r
> return h
>
> My current attempt to lambdify
>
> H_Ktb_polar_lf = sp.lambdify(
> [r, p_r, p_phi, (mu, m_t, g)],
> h_Kepler_two_body_polar(r, p_r, p_phi, (mu, m_t, g)), 'numpy')
>
> returns the error message
>
> TypeError: h_Kepler_two_body_polar takes at least 1 argument (4 given)
>
> The reason for using *args is that later in the code I use fsolve from SciPy
> with
>
> func: callable f(x, *args) and fprime: callable f_prime(x, *args)
>
> Any insight would be appreciated.
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/98a6147c-a081-4432-8168-d0d6b5139247n%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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAKgW%3D6KhwCWUjqU59TyT-MOb3mzyV6Y4MZhmHrVNf0So-0iY4g%40mail.gmail.com.