簡體   English   中英

如何用Python中的Sympy在Y軸而不是X軸方面做功能?

[英]How do you do a function in terms of the Y axis instead of the X axis with Sympy in Python?

我試圖編寫一個Game Theory metod來尋找2x2游戲的最佳混合策略。

無論如何,要做到這一點,我需要定義兩個函數,一個是x軸,另一個是y軸(是的,需要在二維平面上工作的經典軸),所以后來一個可以獲得交點。

例如;
假設我有從2x2游戲中獲得的下一個功能:

L(y)= y + 1

L(x)= x-2

哪里:

   if L(y)> 0, i.e, y+1>0, then x=1 (this is if y>-1 then x=1)
   if L(y)< 0, then x=0  (this is if y<-1 then x=0)
   if L(y)= 0, then x∈[0,1] (this is if y=-1 then x∈[0,1])

   if L(x) >0 then  y=1 (this is: if x>2 then y=1)
   if L(x) <0 then  y=0 (this is: if x<2 then y=0)
   if L(x) =0 then  y∈[0,1] (this is: if x=2 then y∈[0,1]

https://imgur.com/a/eXeTVF0

我正在尋找一個可以做到這一點的Python庫,我發現,通過sympy可以創建分段函數,就像我想要做的那樣。 我做的是這樣的:

    from sympy import symbols, Piecewise
    x,y = symbols('x,y')

    p = Piecewise((1, x > 2), (0, x <2))
    q = Piecewise((1, y>-1), (0,y<-1))

然后從中找到交叉點。 問題是Python並不關心變量的“名稱”,它將它們視為在同一X軸上的位置。

此外,我無法在同一時間繪制它們,當然也找到了它的交叉點。 這對Python來說甚至可能嗎? 我應該怎么做,或者我可以用其他方式解決這個問題。 問候。

如果你想通過“p和q的交集”來表示你想知道它們相同的條件,你可以“折疊”元組(p,q)來告訴你:

>>> piecewise_fold(Tuple(p, q))
Piecewise(
  ((1, 1), (x > 2) & (y > -1)),
  ((1, 0), x > 2),
  ((0, 1), y > -1),
  ((0, 0), True))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM