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



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.

Reply via email to