Force SymPy to keep the order of terms

前端 未结 3 898
南笙
南笙 2020-12-20 15:15

I have the following code:

from sympy import *
init_printing()

x,y = symbols(\'x y\')
u = Function(\'u\')(x,y)
ux,uy,uxx,uxy,uyy = symbols(\"u_x u_y u_xx u_         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 15:32

    try read this http://docs.sympy.org/0.7.2/modules/utilities/misc.html, may be could help you

    Note:

    The key returned is useful for getting items into a canonical order that will be the same across platforms. It is not directly useful for sorting lists of expressions:

    >>> a, b = x, 1/x
    

    Since a has only 1 term, its value of sort_key is unaffected by order:

    >>> a.sort_key() == a.sort_key('rev-lex')
    True
    

    If a and b are combined then the key will differ because there are terms that can be ordered:

    >>> eq = a + b
    >>> eq.sort_key() == eq.sort_key('rev-lex')
    False
    >>> eq.as_ordered_terms()
    [x, 1/x]
    >>> eq.as_ordered_terms('rev-lex')
    [1/x, x]
    

    But since the keys for each of these terms are independent of order‘s value, they don’t sort differently when they appear separately in a list:

    >>> sorted(eq.args, key=default_sort_key)
    [1/x, x]
    >>> sorted(eq.args, key=lambda i: default_sort_key(i, order='rev-lex'))
    [1/x, x]
    

    The order of terms obtained when using these keys is the order that would be obtained if those terms were factors in a product.

提交回复
热议问题