示例#1
0
def main():
    f = open(targetfile_py, 'w')
    print >> f, template
    print >> f, preprocess(
        '''

def fraction_add(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @ADD_FRACTION_INT(LHS=self; RHS=other)
    elif t is cls:
        @ADD_FRACTION_FRACTION(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self + mpf(other)
    return NotImplemented

def fraction_sub(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @SUB_FRACTION_INT(LHS=self; RHS=other)
    elif t is cls:
        @SUB_FRACTION_FRACTION(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self - mpf(other)
    return NotImplemented

def fraction_rsub(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @SUB_INT_FRACTION(RHS=self; LHS=other)
    @IF_CHECK_FLOAT(T=t)
        return mpf(other) - self
    return NotImplemented

def fraction_mul(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    if t is cls:
        @MUL_FRACTION_FRACTION(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_INT(T=t)
        @MUL_FRACTION_INT(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return self * mpf(other)
    return NotImplemented

def fraction_div(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @DIV_FRACTION_INT(LHS=self; RHS=other; MOD=%)
    elif t is cls:
        @DIV_FRACTION_FRACTION(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return self / mpf(other)
    return NotImplemented

def fraction_rdiv(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @DIV_INT_FRACTION(RHS=self; LHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return mpf(other) / self
    return NotImplemented

def fraction_pow(self, other, m=None, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        if not other:
            return 1
        if other==1:
            return self
        @POW_FRACTION_INT(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self ** mpf(other)
    return NotImplemented

def complex_add(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @ADD_COMPLEX_REAL(LHS=self; RHS=other)
    @IF_CHECK_COMPLEX(T=t)
        @ADD_COMPLEX_COMPLEX(LHS=self; RHS=other)
    return NotImplemented

def complex_sub(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @SUB_COMPLEX_REAL(LHS=self; RHS=other)
    @IF_CHECK_COMPLEX(T=t)
        @SUB_COMPLEX_COMPLEX(LHS=self; RHS=other)
    return NotImplemented

def complex_rsub(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @SUB_REAL_COMPLEX(LHS=other; RHS=self)
    if t is complex:
        @SUB_COMPLEX_COMPLEX(LHS=other; RHS=self)
    return NotImplemented

def complex_mul(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_COMPLEX(T=t)
        @MUL_COMPLEX_COMPLEX(LHS=self; RHS=other)
    @IF_CHECK_REAL(T=t)
        @MUL_COMPLEX_REAL(LHS=self; RHS=other)
    return NotImplemented

def complex_div(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @DIV_COMPLEX_REAL(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_COMPLEX(T=t)
        @DIV_COMPLEX_COMPLEX(LHS=self; RHS=other; MOD=%)
    return NotImplemented

def complex_rdiv(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @DIV_REAL_COMPLEX(LHS=other; RHS=self; MOD=%)
    if t is complex:
        @DIV_COMPLEX_COMPLEX(LHS=other; RHS=self; MOD=%)
    return NotImplemented

def complex_pow(self, other, m=None, new=object.__new__, cls=mpqc, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        if not other:
            return 1
        if other==1:
            return self
        if other==2:
            return self*self
        if other < 0:
            base, exponent = 1/self, -other
        else:
            base, exponent = self, other
        @POW_COMPLEX_INT(LHS=base; RHS=exponent; MOD=%)
    return NotImplemented
    ''', globals())

    for (n, s) in [('lt', '<'), ('le', '<='), ('gt', '>'), ('ge', '>=')]:
        print >> f, preprocess(
            '''\
def fraction_%s(self, other, cls=mpq, inttypes_set=inttypes_set):
    p, q = self
    t = type(other)
    @IF_CHECK_INT(T=t)
        return (p %s q*other)
    elif t is cls:
        r, s = other
        return p*s %s q*r
    return NotImplemented
    ''' % (n, s, s), globals())

    f.close()
