问题描述
我尝试运行以下代码
# Import libraries
import sympy as sp
from numpy.random import default_rng
# Define the matrices M,K,a,b
M_1,M_2,K_1,K_2 = sp.symbols('M_1 M_2 K_1 K_2')
M = sp.Matrix([M_1,M_2])
K = sp.Matrix([K_1,K_2])
a = sp.Matrix([[9/2,3/2],[3/2,0]])
b = sp.Matrix([17/4,6/4])
# Define the matrix N
N = sp.Matrix([[9*M_1 + 3*M_2,3*M_1 + M_2],[3*M_1 + M_2,M_1]])
# Implement K^T * N^(-1) * K = 0,solve for M_1 and define M with the updated M_1
M = sp.Matrix([sp.solveset(sp.Eq(K.dot(N.inv()*K),0),M_1).args[0].args[0],M_2])
# Below we perform the random sampling of M,while respecting the
# constraints -1/2*M*K < Q_{D3},|K_1 + K_2|<<|K_2| and a*M,b*K are integer
# valued
def MKSampling(M,b):
rng = default_rng()
result = 0
print('Searching for vacuum...')
while result == 0:
K_1_temp = rng.integers(-100,100)
K_2_temp = rng.integers(-100,100)
M_2_temp = rng.integers(-100,100)
M_temp = M.subs({K_1: K_1_temp,K_2: K_2_temp,M_2: M_2_temp})
K_temp = K.subs({K_1: K_1_temp,K_2: K_2_temp})
print(M_temp)
print(K_temp)
if (((-1/2) * M_temp.dot(K_temp) < 138) and
(abs(K_1_temp + K_2_temp) < abs(K_2_temp)) and
(type((a*M_temp)[0]) == int) and
(type((a*M_temp)[1]) == int) and
(type(b.dot(K_temp)) == int)):
result = [M_temp,K_temp]
return print(result)
MKSampling(M,b)
文件“/home/master/anaconda3/lib/python3.8/site-packages/sympy/core/relational.py”,第 700 行,新 raise TypeError("非真实 %s 的无效比较" % me)
TypeError: 非真实动物园的无效比较
为了找出问题的根源,我在 while 循环中插入了 print(M_temp)
和 print(K_temp)
。似乎最终 rng.integers()
方法会生成一个带有参数“zoo”的 M_temp。
“动物园”是什么意思?为什么会发生?
干杯, 伊卡鲁斯
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)