Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sympy中布尔表达式的非期望求值_Python_Anaconda_Boolean_Sympy_Piecewise - Fatal编程技术网

Python sympy中布尔表达式的非期望求值

Python sympy中布尔表达式的非期望求值,python,anaconda,boolean,sympy,piecewise,Python,Anaconda,Boolean,Sympy,Piecewise,我试着用symphy来表达一个分段函数,然后绘制它,但我不能表达想要的函数。问题是(x>0)和(x0评估为始终True。这可以通过在符号构造函数中不设置positive=True来避免,但这会使平方根简化为√(x^2),在这种情况下,我希望它简化为x。我目前正在windows上最新版本的Anaconda发行版中使用基本(根)环境。代码在上述环境中使用JupyterLab笔记本进行测试 当前代码: from sympy import * x = symbols('x', real=True, po

我试着用symphy来表达一个分段函数,然后绘制它,但我不能表达想要的函数。问题是
(x>0)和(x<1)
在传递给分段构造函数之前,将
x>0
评估为始终
True
。这可以通过在符号构造函数中不设置
positive=True
来避免,但这会使平方根简化为
√(x^2)
,在这种情况下,我希望它简化为
x
。我目前正在windows上最新版本的Anaconda发行版中使用基本(根)环境。代码在上述环境中使用JupyterLab笔记本进行测试

当前代码:

from sympy import *
x = symbols('x', real=True, positive=True)
f = sqrt(x**2)

f_piecewise = Piecewise((2, (x > 0) & (x < 1) ),
          ( 3 * f, (x > 1) & (x < 2) ),
          ( -3 * f, (x > 2) & (x < 3)),
          (0, True)
         )

pprint(f_piecewise)
display(f_piecewise)
plot(f_piecewise, (x, -0.01, 3.01))
⎧ 2        for x < 1    
⎪                       
⎪3⋅x   for x > 1 ∧ x < 2
⎨                       
⎪-3⋅x  for x > 2 ∧ x < 3
⎪                       
⎩ 0        otherwise    
工作代码:

test = Piecewise((f, Gt(x, 0, evaluate=False)),
                 (1, True) 
                )
pprint(test)
display(test)
plot(test)
我尝试了以下方法使其与“和”一起工作,但没有一个成功:

from sympy.parsing.sympy_parser import parse_expr
test = Piecewise((f, parse_expr("GreaterThan(x, 0, evaluate=False) & (x < 1)", {'x':x}, evaluate=False)),
                 (1, True) 
                )

test = Piecewise((f, And(GreaterThan(x, 0, evaluate=False), (x < 1), evaluate=False)),
                 (1, True) 
                )
从sympy.parsing.sympy\u解析器导入parse\u expr
test=分段((f,parse_expr(“大于(x,0,evaluate=False)&(x<1)”,{'x':x},evaluate=False)),
(1,正确)
)
测试=分段((f和(大于(x,0,evaluate=False),(x<1,evaluate=False)),
(1,正确)
)

多亏有人花时间来研究这一点:D.

辛普森形成了正确的方程组。 关键是将符号变量设置为正。在这种情况下,系统:

⎧ 2        for x < 1    
⎪                       
⎪3⋅x   for x > 1 ∧ x < 2
⎨                       
⎪-3⋅x  for x > 2 ∧ x < 3
⎪                       
⎩ 0        otherwise   
⎧ x<1时为2
⎪                       
⎪3.⋅x表示x>1∧ x<2
⎨                       
⎪-3.⋅x表示x>2∧ x<3
⎪                       
⎩ 否则为0
这是正确的

要获得您期望的系统,您需要删除积极性约束。下面的代码可以工作

from sympy import *
x = symbols('x', real=True)
f = sqrt(x**2)

f_piecewise = Piecewise((2, (x > 0) & (x < 1) ),
          ( 3 * f, (x > 1) & (x < 2) ),
          ( -3 * f, (x > 2) & (x < 3)),
          (0, True)
         )

pprint(f_piecewise)
来自sympy导入的
*
x=符号('x',实=真)
f=sqrt(x**2)
f_分段=分段((2,(x>0)和(x<1)),
(3*f,(x>1)和(x<2)),
(-3*f,(x>2)和(x<3)),
(0,对)
)
pprint(f_分段)
这就是它的回报:

⎧  2     for x > 0 ∧ x < 1
⎪                         
⎪3⋅│x│   for x > 1 ∧ x < 2
⎨                         
⎪-3⋅│x│  for x > 2 ∧ x < 3
⎪                         
⎩  0         otherwise
⎧  x>0时为2∧ x<1
⎪                         
⎪3.⋅│x│   对于x>1∧ x<2
⎨                         
⎪-3.⋅│x│  对于x>2∧ x<3
⎪                         
⎩  否则为0

注意,你的函数在第1点和第2点被定义为零。使用“>=”或“可能有某种原因,你不能只做
f=x
”。
from sympy import *
x = symbols('x', real=True)
f = sqrt(x**2)

f_piecewise = Piecewise((2, (x > 0) & (x < 1) ),
          ( 3 * f, (x > 1) & (x < 2) ),
          ( -3 * f, (x > 2) & (x < 3)),
          (0, True)
         )

pprint(f_piecewise)
⎧  2     for x > 0 ∧ x < 1
⎪                         
⎪3⋅│x│   for x > 1 ∧ x < 2
⎨                         
⎪-3⋅│x│  for x > 2 ∧ x < 3
⎪                         
⎩  0         otherwise