0
我在Sympy中創建了兩個未定義的函數。當我把兩個未定義函數的產品log
和我應用log_expand()
時,我沒有得到兩個未定義函數的日誌總和。sympy log_expand兩個未定義函數的乘積
最小工作例子(MWE):
class A(Function):
nargs = 1
def fdiff(self, argindex=1):
if argindex == 1:
return -1*self.args[0]*A(self.args[0])
else:
raise ArgumentIndexError(self, argindex)
class B(Function):
nargs = 1
def fdiff(self, argindex=1):
if argindex == 1:
return self.args[0]*B(self.args[0])
else:
raise ArgumentIndexError(self, argindex)
x = Symbol('x', real=True)
expr = A(x)*B(x)
pprint(expr) # Outputs: A(x)⋅B(x)
pprint(log(expr)) # Outputs: log(A(x)⋅B(x))
pprint(expand_log(log(expr))) # Outputs: log(A(x)⋅B(x))
在最後一行的預期輸出是log(A(x)) + log(B(x))
但我得到log(A(x)⋅B(x))
。
是否有一個屬性或方法需要在每個類中定義?
請注意,MWE中未定義的功能僅用於演示目的。真正的功能更復雜,並且功能eval
類方法(@classmethod
)。
(這是一種雜牌,因爲我們假設F和G始終爲正,而不管參數。) – smichr
道歉「非接受」這樣的回答。我原來接受了這個答案,因爲在我的類定義裏面加上'positive = True'就好了。但是我錯誤地查看了手動添加'log(f)'和'log(g)'的函數的輸出。道歉。 – Stefan
如果您要編寫自己的函數,那麼在內部寫入'is_positive = True' - 使用「is_」前綴。 – smichr