I am using my own codegeneration tool. Basically, I take the output of cse and convert it all to strings and save it to a file. My stuff can be fixed with
from __future__ import division in the module. So, I guess it isn't critical. I don't want the module that gets generated to depend on sympy, so Integer(3)/Integer(2) doesn't solve my problem. I was just thinking that this might bite someone else. Is there an existing function to take a sympy expression (like those returned by cse) and generate numpy compatible Python code that doesn't depend on sympy? This may be a small wheel reinvention. Here is what I am doing (obviousl a naive approach): def cse_tuples_to_txtlist(tuplelist, ws=" "*4): """Take a list of tuples returned as the first output from sympy.cse and convert it to a txtlist of valid Python code.""" mylist = None for var, expr in tuplelist: curline = ws + '%s = %s' % (var, expr) if mylist is None: mylist = [curline] else: mylist.append(curline) return mylist def list_to_array_str(listin, outlabel, ws=" "*4): """Assume that the list contains elements of a square matrix and return valid Python code to convert the list back to an array. Do this using numpy.reshape.""" T = numpy.array(listin) N = T.shape[0] n = numpy.sqrt(N) mat = numpy.reshape(T, (n,n)) str_mat = None for row in mat: row_list = None for ent in row: ent_str = str(ent) if row_list is None: row_list = [ent_str] else: row_list.append(ent_str) row_str = '[' + ', '.join(row_list)+']' if str_mat is None: str_mat = [row_str] else: str_mat.append(row_str) n1 = len(outlabel) n2 = len(' = array([') ws2 = ws + " "*(n1+n2) mat_str = 'array([' + (', \\\n'+ws2).join(str_mat)+'])' return mat_str def cse_to_txtlist(expr, outlabel, ws=" "*4): tuplist, out = cse(expr) mylist = cse_tuples_to_txtlist(tuplist) if len(out) > 1: out_str = list_to_array_str(out, outlabel, ws=ws) else: out_str = str(out[0]) last_line = ws + outlabel + ' = ' + out_str return_line = ws + 'return ' + outlabel mylist.append(last_line) mylist.append(return_line) return mylist def cse_to_file(expr, filename, outlabel, funcname, inputs=[], \ ws=' '*4): line1 = 'def '+funcname +'(' + ', '.join(inputs) + '):' mylist = [line1] mylist.extend(cse_to_txtlist(expr, outlabel, ws=ws)) txt_mixin.dump(filename, mylist) FWIW, Ryan On Tue, Sep 22, 2009 at 3:07 PM, Ondrej Certik <ond...@certik.cz> wrote: > > On Tue, Sep 22, 2009 at 1:07 PM, Ondrej Certik <ond...@certik.cz> wrote: > > On Tue, Sep 22, 2009 at 12:51 PM, Ryan Krauss <ryanli...@gmail.com> > wrote: > >> I just lost a few hours of my day to a problem that turn out to be cse > >> returning things like > >> > >> expr**(3/2) > >> > >> I wrote some code to take the cse output and autogenerate some Python > code I > >> was using with numpy and numerical analysis. How difficult would it be > to > >> add an option to cse to make it output expr**(3.0/2)? > > > > 3/2 is Integer(3)/Integer(2) > > > > so just fix your autogeneration tool. Or are you using some sympy > > autogeneration functions? If so, which? Let's fix those. > > autogeneration -> codegeneration > > O. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---