Two-line function to make expressions callable.

29 views
Skip to first unread message

Andre Bolle

unread,
Apr 17, 2022, 5:33:37 AM4/17/22
to sympy
A function a fund quite useful, so I thought I'd pass it on.

def mkfun(expr):
    """ Creates a lambda with arguments which are the alphabetically ordered free symbols.
    E.g.
    f1 = mkfun(x**2)
    print(f1(3))
    f2 = mkfun(x**2 + y**2)
    print(f2(3,4))
    f3 = mkfun(x**2 + y**2 + z**2)
    print(f3(3,4,5))
    """
    free = list(ordered(expr.free_symbols))
    return lambda *x: expr.subs(zip(free,x))
Message has been deleted

Andre Bolle

unread,
Apr 18, 2022, 6:12:31 AM4/18/22
to sympy
Sorry about the spelling. I meant to write "A function which I find quite useful."

def mkfun(expr):
    free = list(ordered(expr.free_symbols))
    return lambda *x: expr.subs(zip(free,x))

:o)

emanuel.c...@gmail.com

unread,
Apr 19, 2022, 3:41:11 AM4/19/22
to sympy

Try :

In [1]: from sympy import Lambda

In [2]: help(Lambda)

HTH,

Reply all
Reply to author
Forward
0 new messages