python分块三对角矩阵
我想从三个 numpy.ndarray 开始创建一个块三对角矩阵。 有没有任何(直接)方法可以在 python 中做到这一点?
先感谢您!
干杯
I would like to create a block tridiagonal matrix starting from three numpy.ndarray.
Is there any (direct) way to do that in python?
Thank you in advance!
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于“常规”numpy 数组,使用 numpy.diag :
With "regular" numpy arrays, using numpy.diag:
使用函数
scipy.sparse.diags
。示例:
这将返回:
Use the function
scipy.sparse.diags
.Example:
This returns:
您还可以通过花哨的索引使用“常规”numpy 数组来执行此操作:(
如果您想更简洁,您可以将这些对
np.arange
的调用替换为np.r_
例如,使用data[np.r_[:3]+4, np.r_[,而不是
)data[np.arange(3)+4, np.arange(3)]
:3]]这会产生:
但是,如果您无论如何都要使用稀疏矩阵,请查看
scipy.sparse.spdiags
。 示例中位置 4 中的 3),则需要在行值上添加假数据))(请注意,如果您将数据放入具有正值的对角线位置(例如 简单的例子:
这会产生:
You can also do this with "regular" numpy arrays through fancy indexing:
(You could replace those calls to
np.arange
withnp.r_
if you wanted to be more concise. E.g. instead ofdata[np.arange(3)+4, np.arange(3)]
, usedata[np.r_[:3]+4, np.r_[:3]]
)This yields:
However, if you're going to be using sparse matrices anyway, have a look at
scipy.sparse.spdiags
. (Note that you'll need to prepend fake data onto your row values if you're placing data into a diagonal position with a positive value (e.g. the 3's in position 4 in the example))As a quick example:
This yields:
@TheCorwoodRep 的答案实际上可以在一行中完成。不需要单独的函数。
这会产生:
@TheCorwoodRep's answer can actually be done in a single line. No need for a seperate function.
This produces:
为了从三个单独的块构建分块三对角矩阵(并重复这些块 N 次),一种解决方案可以是:
例如:
给出答案
For building a block-wise tridiagonal matrix from the three individual blocks (and repeat the blocks for N times), one solution can be:
For example:
gives the answer
无论好坏,所有其他答案似乎都回答了三对角矩阵,而不是块三对角矩阵。
我认为没有对三对角矩阵的原生支持,所以我编写了自己的代码。我的主对角线上有零,并且我的矩阵是对称的。
这是我的代码。
希望这可以帮助未来寻找块三对角矩阵的人。
For better or worse, all the other answers seem to answer about tridiagonal matrices and not block tridiagonal matrices.
I don't think there is native support for tridiagonal matrices, so I wrote my own code. I had zeros on the main diagonal and my matrix was symmetric.
Here is my code.
Hopefully this can help future people looking for block tridiagonal matrices.
我的答案是基于@TheCorwoodRep 的答案。我只是发布它,因为我做了一些更改以使其更加模块化,以便它适用于不同阶的矩阵,并且还更改了
k1
、k2
的值,k3
即决定对角线出现的位置,将自动处理溢出问题。调用该函数时,您可以指定对角线上应显示的值。My answer builds of @TheCorwoodRep's answer. I am just posting it because I made a few changes to make it more modular so that it would work for different orders of matrices and also changing the values of
k1
,k2
,k3
i.e which decide where the diagonal appears, will take care of the overflow automatically. While calling the function you can specify what values should appear on the diagonals.由于三对角矩阵是一个稀疏矩阵,使用稀疏包可能是一个不错的选择,请参阅 http:// /pysparse.sourceforge.net/spmatrix.html#matlab-implementation,有一些示例以及与 MATLAB 的比较,甚至...
Since tridiagonal matrix is a sparse matrix using a sparse package could be a nice option, see http://pysparse.sourceforge.net/spmatrix.html#matlab-implementation, there are some examples and comparisons with MATLAB even...
我只需要类似的东西。我也分享我的解决方法。
I just needed something similar. I share my workaround too.