- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须求解 x 的大量“Ax=B”类型的线性矩阵方程,其中 A 是一个稀疏矩阵,主要填充主对角线,B 是一个向量。
我的第一种方法是通过 numpy.linalg.solve 使用密集的 numpy 数组来实现此目的,并且它可以很好地处理 (N,n,n) 维数组,其中 N 是线性矩阵方程的数量,n 是方阵维数。我首先将它与迭代所有方程的 for 循环一起使用,这实际上相当慢。但后来意识到您也可以将 (N,n,n) 维矩阵直接传递给 numpy.linalg.solve ,而无需任何 for 循环(顺便说一下,我在阅读的文档中没有找到)。这已经大大提高了计算速度(详细信息见下文)。
但是,因为我有稀疏矩阵,所以我还查看了 scipy.sparse.linalg.spsolve 函数,它与相应的 numpy 函数执行类似的操作。使用 for 循环迭代所有方程比 numpy 解决方案快得多,但似乎不可能将 (N,n,n) 维数组直接传递给 scipy 的 spsolve。
这是我迄今为止尝试过的:
首先,我计算一些虚构的 A 矩阵和带有随机数的 B 向量以用于测试目的,包括稀疏矩阵和稠密矩阵:
import numpy as np
from scipy import sparse
from scipy.sparse.linalg import spsolve
number_of_systems = 100 #corresponds to N in the text
number_of_data_points = 1000 #corresponds to n in the text
#calculation of sample matrices (dense and sparse)
A_sparse = np.empty(number_of_systems,dtype=object)
A_dense = np.empty((number_of_systems,number_of_data_points,number_of_data_points))
for ii in np.arange(number_of_systems):
A_sparse[ii] = sparse.spdiags(np.random.random(number_of_data_points),0,number_of_data_points,number_of_data_points)
A_dense[ii] = A_sparse[ii].todense()
#calculation of sample vectors
B = np.random.random((number_of_systems,number_of_data_points))
1)第一种方法:带有 for 循环的 numpy.linalg.solve:
def solve_dense_3D(A,B):
results = np.empty((A.shape[0],A.shape[1]))
for ii in np.arange(A.shape[0]):
results[ii] = np.linalg.solve(A[ii],B[ii])
return results
result_dense_for = solve_dense_3D(A_dense,B)
时间:
timeit(solve_dense_3D(A_dense,B))
1.25 s ± 27.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
2) 第二种方法:将 (N,n,n) 维矩阵直接传递给 numpy.linalg.solve:
result_dense = np.linalg.solve(A_dense,B)
时间:
timeit(np.linalg.solve(A_dense,B))
769 ms ± 9.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
3)第三种方法:使用 scipy.sparse.linalg.spsolve 和 for 循环:
def solve_sparse_3D(A,B):
results = np.empty((A.shape[0],A[0].shape[0]))
for ii in np.arange(A.shape[0]):
results[ii] = spsolve(A[ii],B[ii])
return results
result_sparse_for = solve_sparse_3D(A_sparse,B)
时间:
timeit(solve_sparse_3D(A_sparse,B))
30.9 ms ± 132 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
很明显,能够省略方法 1 和 2 中的 for 循环是有好处的。正如可能预期的那样,迄今为止最快的替代方案是使用稀疏矩阵的方法 3。
因为这个例子中的方程数量对我来说仍然相当少,而且因为我必须经常做类似的事情,所以如果有一个使用 scipy 稀疏矩阵而不使用 for 循环的解决方案,我会很高兴。有人知道实现这一目标的方法吗?或者也许有另一种方法以甚至不同的方式解决问题?我很乐意提供建议。
最佳答案
一个小演示,概述了我上面评论中的想法:
""" YOUR CODE (only imports changed + deterministic randomness) """
import numpy as np
from scipy import sparse
from scipy.sparse.linalg import spsolve
from scipy.sparse import block_diag
from time import perf_counter as pc
np.random.seed(0)
number_of_systems = 100 #corresponds to N in the text
number_of_data_points = 1000 #corresponds to n in the text
#calculation of sample matrices (dense and sparse)
A_sparse = np.empty(number_of_systems,dtype=object)
A_dense = np.empty((number_of_systems,number_of_data_points,number_of_data_points))
for ii in np.arange(number_of_systems):
A_sparse[ii] = sparse.spdiags(np.random.random(number_of_data_points),0,number_of_data_points,number_of_data_points)
A_dense[ii] = A_sparse[ii].todense()
#calculation of sample vectors
B = np.random.random((number_of_systems,number_of_data_points))
def solve_sparse_3D(A,B):
results = np.empty((A.shape[0],A[0].shape[0]))
for ii in np.arange(A.shape[0]):
results[ii] = spsolve(A[ii],B[ii])
return results
start = pc()
result_sparse_for = solve_sparse_3D(A_sparse,B)
end = pc()
print(result_sparse_for)
print(end - start)
""" ALTERNATIVE APPROACH """
def solve_sparse_3D_blockdiag(A,B):
oldN = B.shape[0]
A_ = block_diag(A) # huge sparse block-matrix of independent problems
B_ = B.ravel() # flattened vector
results = spsolve(A_, B_)
return results.reshape(oldN, -1) # unflatten results
start = pc()
result_sparse_for = solve_sparse_3D_blockdiag(A_sparse,B)
end = pc()
print(result_sparse_for)
print(end - start)
输出
[[ 0.97529866 1.26406276 0.83348888 ... 0.99310639 3.90781207
0.16724226]
[ 1.23398934 28.82088739 1.6955886 ... 1.85011686 0.23386882
1.17208753]
[ 0.92864777 0.22248781 0.09445412 ... 2.5080376 0.91701228
0.97266564]
...
[ 0.33087093 0.89034736 1.7523883 ... 0.2171746 4.89236164
0.31546549]
[ 1.2163625 3.0100941 0.87216264 ... 1.62105596 0.33211353
2.07929302]
[ 5.35677404 1.23830776 0.16073721 ... 0.26492506 0.53676822
3.73192617]]
0.08764066299999995
###
[[ 0.97529866 1.26406276 0.83348888 ... 0.99310639 3.90781207
0.16724226]
[ 1.23398934 28.82088739 1.6955886 ... 1.85011686 0.23386882
1.17208753]
[ 0.92864777 0.22248781 0.09445412 ... 2.5080376 0.91701228
0.97266564]
...
[ 0.33087093 0.89034736 1.7523883 ... 0.2171746 4.89236164
0.31546549]
[ 1.2163625 3.0100941 0.87216264 ... 1.62105596 0.33211353
2.07929302]
[ 5.35677404 1.23830776 0.16073721 ... 0.26492506 0.53676822
3.73192617]]
0.07241856000000013
有一些事情要做:
permc_spec
关于python - 求解多个线性稀疏矩阵方程 : "numpy.linalg.solve" vs. "scipy.sparse.linalg.spsolve",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57484892/
Scipy 和 Numpy 在它们之间具有三个不同的函数来查找给定方阵的特征向量,它们是: numpy.linalg.eig(a) scipy.linalg.eig(a) , 和 scipy.spar
这是我的 numpy 数组: z [[ 3.90311860e-322 1.83939721e-001] [ 0.00000000e+000 1.83939721e-001] [ 0
公认的智慧是更喜欢 scipy.linalg 而不是 numpy.linalg 函数。为了进行线性代数,理想情况下(并且方便地)我想结合 numpy.array 和 scipy.linalg 的功能,
有人知道什么时候最好选择哪个吗?在我看来,它们是一样的... lsmr lsqr 最佳答案 两种软件包的功能相同。 LSMR基于2010年的Fong&Saunders算法(请参阅paper),并且最近
如果我们想通过使用正规方程来搜索线性回归模型的最佳参数 theta: theta = inv(X^T * X) * X^T * y 第一步是计算 inv(X^T*X)。因此 numpy 提供 np.l
要求解线性矩阵方程,可以使用 numpy.linalg.solve它实现了 LAPACK 例程 *gesv . 根据文档 DGESV computes the solution to a real s
问题描述 对于方阵,可以得到SVD X= USV' 分解,通过简单地使用 numpy.linalg.svd u,s,vh = numpy.linalg.svd(X) 例程或 numpy.lin
有没有办法提高numpy.linalg.eig()和scipy.linalg.eig()的输出精度? 我正在对角化一个非对称矩阵,但我希望在物理基础上得到正负特征值对的实谱。事实上,特征值确实成对出现
lstsq 尝试解决 Ax=b 最小化 |b - Ax|。 scipy 和 numpy 都提供了一个具有非常相似接口(interface)的 linalg.lstsq 函数。文档没有提到使用哪种算法,
如果我有一个由五个向量 v1...v5 组成的向量空间,则找到 A 的正交基,其中 A=[v1,v2...v5] 且 A 为 5Xn 我应该使用np.linalg.qr(A)还是scipy.linal
我必须求解 x 的大量“Ax=B”类型的线性矩阵方程,其中 A 是一个稀疏矩阵,主要填充主对角线,B 是一个向量。 我的第一种方法是通过 numpy.linalg.solve 使用密集的 numpy
我不太明白为什么 numpy.linalg.solve() 给出了更准确的答案,而 numpy.linalg.inv() 有点崩溃,给出 (我相信是)估计。 举一个具体的例子,我正在求解方程 C^{-
我有一个奇怪的现象,虽然 scipy.sparse.linalg.eigs 对于稀疏矩阵应该更快,但我知道它运行得比正常的 eigvals 方法慢scipy: In [4]: %timeit m.ca
我正在使用 Spark cluster 2.0,我想从 org.apache.spark.mllib.linalg.VectorUDT 转换一个向量至org.apache.spark.ml.linal
我有下面的代码,我使用命令 scipy.linalg.lu() 计算给定方阵的 L 矩阵,然后我再次执行相同的操作,除了然后应用于给定矩阵的稀疏形式使用 scipy.sparse.linalg.slu
我在学习SVD通过关注这个 MIT course . 矩阵构造为 C = np.matrix([[5,5],[-1,7]]) C matrix([[ 5, 5], [-1, 7]]
如何从org.apache.spark.mllib.linalg.SparseVector至org.apache.spark.ml.linalg.SparseVector ? 我正在从 mllib 转
有人可以帮我解决以下错误吗?我正在尝试将数据帧转换为 rdd,以便它可以用于回归模型构建。 Spark 版本:2.0.0 错误 => ClassCastException: org.apache.sp
我正在尝试在 Python 上实现最小二乘曲线拟合算法,我已经在 Matlab 上编写了它。但是,我无法获得正确的变换矩阵,而且问题似乎发生在求解步骤。 (编辑:我的变换矩阵在 Matlab 中非常准
前言 numpy.linalg模块包含线性代数的函数。使用这个模块,可以计算逆矩阵、求特征值、解线性方程组以及求解行列式等。本文讲给大家介绍关于numpy基础之 np.linalg的相关内容,下面
我是一名优秀的程序员,十分优秀!