TypeError: unsupported operand type(s) for *: 'float' and 'Equality'

TypeError: unsupported operand type(s) for *: 'float' and 'Equality'

我收到一个错误

TypeError: unsupported operand type(s) for *: 'float' and 'Equality' at "Jn=(q*E*muN*N_at_0)".

如何执行 <class 'sympy.core.relational.Equality'><class 'float'> 的乘法运算?

import sympy as sp
from sympy import *
import math
x=sp.Symbol('x')
n=sp.Function('n')(x)
p=sp.Function('p')(x)
t=200*10**(-7)
q=1.60217662*10**(-19)
k=1.38065*10**(-23)
T=300.00
Vbi=1
V=0
alpha=5.7*10**4
N0=1.71*10**17
G=alpha*N0
E=(V-Vbi)/t
muN=1
muP=1
Vt=k*T/q
Dn=muN*Vt
Dp=muP*Vt
Nc=2.2*10**(20)
Nv=Nc
Nd=3*10**(19)
Na=10**(18)
Eg=1.55
n_t = Nc*math.exp(-Eg/Vt)
p_0 = Nc*math.exp(-Eg/Vt)
Nsol=sp.Eq(Dn*n.diff(x,x)+(muN*E)*n.diff(x)+G,0)    
Psol=sp.Eq(Dp*p.diff(x,x)-(muP*E)*p.diff(x)+G,0)
ics1={n.subs(x,0):Nd, n.subs(x,t):n_t}
ics2={p.subs(x,0):p_0, p.subs(x,t):Na}
ivN=sp.dsolve(Nsol, ics=ics1)
ivP=sp.dsolve(Psol, ics=ics2)
N_at_0=ivN.subs(x,0)
P_at_0=ivP.subs(x,0)
Jn=(q*E*muN*N_at_0)
Jp=(q*E*muP*P_at_0)

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-3634a48ae130> in <module>()
     35 N_at_0=ivN.subs(x,0)
     36 P_at_0=ivP.subs(x,0)
---> 37 Jn=(q*E*muN*N_at_0)
     38 Jp=(q*E*muP*P_at_0)

我喜欢看到中间结果,尤其是在 sympy 计算中。它更容易阅读和理解。否则我必须自己 运行 代码(并非总是可行)。

In [4]: n_t = Nc*math.exp(-Eg/Vt)
   ...: p_0 = Nc*math.exp(-Eg/Vt)
In [5]: n_t
Out[5]: 2.0118139286850867e-06

In [6]: Nsol=sp.Eq(Dn*n.diff(x,x)+(muN*E)*n.diff(x)+G,0)
   ...: Psol=sp.Eq(Dp*p.diff(x,x)-(muP*E)*p.diff(x)+G,0) 
In [7]: Nsol
Out[7]: 
                                          2                      
          d                              d                       
- 50000.0⋅──(n(x)) + 0.0258520187368606⋅───(n(x)) + 9.747e+21 = 0
          dx                              2                      
                                        dx                       

In [8]: ics1={n.subs(x,0):Nd, n.subs(x,t):n_t}
   ...: ics2={p.subs(x,0):p_0, p.subs(x,t):Na}
In [9]: ics1
Out[9]: {n(0): 30000000000000000000, n(2.0e-5): 2.0118139286850867e-06}

In [10]: ivN=sp.dsolve(Nsol, ics=ics1)
    ...: ivP=sp.dsolve(Psol, ics=ics2)
In [11]: ivN
Out[11]: 
                                        1934084.9358394⋅x          
n(x) = 1.9494e+17⋅x - 476.291693838697⋅ℯ                  + 3.0e+19

In [12]: N_at_0=ivN.subs(x,0)
    ...: P_at_0=ivP.subs(x,0)
In [13]: N_at_0
Out[13]: n(0) = 3.0e+19

In [14]: type(_)
Out[14]: sympy.core.relational.Equality

所以 N_at_0 是一个 Equality 对象 - 这就是错误所抱怨的:

In [15]: Jn=(q*E*muN*N_at_0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-28d29a3734cf> in <module>
----> 1 Jn=(q*E*muN*N_at_0)

TypeError: unsupported operand type(s) for *: 'float' and 'Equality'

但是我们可以用 .rhs:

得到 Eq 的数字部分
In [24]: N_at_0.rhs
Out[24]: 3.00000000000000e+19

In [25]: Jn=(q*E*muN*N_at_0.rhs)

In [26]: Jn
Out[26]: -240326.493000000