def derive_mult(expression, respect_to): # U dv + V du return construct_binary_ast_node( label("+"), construct_binary_ast_node(label("*"), expression[1], derivative.derive(expression[2], respect_to)), construct_binary_ast_node(label("*"), derivative.derive(expression[1], respect_to), expression[2]), )
def derive_mult(expression, respect_to): # U dv + V du return construct_binary_ast_node( label('+'), construct_binary_ast_node(label('*'), expression[1], derivative.derive(expression[2], respect_to)), construct_binary_ast_node(label('*'), derivative.derive(expression[1], respect_to), expression[2]))
def derive_div(expression, respect_to): return construct_binary_ast_node( label("/"), construct_binary_ast_node( label("-"), construct_binary_ast_node(label("*"), derivative.derive(expression[1], respect_to), expression[2]), construct_binary_ast_node(label("*"), expression[1], derivative.derive(expression[2], respect_to)), ), construct_binary_ast_node(label("*"), expression[2], expression[2]), )
def derive_pow(expression, respect_to): # TODO CURRENTLY ASSUMES A CONSTANT IN THE EXPONENT, FIX THIS # TODO WOW THIS METHOD IS BAD, FIX THIS return construct_binary_ast_node( label('*'), expression[2], construct_binary_ast_node( label('*'), construct_binary_ast_node( label('^'), expression[1], (str(float(expression[2][0]) - 1), 'CONSTANT')), derivative.derive(expression[1], respect_to)))
def derive_div(expression, respect_to): return construct_binary_ast_node( label('/'), construct_binary_ast_node( label('-'), construct_binary_ast_node( label('*'), derivative.derive(expression[1], respect_to), expression[2]), construct_binary_ast_node( label('*'), expression[1], derivative.derive(expression[2], respect_to)), ), construct_binary_ast_node(label('*'), expression[2], expression[2]))
def derive_pow(expression, respect_to): # TODO CURRENTLY ASSUMES A CONSTANT IN THE EXPONENT, FIX THIS # TODO WOW THIS METHOD IS BAD, FIX THIS return construct_binary_ast_node( label("*"), expression[2], construct_binary_ast_node( label("*"), construct_binary_ast_node(label("^"), expression[1], (str(float(expression[2][0]) - 1), "CONSTANT")), derivative.derive(expression[1], respect_to), ), )
def derive(expression, respect_to): # TODO Fix access for operator expressions # EX * 5 x token_type = get_token_type(expression[0]) token_value = get_token_value(expression[0]) if token_type in ['CONSTANT']: return label('0') if token_type in ['IDENTIFIER']: # Check if in respect to identifier if token_value == respect_to: return label('1') else: return label('0') if token_type in ['OPERATOR']: # TODO implement this monster by implementing each operator individually # raise Exception('TODO Not Implemented') method = bin_operators_impl[token_value] return method(expression, respect_to) raise Exception('wat')
def test_label_bad(self): expected = ('pear95', 'UNKNOWN') result = label('pear95') self.assertEqual(expected, result)
def test_label_operator_plus(self): expected = ('+', 'OPERATOR') result = label('+') self.assertEqual(expected, result)
def test_label_ident2(self): expected = ('apples', 'IDENTIFIER') result = label('apples') self.assertEqual(expected, result)
def test_label_num2(self): expected = ('67', 'CONSTANT') result = label('67') self.assertEqual(expected, result)