我想定义任意函数f。我知道f总是会返回一个正数。我希望能够在运行简化时(尤其是简化文档中提到的三个电源规则)使用这些知识。有没有办法做到这一点?我正在寻找类似下面的内容:
f = Function("f", positive = True)
g = Function("g", positive = True)
x = symbols("x")
y = symbols("y")
n = symbols("n", real = True)
test = ( f(x) * g(y) ) ** n
# This should work but doesn't
expand_power_base(test)
答案 0 :(得分:0)
这是一种不那么好的方式:
alphabet = list(string.ascii_lowercase)
def assert_positive(value, args):
result = value
for i in range( len(args) ):
a_symbol = symbols( alphabet[i], positive = True)
result = result.subs(args[i], a_symbol)
result = simplify(result)
for i in range( len(args) ):
a_symbol = symbols( alphabet[i], positive = True)
result = result.subs(a_symbol, args[i])
return(result)
答案 1 :(得分:0)
一种解决方法是使用expand_power_base
选项调用force=True
。这迫使sympy
执行功率简化,而不考虑假设。
import sympy as sp
f = sp.Function("f")
g = sp.Function("g")
x, y, n = sp.symbols("x, y, n")
test = ( f(x) * g(y) ) ** n
sp.expand_power_base(test, force=True)
f(x)**n*g(y)**n
答案 2 :(得分:0)
定义为Function('f')
的函数目前不支持假设。您需要显式创建子类,如
class f(Function):
is_positive = True