我正在嘗試將帶有sympy的布爾變量的函數派生出來。帶布爾值的sympy衍生物
我的預期的結果:
兩種不同的衍生物,這取決於布爾型用True或False(即1或0)。
例子:
import sympy as sy
c, x = sy.symbols("c x", positive=True, real=True)
bo = sy.Function("bo")
fct1 = sy.Function("fct1")
fct2 = sy.Function("fct2")
FOC2 = sy.Function("FOC2")
y = 5
a = 2
b = 4
def fct1(x):
return -0.004*x**2 + 0.25*x + 4
# the following gives the smaller positive intercept with the x-axis)
# this intercept is the threshold value for the boolean function, bo
min(sy.solve(fct1(x)-y, x))
def bo(x):
if fct1(x) <= y:
return 1
else:
return 0
def fct2(c, x):
return a + b*c + bo(x)*c
def FOC2(c, x):
return sy.diff(fct2(c, x), c)
print(FOC2(c, x))
的最小功能的意見後,顯示我的x爲博是真或假是4.29 ... ...,從而積極和現實的門檻。
輸出:
TypeError: cannot determine truth value of Relation
我明白,真值取決於X,這是一個象徵。因此,在不知道x的情況下,無法確定博。
但是,如何得到我的預期結果,其中博是象徵性的?
感謝您的建議和對我的代碼的評論。你的想法對我提出的問題很有效。在採取FOCs之後。一些符號變量,我試圖解決這些符號變量的FOCs系統。看起來我無法用海維思來做到這一點?使用sy.solve_triangulated會給我一個PolynomialError,說明Heaviside表達式包含發生器集的一個元素。有什麼想法嗎? –