On Dec 2, 2012, at 10:21 AM, Freddie Witherden <fred...@witherden.org> wrote:

> On 02/12/12 16:07, Stefan Krastanov wrote:
>> I do not think that the sympy's lambdify function is a good fit here.
>> It is mainly used for translating sympy expressions to something
>> faster but not as precise (python math or numpy). It is strange to use
>> it to translate something from sympy back to sympy, especially given
>> that `lambdify` is very buggy.
>>
>> Why don't you use just the normal python lambda:
>>
>> lambda arg: high_precision_expression*arg
>
> The example of a single constant times an argument was just a minimal
> test case.  Nothing more.  My actual expressions are rather large
> polynomials (products of various Lagrange polynomials).  Hence I never
> have symbols/variables corresponding to the various constants in the
> polynomial.
>
> The performance improvement in my application from having lambdified
> expressions vs .subs() is an order of magnitude.  (Even when using mpf
> variables for constants.)  Hence, it is worth jumping through a hoop or
> two to get there.
>
> The following function:
>
> def lambdify_polys(dims, polys):
>    ls = [lambdastr(dims, p) for p in polys]
>    csf = {}
>
>    for l in ls:
>        for m in re.findall('([0-9]*\.[0-9]+(?:[eE][-+]?[0-9]+)?)', l):
>            if m not in csf:
>                csf[m] = mp.mpf(m)
>
>    csn = {s: '__c%d' % i for i,s in enumerate(csf.iterkeys())}
>    cnf = {n: csf[s] for s,n in csn.iteritems()}
>
>    lex = []
>    for l in ls:
>        for s,n in csn.iteritems():
>            l = l.replace(s, n)
>        lex.append(eval(l, cnf))
>
>    return lex
>
> works well by replacing floating point constants with pre-constructed
> mpf constants.
>
> My only issue is that the printer used by lambdastr spits out rational
> constants as "5/3" for example (as opposed to 1.66666...).  If I can
> sort this out then my problem is basically solved.
>
> Regards, Freddie.
>

So just call evalf, or if that does too much, selectively evalf the
rationals by finding them with .atoms(Rational).

Aaron Meurer

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to