Ah, I see. You want to substitute in monkey_log(whatever) for every instance of log(whatever) in some expression.
@sympy community - Do we have any technology to traverse the AST tree and replace nodes with transformed nodes? SymPy has some basic pattern matching. I've never used it though. Perhaps a combination of it and subs could help here? On Wed, Aug 8, 2012 at 1:39 PM, Skipper Seabold <[email protected]> wrote: > On Wed, Aug 8, 2012 at 2:02 PM, Matthew Rocklin <[email protected]> wrote: >> >> Would Piecewise work well here? >> >> >> # Return 0 if x==0 else return log(x) >> >>> monkey_log = Piecewise((0, Eq(x, 0)), (log(x), True)) >> >>> fn = lambdify(x, monkey_log, 'mpmath') >> >>> fn(0) >> 0 >> >>> fn(2.71828) >> 0.999999327347282 >> > > Maybe I'm being dense, but I'm not sure I understand correctly what to do > with this. How do I inject this into the namespace used for evaluating > lam_func in the below? I want to replace the log function which is part of a > bigger expression. > > Skipper > >> >> >> >> On Wed, Aug 8, 2012 at 11:37 AM, Skipper Seabold <[email protected]> >> wrote: >>> >>> Hi, >>> >>> I'm trying to evaluate a Hessian that I'm computing symbolically. To do >>> so, I'd like to monkey-patch the mpmath.log function to define log(0) == 0. >>> I can replicate the error with the minimal example below. I get >>> >>> /usr/local/lib/python2.7/dist-packages/sympy/core/function.pyc in >>> __new__(cls, *args, **options) >>> 606 args = map(sympify, args) >>> 607 result = super(AppliedUndef, cls).__new__(cls, *args, >>> **options) >>> --> 608 result.nargs = len(args) >>> 609 return result >>> 610 >>> >>> AttributeError: 'Float' object has no attribute 'nargs' >>> >>> >>> However, what I'm seeing with my actual script is that the first time I >>> run it, I get this error. The second time, it works, which is baffling. I am >>> on recent sympy master from git. Anyone have any ideas? Am I doing this >>> wrong? >>> >>> import sympy >>> from sympy.abc import y,x,z >>> from sympy.utilities.lambdify import implemented_function >>> from sympy import Function >>> from mpmath import mpf, log as mplog >>> >>> func_ = y * sympy.log(x) * z >>> func = func_(y, x).subs({z : 2.0}) >>> >>> log = implemented_function(Function("log"), lambda x : mpf(0) if x == 0 >>> else mplog(x)) >>> >>> lam_func = sympy.lambdify((y, x), func, [{'log' : log}, 'mpmath']) >>> >>> data = (1., 2.) >>> lam_func(*data) >>> >>> Thanks, >>> >>> Skipper >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "sympy" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/sympy?hl=en. >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/sympy?hl=en. > > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
