On Tuesday, September 7, 2021 at 2:12:16 PM UTC-4 [email protected] wrote:
> You seem to be quite a bit confused here.
Yes, I may be.
>
> 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.
Understood.
>
> 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.
>
Understood.
Old OO habits.
Now
mu, m_t, g = sp.symbols('mu, m_t, g')
def h_Kepler_two_body_polar(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
. . .
# Lambdify the symbolic Hamiltonian to numeric
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')
. . .
# Energy - Hamiltonian first integral
Ho = H_Ktb_polar_lf(R_o, Pr_o, Phi_o, (Mu, M_t, G))
works.
Was unfamiliar with the use of *args.
> 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()).
Defined, not shown for brevity.
Thanks for your reply and explanations.
--
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/b5501cf4-2bb0-4533-b588-7ceddd33e826n%40googlegroups.com.