如何解决我的使用scipy的python代码抛出值错误
def prob6():
"""Time regular and sparse linear system solvers.
For varIoUs values of n,generate the (n**2,n**2) matrix A described of
prob5() and vector b of length n**2. Time how long it takes to solve the
system Ax = b with each of the following approaches:
1. Convert A to CSR format and use scipy.sparse.linalg.spsolve()
2. Convert A to a NumPy array and use scipy.linalg.solve().
In each experiment,only time how long it takes to solve the system (not
how long it takes to convert A to the appropriate format). Plot the system
size n**2 versus the execution times. As always,use log scales where
appropriate and use a legend to label each line.
"""
#raise NotImplementedError("Problem 6 Incomplete")
n = 2**np.arange(1,5)
time1 = []
time2 = []
for i in n:
diagonals = [[1],[-4],[1]]
offsets = [-1,1]
B = (sparse.diags(diagonals,offsets,shape=(i,i)))
A = sparse.block_diag(([B]*i))
A.setdiag(1,i)
A.setdiag(1,-i)
A.setdiag(1,-i)
print(A.toarray())
b = np.random.random(i*i)
Acsr = A.tocsr()
start = time.time()
Acsr_final = spla.spsolve(A,b)
time1.append(time.time() - start)
for i in n:
diagonals = [[1],-i)
A.toarray()
b = np.random.random(i*i)
Anpa = np.squeeze(np.asarray(A))
start = time.time()
Anpa_final = sp.linalg.solve(A,b)
time2.append(time.time() - start)
plt.loglog(n,time1,'m.-',lw=2,ms=15,label="time 1")
plt.loglog(n,time2,'g.-',label="time 2")
plt.legend()
plt.title("Times regular and sparse linear system solvers")
plt.xlabel("n",fontsize=14)
plt.ylabel("Seconds",fontsize=14)
plt.show()
prob6()
它一直在第243行抛出以下ValueError,我似乎无法解决。我的scipy功能之一似乎有问题。我也不确定我所做的是否正确,因此这也可能使事情变得混乱。有人可以帮忙吗?
ValueError: Sparse matrices are not supported by this function. Perhaps one of the scipy.sparse.linalg functions would work instead.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。