【发布时间】:2017-03-24 01:00:49
【问题描述】:
更新:
>>> solve([A(x)*A(y) + A(-1), A(x) + A(-2)], x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 12, in __mul__
TypeError: unbound method __multiplyFunction__() must be called with A instance
as first argument (got Symbol instance instead)
class A:
@staticmethod
def __additionFunction__(a1, a2):
return a1*a2 #Put what you want instead of this
def __multiplyFunction__(a1, a2):
return a1*a2+a1 #Put what you want instead of this
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.__class__.__additionFunction__(self.value, other.value)
def __mul__(self, other):
return self.__class__.__multiplyFunction__(self.value, other.value)
solve([A(x)*A(y) + A(-1), A(x) + A(-2)], x, y)
更新2:
>>> ss([x*y + -1, x-2], x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: solve instance has no __call__ method
class AA:
@staticmethod
def __additionFunction__(a1, a2):
return a1*a2 #Put what you want instead of this
def __multiplyFunction__(a1, a2):
return a1*a2+a1 #Put what you want instead of this
def __init__(self, value):
self.value = value
def __add__(self, other):
return self.__class__.__additionFunction__(self.value, other.value)
def __mul__(self, other):
return self.__class__.__multiplyFunction__(self.value, other.value)
ss = solve(AA)
ss([x*y + -1, x-2], x, y)
想在求解函数中将添加运算符更改为自定义函数 op2 然后这个求解([x*y - 1, x + 2], x, y) 在求解过程中,参数也发生变化 添加到自定义函数 op2
错误,因为我不知道如何将 op2 作为 ast 树注入 ast 树以供 ast 树使用
>>> class ChangeAddToMultiply(ast.NodeTransformer, ast2.NodeTransformer):
... """Wraps all integers in a call to Integer()"""
... def visit_BinOp(self, node):
... print(dir(node))
... print(dir(node.left))
... if isinstance(node.op, ast.Add):
... ast.Call(Name(id="op2", ctx=ast2.Load()), [node.left, node.right
], [])
... return node
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ast2' is not defined
>>>
>>> code = inspect.getsourcelines(solve)
>>> tree = ast.parse(code)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
TypeError: expected a readable buffer object
>>> tree2 = ast.parse("def op2(a,b): return a*b+a")
>>> tree = ChangeAddToMultiply().visit(tree,tree2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>> ast.fix_missing_locations(tree)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>> co = compile(tree, '<ast>', "exec")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>>
>>> exec(code)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exec: arg 1 must be a string, file, or code object
>>> exec(co)
原代码
import ast
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
import inspect
def op2(a,b):
return a*b+a
class ChangeAddToMultiply(ast.NodeTransformer, ast2.NodeTransformer):
"""Wraps all integers in a call to Integer()"""
def visit_BinOp(self, node):
print(dir(node))
print(dir(node.left))
if isinstance(node.op, ast.Add):
ast.Call(Name(id="op2", ctx=ast2.Load()), [node.left, node.right], [])
return node
code = inspect.getsourcelines(solve([x*y - 1, x - 2], x, y))
tree = ast.parse(code)
tree2 = ast.parse("def op2(a,b): return a*b+a")
tree = ChangeAddToMultiply().visit(tree,tree2)
ast.fix_missing_locations(tree)
co = compile(tree, '<ast>', "exec")
exec(code)
exec(co)
【问题讨论】:
标签: python python-2.7 sympy