Ejemplo n.º 1
0
    def canonicalize(cls, args):
        use_glib = 0
        if use_glib:
            from csympy import HashTable

            d = HashTable()
        else:
            d = {}
        num = Integer(1)
        for a in args:
            if a.type == INTEGER:
                num *= a
            elif a.type == MUL:
                for b in a.args:
                    if b.type == INTEGER:
                        num *= b
                    else:
                        key, coeff = b.as_base_exp()
                        if key in d:
                            d[key] += coeff
                        else:
                            d[key] = coeff
            else:
                key, coeff = a.as_base_exp()
                if key in d:
                    d[key] += coeff
                else:
                    d[key] = coeff
        if num.i == 0 or len(d) == 0:
            return num
        args = []
        for a, b in d.iteritems():
            args.append(Pow((a, b)))
        if num.i != 1:
            args.insert(0, num)
        if len(args) == 1:
            return args[0]
        else:
            return Mul(args, False)
Ejemplo n.º 2
0
    def canonicalize(cls, args):
        use_glib = 0
        if use_glib:
            from csympy import HashTable

            d = HashTable()
        else:
            d = {}
        num = Integer(0)
        for a in args:
            if a.type == INTEGER:
                num += a
            elif a.type == ADD:
                for b in a.args:
                    if b.type == INTEGER:
                        num += b
                    else:
                        coeff, key = b.as_coeff_rest()
                        if key in d:
                            d[key] += coeff
                        else:
                            d[key] = coeff
            else:
                coeff, key = a.as_coeff_rest()
                if key in d:
                    d[key] += coeff
                else:
                    d[key] = coeff
        if len(d) == 0:
            return num
        args = []
        for a, b in d.iteritems():
            args.append(Mul((a, b)))
        if num.i != 0:
            args.insert(0, num)
        if len(args) == 1:
            return args[0]
        else:
            return Add(args, False)