def test_apply_infinite_stop(): expr = Sub(Sub('0', 'x'), '1') rules = [Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1)))), Rule(Add(any(0), any(1)), Add(any(1), any(0)))] rearranged = apply_rules(expr, rules) rules = [Rule('x', '2')] result = apply_rules(rearranged, rules) assert(result == '-3')
def test_apply_infinite_stop(): expr = Sub(Sub('0', 'x'), '1') rules = [ Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1)))), Rule(Add(any(0), any(1)), Add(any(1), any(0))) ] rearranged = apply_rules(expr, rules) rules = [Rule('x', '2')] result = apply_rules(rearranged, rules) assert (result == '-3')
def test_apply(): expr = Sub(Sub('0', 'x'), '1') rules = [Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1)))), Rule(Add(any(0), any(1)), Add(any(1), any(0))), Rule('x', '2')] result = apply_rules(expr, rules) assert(result == '-3')
def test_apply(): expr = Sub(Sub('0', 'x'), '1') rules = [ Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1)))), Rule(Add(any(0), any(1)), Add(any(1), any(0))), Rule('x', '2') ] result = apply_rules(expr, rules) assert (result == '-3')
def extend_playlist(self, *rules) : """Create a playlist by applying a set of RULES. RULES is a list of callables. The first of the RULES is given the unused tracks in the library. Each succesive rule's input is the output from the previous rule. The output of the last rule is the returned by this function. Also, all the tracks in the return value are removed from the list of available tracks.""" t = apply_rules(self.tracks, *rules) self.playlist.extend(t) for s in t : self.tracks.remove(s)
# P_{n+1} = alpha_{n} * x * P_n - beta_{n} * P_{n - 1} legendre_recursion = lambda n: Rule( LegendreP(Add(n, '1'), any(0)), Add(Mul(Mul(Alpha(n), any(0)), LegendreP(n, any(0))), Neg(Mul(Beta(n), LegendreP(Sub(n, '1'), any(0)))))) # (1 / alpha_{n}) * P_{n+1} + (beta_n / alpha_n) * P_{n - 1} = x * P_n legendre_inv_recursion = \ lambda n: Rule(Mul(any(0), LegendreP(n, any(0))), Add(Mul(Div('1', Alpha(n)), LegendreP(Add(n, '1'), any(0))), Mul(Div(Beta(n), Alpha(n)), LegendreP(Sub(n, '1'), any(0))))) nice_form = lambda n: Rule(Mul(Mul(any(0), any(1)), LegendreP(n, any(1))), Mul(any(0), Mul(any(1), LegendreP(n, any(1))))) expr = LegendreP(Add('n', '1'), 'x') recursed = apply_rules(expr, [legendre_recursion('n')]) print recursed inv_recursed = apply_rules( recursed, [legendre_inv_recursion('n'), nice_form('n')]) print inv_recursed inv_recursed_simplified = apply_rules(recursed, algebra_rules) print inv_recursed_simplified # # Integral from neg 1 to 1 # IntM11 = create_op('intm11', lambda x: 2 * x) # int_leg_0 = Rule(IntM11(LegendreP('0', any(0)), any(0)), '2') # int_leg_gt_0 = Rule(IntM11(LegendreP(any(0), any(1)), any(1)), '0') # # legendre_rules = [int_leg_0, int_leg_gt_0] #
def test_apply_rules(): expr = Sub(Sub('0', 'x'), '1') rules = [Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1))))] result = apply_rules(expr, rules) assert(result == Add(Add('0', Neg('x')), '-1'))
Beta = create_op('beta', None) # P_{n+1} = alpha_{n} * P_n - beta_{n} * P_{n - 1} n = 'n' n_plus_1 = Add(n, '1') n_minus_1 = Sub(n, '1') x = any(1) term1 = Mul(Alpha(n), LegendreP(n, x)) term2 = Neg(Mul(Beta(n_minus_1), LegendreP(n_minus_1, x))) rhs = Add(term1, term2) lhs = LegendreP(n_plus_1, any(1)) legendre_recursion = Rule(lhs, rhs) expr = LegendreP(n_plus_1, 'y') print apply_rules(expr, [legendre_recursion]) # Multiplicative identity mult_ident = Rule(Mul(any(0), '1'), any(0)) mult_ident2 = Rule(Mul('1', any(0)), any(0)) # Addition to Multiplication add_to_mult = Rule(Add(any(0), any(0)), Mul('2', any(0))) # d/dx (x) = 1 D = create_op('derivative', lambda x: 0) expr = D('x', 'x') self_deriv = Rule(D(any(0), any(0)), '1') print apply_rules(expr, [self_deriv]) Inde = create_op('independent', None)
# P_{n+1} = alpha_{n} * x * P_n - beta_{n} * P_{n - 1} legendre_recursion = lambda n: Rule(LegendreP(Add(n, '1'), any(0)), Add(Mul(Mul(Alpha(n), any(0)), LegendreP(n, any(0))), Neg(Mul(Beta(n), LegendreP(Sub(n, '1'), any(0)))))) # (1 / alpha_{n}) * P_{n+1} + (beta_n / alpha_n) * P_{n - 1} = x * P_n legendre_inv_recursion = \ lambda n: Rule(Mul(any(0), LegendreP(n, any(0))), Add(Mul(Div('1', Alpha(n)), LegendreP(Add(n, '1'), any(0))), Mul(Div(Beta(n), Alpha(n)), LegendreP(Sub(n, '1'), any(0))))) nice_form = lambda n: Rule(Mul(Mul(any(0), any(1)), LegendreP(n, any(1))), Mul(any(0), Mul(any(1), LegendreP(n, any(1))))) expr = LegendreP(Add('n', '1'), 'x') recursed = apply_rules(expr, [legendre_recursion('n')]) print recursed inv_recursed = apply_rules(recursed, [legendre_inv_recursion('n'), nice_form('n')]) print inv_recursed inv_recursed_simplified = apply_rules(recursed, algebra_rules) print inv_recursed_simplified # # Integral from neg 1 to 1 # IntM11 = create_op('intm11', lambda x: 2 * x) # int_leg_0 = Rule(IntM11(LegendreP('0', any(0)), any(0)), '2') # int_leg_gt_0 = Rule(IntM11(LegendreP(any(0), any(1)), any(1)), '0') # # legendre_rules = [int_leg_0, int_leg_gt_0] # # print apply_rules(IntM11(LegendreP('n', 'x'), 'x'), legendre_rules)
def test_apply_rules(): expr = Sub(Sub('0', 'x'), '1') rules = [Rule(Sub(any(0), any(1)), Add(any(0), Neg(any(1))))] result = apply_rules(expr, rules) assert (result == Add(Add('0', Neg('x')), '-1'))
Beta = create_op('beta', None) # P_{n+1} = alpha_{n} * P_n - beta_{n} * P_{n - 1} n = 'n' n_plus_1 = Add(n, '1') n_minus_1 = Sub(n, '1') x = any(1) term1 = Mul(Alpha(n), LegendreP(n, x)) term2 = Neg(Mul(Beta(n_minus_1), LegendreP(n_minus_1, x))) rhs = Add(term1, term2) lhs = LegendreP(n_plus_1, any(1)) legendre_recursion = Rule(lhs, rhs) expr = LegendreP(n_plus_1, 'y') print apply_rules(expr, [legendre_recursion]) # Multiplicative identity mult_ident = Rule(Mul(any(0), '1'), any(0)) mult_ident2 = Rule(Mul('1', any(0)), any(0)) # Addition to Multiplication add_to_mult = Rule(Add(any(0), any(0)), Mul('2', any(0))) # d/dx (x) = 1 D = create_op('derivative', lambda x: 0) expr = D('x', 'x') self_deriv = Rule(D(any(0), any(0)), '1')