sympy arbitrary function range

拟墨画扇 提交于 2019-12-06 07:27:21

Functions defined like Function('f') do not support assumptions at this time. You'll need to create a subclass explicitly, like

class f(Function):
    is_positive = True

Here is a not-so-great way of going about things:

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)

One workaround is to call expand_power_base with the force=True option. This forces sympy to perform power simplifications, irrespective of assumptions.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!