符号表达式的运用与导数计算
1 符号表达式求值
在处理符号表达式时,我们可以通过定义已知函数的字典,让求值器识别特定的函数。首先,我们可以定义一个包含三个命名函数的字典:
import math
_function_bindings = {
"sin": math.sin,
"cos": math.cos,
"ln": math.log
}
然后,创建一个 Apply 类来表示函数应用:
class Apply(Expression):
def __init__(self, function, argument):
self.function = function
self.argument = argument
def evaluate(self, **bindings):
return _function_bindings[self.function.name](self.argument.evaluate(**bindings))
当所有的求值方法都实现后,我们就可以对表达式进行求值,例如:
>>> f_expression.evaluate(x=5)
-76.71394197305108
这个结果与普通 Python 函
超级会员免费看
订阅专栏 解锁全文
14

被折叠的 条评论
为什么被折叠?