示例#2
0
def main():
    f = open(targetfile_py, 'w')
    print >> f, template
    print >> f, preprocess('''

def return_terms(cls, pairs):
    if not pairs:
        return cls.zero
    if len(pairs)==1:
        t, c = pairs.items()[0]
        if c==1:
            return t
        if t==cls.one:
            return cls(NUMBER, c)
    @RETURN_NEW(HEAD=TERMS; DATA=pairs)

def return_factors(cls, pairs):
    if not pairs:
        return cls.one
    elif len(pairs)==1:
        t, c = pairs.items()[0]
        if c==1:
            return t
        if t==cls.one:
            return t
    @RETURN_NEW(HEAD=FACTORS; DATA=pairs)

def inplace_add(cls, obj, pairs, pairs_get, one):
    tobj = type(obj)
    if tobj is cls:
        head, data = obj.pair
        if head is NUMBER:
            if data:
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=data; SIGN=+; USIGN=)
        elif head is TERMS:
            for t,c in data.iteritems():
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=t; VALUE=c; SIGN=+; USIGN=)
        else:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=obj; VALUE=1; SIGN=+; USIGN=)
    @ELIF_CHECK_NUMBER(T=tobj)
        if obj:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=obj; SIGN=+; USIGN=)
    else:
        inplace_add(cls, cls.convert(obj), pairs, pairs_get, one)

def inplace_add2(cls, obj, coeff, pairs, pairs_get, one):
    if not coeff:
        return
    tobj = type(obj)
    if tobj is cls:
        head, data = obj.pair
        if head is NUMBER:
            value = coeff * data
            if value:
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=value; SIGN=+; USIGN=)
        elif head is TERMS:
            for t,c in data.iteritems():
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=t; VALUE=coeff*c; SIGN=+; USIGN=)
        else:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=obj; VALUE=coeff; SIGN=+; USIGN=)
    @ELIF_CHECK_NUMBER(T=tobj)
        value = coeff * obj
        if value:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=value; SIGN=+; USIGN=)
    else:
        inplace_add2(cls, cls.convert(obj), coeff, pairs, pairs_get, one)

def inplace_sub(cls, obj, pairs, pairs_get, one):
    tobj = type(obj)
    if tobj is cls:
        head, data = obj.pair
        if head is NUMBER:
            value = data
            if value:
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=value; SIGN=-; USIGN=-)
        elif head is TERMS:
            for t,c in data.iteritems():
                @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=t; VALUE=c; SIGN=-; USIGN=-)
        else:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=obj; VALUE=1; SIGN=-; USIGN=-)
    @ELIF_CHECK_NUMBER(T=tobj)
        if obj:
            @ADD_TERM_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; TERM=one; VALUE=obj; SIGN=-; USIGN=-)
    else:
        inplace_add(cls, cls.convert(obj), pairs, pairs_get, one)

def inplace_mul(cls, obj, pairs, pairs_get, try_power=try_power, NUMBER=NUMBER):
    tobj = type(obj)
    if tobj is cls:
        head, data = obj.pair
        if head is NUMBER:
            return data
        elif head is TERMS:
            if len(data)==1:
                t, number = data.items()[0]
                @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=t; VALUE=1; SIGN=+; USIGN=; NUMBER=number)
                return number
            number = 1
            @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=obj; VALUE=1; SIGN=+; USIGN=; NUMBER=number)
            return number
        elif head is FACTORS:
            number = 1
            for t, c in data.iteritems():
                @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=t; VALUE=c; SIGN=+; USIGN=; NUMBER=number)
            return number
        else:
            number = 1
            @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=obj; VALUE=1; SIGN=+; USIGN=; NUMBER=number)
            return number
    @ELIF_CHECK_NUMBER(T=tobj)
        return obj
    else:
        return inplace_mul(cls, cls.convert(obj), pairs, pairs_get)

def inplace_mul2(cls, obj, exp, pairs, pairs_get, try_power=try_power, NUMBER=NUMBER):
    if not exp:
        return cls.one
    tobj = type(obj)
    texp = type(exp)
    if tobj is cls:
        head, data = obj.pair
        if head is NUMBER:
            if texp in inttypes_set and exp < 0:
                return div(1, data ** -exp, cls)
            return data ** exp
        elif head is TERMS:
            if len(data)==1:
                t, number = data.items()[0]
                if  texp in inttypes_set and exp < 0:
                    number = div(1, number ** -exp, cls)
                else:
                    number = number ** exp
                @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=t; VALUE=exp; SIGN=+; USIGN=; NUMBER=number)
                return number
            number = 1
            @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=obj; VALUE=exp; SIGN=+; USIGN=; NUMBER=number)
            return number
        elif head is FACTORS:
            number = 1
            for t, c in data.iteritems():
                @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=t; VALUE=c*exp; SIGN=+; USIGN=; NUMBER=number)
            return number
        else:
            number = 1
            @MUL_FACTOR_VALUE_DICT(DICT=pairs; DICT_GET=pairs_get; FACTOR=obj; VALUE=exp; SIGN=+; USIGN=; NUMBER=number)
            return number
    @ELIF_CHECK_NUMBER(T=tobj)
        if  texp in inttypes_set and exp < 0:
            return div(1, obj ** -exp, cls)
        return obj ** exp
    else:
        return inplace_mul2(cls, cls.convert(obj), exp, pairs, pairs_get)

    ''', globals())
