Beispiel #1
0
    def _eval_power(b, e):
        """
        b is Real but not equal to rationals, integers, 0.5, oo, -oo, nan
        e is symbolic object but not equal to 0, 1

        (-p) ** r -> exp(r * log(-p)) -> exp(r * (log(p) + I*Pi)) ->
                  -> p ** r * (sin(Pi*r) + cos(Pi*r) * I)
        """
        if isinstance(e, Number):
            if isinstance(e, Integer):
                e = e.p
                return Real(decimal_math.pow(b.num, e))

            e2 = e._as_decimal()
            if b.is_negative and not e.is_integer:
                m = decimal_math.pow(-b.num, e2)
                a = decimal_math.pi() * e2
                s = m * decimal_math.sin(a)
                c = m * decimal_math.cos(a)
                return Real(s) + Real(c) * S.ImaginaryUnit
            return Real(decimal_math.pow(b.num, e2))
        return
Beispiel #2
0
 def _eval_power(b, e):
     if e.is_odd: return S.NegativeOne
     if e.is_even: return S.One
     if isinstance(e, Number):
         if isinstance(e, Real):
             a = e.num * decimal_math.pi()
             s = decimal_math.sin(a)
             c = decimal_math.cos(a)
             return Real(s) + Real(c) * S.ImaginaryUnit
         if isinstance(e, NaN):
             return S.NaN
         if isinstance(e, (Infinity, NegativeInfinity)):
             return S.NaN
         if isinstance(e, Half):
             return S.ImaginaryUnit
         if isinstance(e, Rational):
             if e.q == 2:
                 return S.ImaginaryUnit ** Integer(e.p)
             q = int(e)
             if q:
                 q = Integer(q)
                 return b ** q * b ** (e - q)
     return
Beispiel #3
0
    t.lexer.skip(1)
    
# Build the lexer
lex.lex()

# Parsing rules

precedence = (
    ('left','PLUS','MINUS'),
    ('left','TIMES','DIVIDE'),
    ('right','UMINUS'),
    ('right','RAISE_TO'),
    )


names = {'e': exp(Decimal(1)), 'pi': pi(), '_last': Decimal(0)}
reserved_names = ['e','pi','_last']
def p_statement_assign(t):
    'statement : NAME EQUALS expression'
    if t[1] in reserved_names:
        print "Name %s reserved" % t[1]
    else: 
        names[t[1]] = t[3]

def p_set_last_to(t):
    'statement : SET_TO NAME'
    if t[2] in reserved_names:
        print "Name %s reserved" % t[2]
    else:
        names[t[2]] = names['_last'] 
Beispiel #4
0
 def _eval_evalf(self):
     return Real(decimal_math.pi())
Beispiel #5
0
    t.lexer.skip(1)


# Build the lexer
lex.lex()

# Parsing rules

precedence = (
    ('left', 'PLUS', 'MINUS'),
    ('left', 'TIMES', 'DIVIDE'),
    ('right', 'UMINUS'),
    ('right', 'RAISE_TO'),
)

names = {'e': exp(Decimal(1)), 'pi': pi(), '_last': Decimal(0)}
reserved_names = ['e', 'pi', '_last']


def p_statement_assign(t):
    'statement : NAME EQUALS expression'
    if t[1] in reserved_names:
        print "Name %s reserved" % t[1]
    else:
        names[t[1]] = t[3]


def p_set_last_to(t):
    'statement : SET_TO NAME'
    if t[2] in reserved_names:
        print "Name %s reserved" % t[2]