Thanks. I think I understand what this code does. How do you want it patched? Am I just writing my own alternative to sympy.latex? Are mainvar and descending keyword arguments to sympy.latex or some other printing function? I assume not everyone wants their polynomials in order of descending powers or you guys would have already done this.
Ryan On Mon, Jun 1, 2009 at 10:49 PM, Ondrej Certik <ond...@certik.cz> wrote: > > On Mon, Jun 1, 2009 at 9:33 PM, Ryan Krauss <ryanli...@gmail.com> wrote: > > So, I think I have a good test and a better understanding of the problem: > > > > mylist = ['m', 's', 'b', 'x', 'y', 's', 'x', 'y', 's', 'EI'] > > sympy.var(mylist) > > test = m*s**2+b*x*y*s+x**2+y**2+s+EI > > args = list(test.args) > > args.sort(sympy.Basic._compare_pretty) > > args.reverse() > > > > > > This doesn't get my list in the right order because of the number of > > variables or whatever _compare_pretty does. > > > > It seems like I need to make a dictionary or something that uses the > powers > > of my main variable as the keys (this could cause issues if there is more > > than one arg with the same power). What is the best way to sort based on > > powers of the main variable? Is this the right approach: > > args[1].as_coeff_exponent('s') > > > > (returns (y**2, 0) in this case) stepping through args in a for loop. > > I would just do it as I suggested above, e.g. like in > sympy/core/basic.py:Basic._compare_pretty: > > @staticmethod > def _compare_pretty(a, b): > from sympy.series.order import Order > if isinstance(a, Order) and not isinstance(b, Order): > return 1 > if not isinstance(a, Order) and isinstance(b, Order): > return -1 > > # FIXME this produces wrong ordering for 1 and 0 > # e.g. the ordering will be 1 0 2 3 4 ... > # because 1 = x^0, but 0 2 3 4 ... = x^1 > p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3") > r_a = a.match(p1 * p2**p3) > r_b = b.match(p1 * p2**p3) > if r_a is not None and r_b is not None: > c = Basic.compare(r_a[p3], r_b[p3]) > if c!=0: > return c > > return Basic.compare(a,b) > > > So for example this code: > > from sympy import Wild, var, Basic > > mylist = ['m', 's', 'b', 'x', 'y', 's', 'x', 'y', 's', 'EI'] > var(mylist) > test = m*s**2+b*x*y*s+x**2+y**2+s+EI > args = list(test.args) > > def my_compare(a, b): > main_var = s > p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3") > r_a = a.match(p1 * s**p3) > r_b = b.match(p1 * s**p3) > if r_a is not None and r_b is not None: > c = Basic.compare(r_a[p3], r_b[p3]) > if c!=0: > return c > > return Basic._compare_pretty(a,b) > > print args > args.sort(my_compare) > print args > > > produces: > > [m*s**2, b*s*x*y, y**2, s, EI, x**2] > [EI, x**2, y**2, s, b*s*x*y, m*s**2] > > > This should fix your problem. Please send a patch. :) > > Ondrej > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---