python - 替换数学公式中的点积运算符
问题描述
如何将点积(@)的pythons内部符号替换为numpy的numpy.dot?例如,公式
m.a@x + m.b@y
应转换为np.dot(m.a, x) + np.dot(m.b, y)
.
我最初的想法是使用正则表达式在 @ (m.a
和m.b
上面的示例中)之前和之后查找文本,然后将它们放入 dot 函数中。这是我想象的使用正则表达式的方式:
# m.a, m.b, x and y are vectors of some equal size
formula = "m.a@x + m.b@y"
before_dots, after_dots = some_regex_function(formula)
result = eval(f"np.dot({before_dot[0]},{after_dot[0]}) + np.dot({before_dot[1]},{after_dot[1]})")
解决方案
使用ast
andastor
模块,您可以解析代码并将运算符为矩阵乘法的所有二元运算节点替换为np.dot
调用。
import ast
import astor
class ReplaceNpDot(astor.TreeWalk):
def post_BinOp(self):
node = self.cur_node
if isinstance(node.op, ast.MatMult):
np = ast.Name(id="np", ctx=ast.Load())
np_dot = ast.Attribute(np, 'dot', ctx=ast.Load())
self.replace(ast.Call(
np_dot,
args=[node.left, node.right],
keywords=[],
startargs=[]
))
else:
return node
# define m so it works...
# ...
# replace @ with np.dot
tree = ast.parse("m.a@x + m.b@y", mode='eval')
walker = ReplaceNpDot()
walker.walk(tree)
# print source code
print(astor.to_source(tree))
# run code
code = compile(ast.fix_missing_locations(tree), '<string>', 'eval')
exec(code)
推荐阅读
- python - 我可以将 Travis-CI 与 Tkinter GUI 一起使用吗?
- python - 想要避免在输出的最后一个空白的新行
- angular - Angular 7 - 选择一个下拉列表的值正在更新其他下拉列表
- python - 使用请求模块正确分配 cookie
- excel - 将行与重复项合并,然后删除行
- android - 阻止视图在 ConstraintLayout 中重叠
- typescript - 如何在数组项中获取嵌套属性的映射类型?
- python - 获得关键字参数“标签”的多个值
- javascript - 有没有办法将数组的变量名打印为字符串?(JavaScript)
- django - 将模型和表单拆分为子文件夹结构 Django 2.0+