示例#3
0
def main():
    f = open(targetfile_py, 'w')
    print >> f, template
    print >> f, preprocess('''

def fraction_add(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @ADD_FRACTION_INT(LHS=self; RHS=other)
    elif t is cls:
        @ADD_FRACTION_FRACTION(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self + mpf(other)
    return NotImplemented

def fraction_sub(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @SUB_FRACTION_INT(LHS=self; RHS=other)
    elif t is cls:
        @SUB_FRACTION_FRACTION(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self - mpf(other)
    return NotImplemented

def fraction_rsub(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @SUB_INT_FRACTION(RHS=self; LHS=other)
    @IF_CHECK_FLOAT(T=t)
        return mpf(other) - self
    return NotImplemented

def fraction_mul(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    if t is cls:
        @MUL_FRACTION_FRACTION(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_INT(T=t)
        @MUL_FRACTION_INT(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return self * mpf(other)
    return NotImplemented

def fraction_div(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @DIV_FRACTION_INT(LHS=self; RHS=other; MOD=%)
    elif t is cls:
        @DIV_FRACTION_FRACTION(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return self / mpf(other)
    return NotImplemented

def fraction_rdiv(self, other, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        @DIV_INT_FRACTION(RHS=self; LHS=other; MOD=%)
    @IF_CHECK_FLOAT(T=t)
        return mpf(other) / self
    return NotImplemented

def fraction_pow(self, other, m=None, cls=mpq, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        if not other:
            return 1
        if other==1:
            return self
        @POW_FRACTION_INT(LHS=self; RHS=other)
    @IF_CHECK_FLOAT(T=t)
        return self ** mpf(other)
    return NotImplemented

def complex_add(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @ADD_COMPLEX_REAL(LHS=self; RHS=other)
    @IF_CHECK_COMPLEX(T=t)
        @ADD_COMPLEX_COMPLEX(LHS=self; RHS=other)
    return NotImplemented

def complex_sub(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @SUB_COMPLEX_REAL(LHS=self; RHS=other)
    @IF_CHECK_COMPLEX(T=t)
        @SUB_COMPLEX_COMPLEX(LHS=self; RHS=other)
    return NotImplemented

def complex_rsub(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @SUB_REAL_COMPLEX(LHS=other; RHS=self)
    if t is complex:
        @SUB_COMPLEX_COMPLEX(LHS=other; RHS=self)
    return NotImplemented

def complex_mul(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_COMPLEX(T=t)
        @MUL_COMPLEX_COMPLEX(LHS=self; RHS=other)
    @IF_CHECK_REAL(T=t)
        @MUL_COMPLEX_REAL(LHS=self; RHS=other)
    return NotImplemented

def complex_div(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set, complextypes_set=complextypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @DIV_COMPLEX_REAL(LHS=self; RHS=other; MOD=%)
    @IF_CHECK_COMPLEX(T=t)
        @DIV_COMPLEX_COMPLEX(LHS=self; RHS=other; MOD=%)
    return NotImplemented

def complex_rdiv(self, other, new=object.__new__, cls=mpqc, realtypes_set=realtypes_set):
    t = type(other)
    @IF_CHECK_REAL(T=t)
        @DIV_REAL_COMPLEX(LHS=other; RHS=self; MOD=%)
    if t is complex:
        @DIV_COMPLEX_COMPLEX(LHS=other; RHS=self; MOD=%)
    return NotImplemented

def complex_pow(self, other, m=None, new=object.__new__, cls=mpqc, inttypes_set=inttypes_set):
    t = type(other)
    @IF_CHECK_INT(T=t)
        if not other:
            return 1
        if other==1:
            return self
        if other==2:
            return self*self
        if other < 0:
            base, exponent = 1/self, -other
        else:
            base, exponent = self, other
        @POW_COMPLEX_INT(LHS=base; RHS=exponent; MOD=%)
    return NotImplemented
    ''', globals())

    for (n,s) in [('lt','<'), ('le','<='), ('gt','>'), ('ge','>=')]:
        print >> f, preprocess('''\
def fraction_%s(self, other, cls=mpq, inttypes_set=inttypes_set):
    p, q = self
    t = type(other)
    @IF_CHECK_INT(T=t)
        return (p %s q*other)
    elif t is cls:
        r, s = other
        return p*s %s q*r
    return NotImplemented
    ''' % (n, s, s), globals())

    f.close()
