微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeError:非真实动物园的无效比较

如何解决TypeError:非真实动物园的无效比较

我尝试运行以下代码

# 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 举报,一经查实,本站将立刻删除。