我正在尝试编写一个带有参数(a,tol=1e-8)
的函数,该函数返回一个布尔值,告诉用户矩阵是否对称(对称矩阵等于其转置)。到目前为止,我的代码如下:
def check_symmetric(a, tol=1e-8):
if np.transpose(a, axes=axes) == np.transpose(a, axes=axes):
return True
def sqr(s):
rows = len(s)
for row in sq:
if len(row) != rows:
return False
return True
if a != sqr(s):
raise ValueError
尽管我一直收到“axes未定义”的消息,所以我非常确定那根本不起作用......我想通过的测试是:
e = np.eye(4)
f = np.diag([1], k=3)
g = e[1:, :]
print(check_symmetric(e))
print(not check_symmetric(e + f))
print(check_symmetric(e + f * 1e-9))
print(not check_symmetric(e + f * 1e-9, 1e-10))
try:
check_symmetric(g)
print(False)
except ValueError:
print(True)
Any help is appreciated, thanks!
def check_symmetric(a, tol=1e-8): if np.transpose(a.any()) == np.array(a.any()): return True def sqr(s): rows = len(s) for row in sq: if len(row) != rows: return False return True if a != sqr(s): raise ValueError
- plshalp