示例#4
0
def main():
    f = open(targetfile_py, 'w')
    print >> f, template
    print >> f, preprocess('''

def expand_mul_method(cls, self, other):
    lhead, ldata = self.pair
    rhead, rdata = other.pair
    if lhead is FACTORS:
        if rhead is FACTORS:
            @MUL_FACTORS_FACTORS(LHS=self; LHSDATA=ldata; RHS=other; RHSDATA=rdata)
        elif rhead is NUMBER:
            return self # other must be one
        else:
            @MUL_FACTORS_SYMBOL(LHS=self; LHSDATA=ldata; RHS=other; RHSDATA=rdata)
    elif lhead is NUMBER:
        return other # self must be one
    else:
        if rhead is FACTORS:
            @MUL_SYMBOL_FACTORS(LHS=self; LHSDATA=ldata; RHS=other; RHSDATA=rdata)
        elif rhead is NUMBER:
            return self # other must be one
        else:
            @MUL_SYMBOL_SYMBOL(LHS=self; LHSDATA=ldata; RHS=other; RHSDATA=rdata)

#def rsub_method(self, other, NUMBER=NUMBER, TERMS=TERMS, FACTORS=FACTORS):
#    cls = type(self)
#    lhead, ldata = self.pair
#    if isinstance(other, cls.coefftypes):
#        if lhead is NUMBER:
#            @SUB_VALUE_NUMBER(VALUE=other; RHS=self; RHSDATA=ldata)
#        elif lhead is TERMS:
#            @SUB_VALUE_TERMS(VALUE=other; RHS=self; RHSDATA=ldata)
#        else:
#            @SUB_VALUE_SYMBOL(VALUE=other; RHS=self; RHSDATA=ldata)
#    other = cls.convert(other, False)
#    if other is NotImplemented:
#        return other
#    return other - self

def rdiv_method(self, other, NUMBER=NUMBER, TERMS=TERMS, FACTORS=FACTORS):
    cls = type(self)
    lhead, ldata = self.pair
    if isinstance(other, cls.coefftypes):
        if lhead is NUMBER:
            @DIV_VALUE_NUMBER(VALUE=other; RHS=self; RHSDATA=ldata)
        elif lhead is TERMS:
            @DIV_VALUE_TERMS(VALUE=other; RHS=self; RHSDATA=ldata)
        elif lhead is FACTORS:
            @DIV_VALUE_FACTORS(VALUE=other; RHS=self; RHSDATA=ldata)
        else:
            @DIV_VALUE_SYMBOL(VALUE=other; RHS=self; RHSDATA=ldata)
    other = cls.convert(other, False)
    if other is NotImplemented:
        return other
    return other / self

''', globals())

    #print >> f, preprocess(OP3_TEMPLATE % (dict(op='add', OP='ADD')), globals())
    #print >> f, preprocess(OP3_TEMPLATE % (dict(op='sub', OP='SUB')), globals())
    #print >> f, preprocess(OP4_TEMPLATE % (dict(op='mul', OP='MUL')), globals())
    print >> f, preprocess(OP4_TEMPLATE % (dict(op='div', OP='DIV')), globals())

    f.close()