SympifyError:SympifyError:使用循环时索引
在更改循环中的参数时,我在使用 simplify 时遇到问题。在添加循环之前,它工作得很好,所以我对出了什么问题有点困惑。这个想法是在参数变化时计算上述方程的不动点。我事先使用随机算法确定了参数。
使用的数据
index c1 c2 c3 c4 c5
2 0.182984 2.016811 0.655393 1.581344 1000.0
3 0.481093 3.696431 0.174021 2.604066 1000.0
4 2.651888 0.665661 2.010521 1.004902 1000.0
5 4.356905 3.805205 0.169469 0.188154 1000.0
6 0.618898 1.205760 0.394822 0.624573 1000.0
7 1.628458 0.908339 0.117855 0.801636 1000.0
8 1.084346 0.251490 5.008077 4.606338 1000.0
9 0.314420 4.553279 0.279103 1.136288 1000.0
10 0.309323 3.447195 0.769426 1.058890 1000.0
11 1.353905 5.034620 3.025668 0.136687 1000.0
12 0.294230 0.590507 0.203964 0.105073 1000.0
13 0.433693 1.040195 0.197015 0.214636 1000.0
14 5.597691 2.734779 0.298786 6.869852 1000.0
15 0.106748 0.329506 1.642285 2.259433 1000.0
16 7.065243 0.138986 6.280275 0.265305 1000.0
17 0.676381 0.263757 6.540224 2.890927 1000.0
18 0.646750 2.573060 0.157341 1.779078 1000.0
19 2.829030 0.208247 0.102454 0.117786 1000.0
20 3.973703 0.134666 1.099034 4.255214 1000.0
df1 = df[df.columns[1]]
df2 = df[df.columns[2]]
df3 = df[df.columns[3]]
df4 = df[df.columns[4]]
EQ=[]
for i in df[:5]:
a = df["c1"]
b = df["c2"]
c = df["c3"]
d = df["c4"]
Q = 1
a1 = 0
b1 = 0
c1 = 0
d1 = 0
u,v = sm.symbols('u,v', negative=False)
# equations
U = a * u -a1* v**2 - b*v+b1*v + Q
V = c * u -c1*u*v- d*v + d1 + Q
# use sympy's way of setting equations to zero
Uqual = sm.Eq(U, 0)
Vqual = sm.Eq(V, 0)
# compute fixed points
equilibria = sm.solve( (Uqual, Vqual), u,v)
print('The fixed point(s) of this system are: %s' %equilibria)
equilibria.append(equilibria)
SympifyError Traceback (most recent call last)
<ipython-input-81-7104e05ced6a> in <module>
16 V = c * u -c1*u*v- d*v + d1 + Q
17 # use sympy's way of setting equations to zero
---> 18 Uqual = sm.Eq(U, 0)
19 Vqual = sm.Eq(V, 0)
20
~\anaconda3\lib\site-packages\sympy\core\relational.py in __new__(cls, lhs, rhs, **options)
501 rhs = 0
502 evaluate = options.pop('evaluate', global_parameters.evaluate)
--> 503 lhs = _sympify(lhs)
504 rhs = _sympify(rhs)
505 if evaluate:
~\anaconda3\lib\site-packages\sympy\core\sympify.py in _sympify(a)
510
511 """
--> 512 return sympify(a, strict=True)
513
514
~\anaconda3\lib\site-packages\sympy\core\sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
431
432 if strict:
--> 433 raise SympifyError(a)
434
435 if iterable(a):
SympifyError: SympifyError: index
1 0.32539361355594*u - 0.153951771353544*v + 1
2 0.111286178007145*u - 0.211620881593914*v + 1
3 0.410704332996077*u - 0.338148622964363*v + 1
4 1.39126513227539*u - 0.715390758416011*v + 1
5 0.289981428632838*u - 3.76334113661812*v + 1
...
96 0.450838908230239*u - 7.00849756407416*v + 1
97 4.59646738213032*u - 1.45107766000711*v + 1
98 6.28779804684458*u - 0.395831415205476*v + 1
99 0.196464087698782*u - 0.205057919337616*v + 1
100 1.69031014508742*u - 0.140571509904066*v + 1
Length: 100, dtype: object
I am having trouble using simpify when changing the parameters in a loop. Before adding the loop it worked just fine so I am a bit confused about what is going wrong. The idea is to calculate the fixed points for the above equations when having a varying parameter. I determined the parameters by using a random algorithm beforehand.
data used
index c1 c2 c3 c4 c5
2 0.182984 2.016811 0.655393 1.581344 1000.0
3 0.481093 3.696431 0.174021 2.604066 1000.0
4 2.651888 0.665661 2.010521 1.004902 1000.0
5 4.356905 3.805205 0.169469 0.188154 1000.0
6 0.618898 1.205760 0.394822 0.624573 1000.0
7 1.628458 0.908339 0.117855 0.801636 1000.0
8 1.084346 0.251490 5.008077 4.606338 1000.0
9 0.314420 4.553279 0.279103 1.136288 1000.0
10 0.309323 3.447195 0.769426 1.058890 1000.0
11 1.353905 5.034620 3.025668 0.136687 1000.0
12 0.294230 0.590507 0.203964 0.105073 1000.0
13 0.433693 1.040195 0.197015 0.214636 1000.0
14 5.597691 2.734779 0.298786 6.869852 1000.0
15 0.106748 0.329506 1.642285 2.259433 1000.0
16 7.065243 0.138986 6.280275 0.265305 1000.0
17 0.676381 0.263757 6.540224 2.890927 1000.0
18 0.646750 2.573060 0.157341 1.779078 1000.0
19 2.829030 0.208247 0.102454 0.117786 1000.0
20 3.973703 0.134666 1.099034 4.255214 1000.0
df1 = df[df.columns[1]]
df2 = df[df.columns[2]]
df3 = df[df.columns[3]]
df4 = df[df.columns[4]]
EQ=[]
for i in df[:5]:
a = df["c1"]
b = df["c2"]
c = df["c3"]
d = df["c4"]
Q = 1
a1 = 0
b1 = 0
c1 = 0
d1 = 0
u,v = sm.symbols('u,v', negative=False)
# equations
U = a * u -a1* v**2 - b*v+b1*v + Q
V = c * u -c1*u*v- d*v + d1 + Q
# use sympy's way of setting equations to zero
Uqual = sm.Eq(U, 0)
Vqual = sm.Eq(V, 0)
# compute fixed points
equilibria = sm.solve( (Uqual, Vqual), u,v)
print('The fixed point(s) of this system are: %s' %equilibria)
equilibria.append(equilibria)
SympifyError Traceback (most recent call last)
<ipython-input-81-7104e05ced6a> in <module>
16 V = c * u -c1*u*v- d*v + d1 + Q
17 # use sympy's way of setting equations to zero
---> 18 Uqual = sm.Eq(U, 0)
19 Vqual = sm.Eq(V, 0)
20
~\anaconda3\lib\site-packages\sympy\core\relational.py in __new__(cls, lhs, rhs, **options)
501 rhs = 0
502 evaluate = options.pop('evaluate', global_parameters.evaluate)
--> 503 lhs = _sympify(lhs)
504 rhs = _sympify(rhs)
505 if evaluate:
~\anaconda3\lib\site-packages\sympy\core\sympify.py in _sympify(a)
510
511 """
--> 512 return sympify(a, strict=True)
513
514
~\anaconda3\lib\site-packages\sympy\core\sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
431
432 if strict:
--> 433 raise SympifyError(a)
434
435 if iterable(a):
SympifyError: SympifyError: index
1 0.32539361355594*u - 0.153951771353544*v + 1
2 0.111286178007145*u - 0.211620881593914*v + 1
3 0.410704332996077*u - 0.338148622964363*v + 1
4 1.39126513227539*u - 0.715390758416011*v + 1
5 0.289981428632838*u - 3.76334113661812*v + 1
...
96 0.450838908230239*u - 7.00849756407416*v + 1
97 4.59646738213032*u - 1.45107766000711*v + 1
98 6.28779804684458*u - 0.395831415205476*v + 1
99 0.196464087698782*u - 0.205057919337616*v + 1
100 1.69031014508742*u - 0.140571509904066*v + 1
Length: 100, dtype: object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
isympy
会话中:制作示例数据帧:
设置非迭代案例:
与数据帧值对比:
此
U
是 pandasSeries
,带有对象元素(它们是sympy表达式)。但U
本身并不是 sympy。应用于简单表达式的 Eq:
您的错误 - 应用于系列的 Eq:
Eq()
不具有“迭代系列”(甚至遍历列表)功能。我们可以迭代(列表理解)并将
Eq
应用于系列的每个项:作为一般规则,
sympy
和pandas/numpy
确实效果不好。In an
isympy
session:Make a sample dataframe:
Set up a non-iterative case:
Versus on with dataframe values:
This
U
is a pandasSeries
, with object elements (which are sympy expressions). ButU
itself is not sympy.Eq applied to the simple expression:
Your error - Eq applied to the Series:
Eq()
does not have a 'iterate over Series' (or even over list) capability.We can iterate (list comprehension) and apply the
Eq
to each terms of the Series:As a general rule,
sympy
andpandas/numpy
does not work well.很难理解您在上面发布的代码中要实现的目标。因此,以下是我的猜测:
It's hard to understand what you are trying to achieve with the code you posted above. So, the following represents my guess: