分块矩阵的逆

这篇具有很好参考价值的文章主要介绍了分块矩阵的逆。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

分块矩阵怎么求逆? - 知乎四分块矩阵求逆是有通式的(参见分块矩阵求逆公式)下面是一些分块矩阵求逆公式:另可参考Block matrix o…https://www.zhihu.com/question/47760591Matrix inversion identities - 知乎Recently I reviewed some tricks in obtaining the inversion of the matrices. An efficient technique to obtain the inversion of a matrix makes use of Schur's Complement. In China, this trick is al…https://zhuanlan.zhihu.com/p/157436207分块矩阵的逆

 分块矩阵的逆

已经验证了上面两个公式是正确的。

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from time import time
from numpy.linalg import inv

X, y = datasets.load_iris(return_X_y=True)

A = rbf_kernel(X=X[0:5],gamma=0.1) + 0.1 * np.eye(5)
B = rbf_kernel(X=X[0:5],Y=[X[5]],gamma=0.1)
C = B.T
D = rbf_kernel(X=[X[5]],Y=[X[5]],gamma=0.1) + 0.1
# print(A.shape)
# print(B.shape)
# print(C.shape)
# print(D.shape)

K = rbf_kernel(X=X[0:6],gamma=0.1) + 0.1 * np.eye(6)
print("直接")
print(np.linalg.inv(K))
# S_time = time()
# for i in range(10000):
#     AAi = np.linalg.inv(K)
# print("Time_1::",time()-S_time)

Ai = np.linalg.inv(A)
S_time = time()
average_time = []
for i in range(10000):
    n = A.shape[0]
    a1 = np.concatenate((np.eye(n),-Ai@B),axis=1)
    b1 = np.concatenate((np.zeros((1,n)),np.eye(1)),axis=1)
    M1 = np.concatenate((a1,b1),axis=0)
    # print("M1:",M1.shape)

    a2 = np.concatenate((Ai,np.zeros((n,1))),axis=1)
    b2 = np.concatenate((np.zeros((1,n)),np.linalg.inv(D-C@Ai@B)),axis=1)
    M2 = np.concatenate((a2,b2),axis=0)
    # print("M2:",M2.shape)

    a3 = np.concatenate((np.eye(n),np.zeros((n,1))),axis=1)
    b3 = np.concatenate((-C@Ai,np.eye(1)),axis=1)
    M3 = np.concatenate((a3,b3),axis=0)
    # print("M2:",M3.shape)

    s_time = time()
    AAi = M1@M2@M3
    e_time = time()
    cost_time = e_time - s_time
    average_time.append(cost_time)
    # print(M1@M2@M3)
# print("Time_2::",time()-S_time)
print("Time_2::",np.mean(average_time))

DCAB = inv(D-C@Ai@B)
# for i in range(10000):
a = Ai+ Ai@B@DCAB@C@Ai
b = -Ai@B@DCAB
c = -DCAB@C@Ai
d = DCAB
M1 = np.concatenate((a,b),axis=1)
M2 = np.concatenate((c,d),axis=1)
M = np.concatenate((M1,M2),axis=0)
print("M:")
print(M)



import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from time import time
from numpy.linalg import inv

X, y = datasets.load_iris(return_X_y=True)

A = rbf_kernel(X=X[0:100],gamma=0.1) + 0.1 * np.eye(100)
B = rbf_kernel(X=X[0:100],Y=[X[100]],gamma=0.1)
C = B.T
D = rbf_kernel(X=[X[100]],Y=[X[100]],gamma=0.1) + 0.1


K = rbf_kernel(X=X[0:101],gamma=0.1) + 0.1 * np.eye(101)
# print("直接")
# print(np.linalg.inv(K))
S_time = time()
for i in range(1000):
    AAi = np.linalg.inv(K)
print("Time_1::",time()-S_time)

Ai = np.linalg.inv(A)
# S_time = time()
# # average_time = []
# for i in range(1000):
#     n = A.shape[0]
#     a1 = np.concatenate((np.eye(n),-Ai@B),axis=1)
#     b1 = np.concatenate((np.zeros((1,n)),np.eye(1)),axis=1)
#     M1 = np.concatenate((a1,b1),axis=0)
#     # print("M1:",M1.shape)
#
#     a2 = np.concatenate((Ai,np.zeros((n,1))),axis=1)
#     b2 = np.concatenate((np.zeros((1,n)),np.linalg.inv(D-C@Ai@B)),axis=1)
#     M2 = np.concatenate((a2,b2),axis=0)
#     # print("M2:",M2.shape)
#
#     a3 = np.concatenate((np.eye(n),np.zeros((n,1))),axis=1)
#     b3 = np.concatenate((-C@Ai,np.eye(1)),axis=1)
#     M3 = np.concatenate((a3,b3),axis=0)
#     # print("M2:",M3.shape)
#
#     s_time = time()
#     AAi = M1@M2@M3
#     e_time = time()
#     cost_time = e_time - s_time
#     # average_time.append(cost_time)
#     # print(M1@M2@M3)
# print("Time_2::",time()-S_time)
# # print("Time_2::",np.mean(average_time))

DCAB = inv(D-C@Ai@B)
S_time = time()
for i in range(1000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M = np.concatenate((np.concatenate((a,b),axis=1),np.concatenate((c,d),axis=1)),axis=0)
print("Time_3::",time()-S_time)

S_time = time()
for i in range(1000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M1 = np.concatenate((a,b),axis=1)
    M2 = np.concatenate((c,d),axis=1)
    M = np.concatenate((M1,M2),axis=0)
print("Time_4::",time()-S_time)




使用分块矩阵计算缺失节约了大量的计算代价(前提A的逆已知)。

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from time import time
from numpy.linalg import inv

X, y = datasets.load_iris(return_X_y=True)

A = rbf_kernel(X=X[0:5],gamma=0.1) + 0.1 * np.eye(5)
B = rbf_kernel(X=X[0:5],Y=[X[5]],gamma=0.1)
C = B.T
D = rbf_kernel(X=[X[5]],Y=[X[5]],gamma=0.1) + 0.1


K = rbf_kernel(X=X[0:6],gamma=0.1) + 0.1 * np.eye(6)
print("直接")
print(np.linalg.inv(K))
S_time = time()
for i in range(10000):
    AAi = np.linalg.inv(K)
print("Time_1::",time()-S_time)

n = A.shape[0]
Ai = np.linalg.inv(A)
# S_time = time()
# # average_time = []
# for i in range(1000):
#     n = A.shape[0]
#     a1 = np.concatenate((np.eye(n),-Ai@B),axis=1)
#     b1 = np.concatenate((np.zeros((1,n)),np.eye(1)),axis=1)
#     M1 = np.concatenate((a1,b1),axis=0)
#     # print("M1:",M1.shape)
#
#     a2 = np.concatenate((Ai,np.zeros((n,1))),axis=1)
#     b2 = np.concatenate((np.zeros((1,n)),np.linalg.inv(D-C@Ai@B)),axis=1)
#     M2 = np.concatenate((a2,b2),axis=0)
#     # print("M2:",M2.shape)
#
#     a3 = np.concatenate((np.eye(n),np.zeros((n,1))),axis=1)
#     b3 = np.concatenate((-C@Ai,np.eye(1)),axis=1)
#     M3 = np.concatenate((a3,b3),axis=0)
#     # print("M2:",M3.shape)
#
#     s_time = time()
#     AAi = M1@M2@M3
#     e_time = time()
#     cost_time = e_time - s_time
#     # average_time.append(cost_time)
#     # print(M1@M2@M3)
# print("Time_2::",time()-S_time)
# # print("Time_2::",np.mean(average_time))

DCAB = inv(D-C@Ai@B)
S_time = time()
for i in range(10000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M = np.concatenate((np.concatenate((a,b),axis=1),np.concatenate((c,d),axis=1)),axis=0)
print("Time_3::",time()-S_time)

S_time = time()
for i in range(10000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M1 = np.concatenate((a,b),axis=1)
    M2 = np.concatenate((c,d),axis=1)
    M = np.concatenate((M1,M2),axis=0)
print("Time_4::",time()-S_time)

S_time = time()
for i in range(10000):
    M = np.zeros((n+1,n+1))
    M[:n,:n] = Ai+ Ai@B@DCAB@C@Ai
    # print(M[:n,n].shape)
    M[:n,n] = -Ai@B@DCAB.reshape(1)
    M[n,:n] = -DCAB@C@Ai
    M[n,n] = DCAB
    # a = Ai+ Ai@B@DCAB@C@Ai
    # b = -Ai@B@DCAB
    # c = -DCAB@C@Ai
    # d = DCAB
    # M1 = np.concatenate((a,b),axis=1)
    # M2 = np.concatenate((c,d),axis=1)
    # M = np.concatenate((M1,M2),axis=0)
print("Time_5::",time()-S_time)

M = np.zeros((n+1,n+1))
M[:n,:n] = Ai+ Ai@B@DCAB@C@Ai
M[:n,n] = -Ai@B@DCAB.reshape(1)
M[n,:n] = -DCAB@C@Ai
M[n,n] = DCAB
print(M)




时间对比(样本数加到100): 

分块矩阵的逆

 最后一种写法最节约时间。文章来源地址https://www.toymoban.com/news/detail-456180.html

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.metrics.pairwise import rbf_kernel
from time import time
from numpy.linalg import inv

X, y = datasets.load_iris(return_X_y=True)

A = rbf_kernel(X=X[0:100],gamma=0.1) + 0.1 * np.eye(100)
B = rbf_kernel(X=X[0:100],Y=[X[100]],gamma=0.1)
C = B.T
D = rbf_kernel(X=[X[100]],Y=[X[100]],gamma=0.1) + 0.1


K = rbf_kernel(X=X[0:101],gamma=0.1) + 0.1 * np.eye(101)
# print("直接")
# print(np.linalg.inv(K))
S_time = time()
for i in range(10000):
    AAi = np.linalg.inv(K)
print("Time_1::",time()-S_time)

n = A.shape[0]
Ai = np.linalg.inv(A)
# S_time = time()
# # average_time = []
# for i in range(1000):
#     n = A.shape[0]
#     a1 = np.concatenate((np.eye(n),-Ai@B),axis=1)
#     b1 = np.concatenate((np.zeros((1,n)),np.eye(1)),axis=1)
#     M1 = np.concatenate((a1,b1),axis=0)
#     # print("M1:",M1.shape)
#
#     a2 = np.concatenate((Ai,np.zeros((n,1))),axis=1)
#     b2 = np.concatenate((np.zeros((1,n)),np.linalg.inv(D-C@Ai@B)),axis=1)
#     M2 = np.concatenate((a2,b2),axis=0)
#     # print("M2:",M2.shape)
#
#     a3 = np.concatenate((np.eye(n),np.zeros((n,1))),axis=1)
#     b3 = np.concatenate((-C@Ai,np.eye(1)),axis=1)
#     M3 = np.concatenate((a3,b3),axis=0)
#     # print("M2:",M3.shape)
#
#     s_time = time()
#     AAi = M1@M2@M3
#     e_time = time()
#     cost_time = e_time - s_time
#     # average_time.append(cost_time)
#     # print(M1@M2@M3)
# print("Time_2::",time()-S_time)
# # print("Time_2::",np.mean(average_time))

DCAB = inv(D-C@Ai@B)
S_time = time()
for i in range(10000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M = np.concatenate((np.concatenate((a,b),axis=1),np.concatenate((c,d),axis=1)),axis=0)
print("Time_3::",time()-S_time)

S_time = time()
for i in range(10000):
    a = Ai+ Ai@B@DCAB@C@Ai
    b = -Ai@B@DCAB
    c = -DCAB@C@Ai
    d = DCAB
    M1 = np.concatenate((a,b),axis=1)
    M2 = np.concatenate((c,d),axis=1)
    M = np.concatenate((M1,M2),axis=0)
print("Time_4::",time()-S_time)

S_time = time()
for i in range(10000):
    M = np.zeros((n+1,n+1))
    M[:n,:n] = Ai+ Ai@B@DCAB@C@Ai
    # print(M[:n,n].shape)
    M[:n,n] = -Ai@B@DCAB.reshape(1)
    M[n,:n] = -DCAB@C@Ai
    M[n,n] = DCAB
    # a = Ai+ Ai@B@DCAB@C@Ai
    # b = -Ai@B@DCAB
    # c = -DCAB@C@Ai
    # d = DCAB
    # M1 = np.concatenate((a,b),axis=1)
    # M2 = np.concatenate((c,d),axis=1)
    # M = np.concatenate((M1,M2),axis=0)
print("Time_5::",time()-S_time)






到了这里,关于分块矩阵的逆的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【例题】利用伴随矩阵求逆矩阵

    【例1:同济线代习题二 9.1】求下列矩阵的逆矩阵: A = ( 1 2 2 5 ) boldsymbol{A} = begin{pmatrix} 1 2 \\\\ 2 5 end{pmatrix} A = ( 1 2 ​ 2 5 ​ ) 解答 因为 ∣ A ∣ = 5 − 4 = 1 ≠ 0 |boldsymbol{A}| = 5 - 4 = 1 ne 0 ∣ A ∣ = 5 − 4 = 1  = 0 ,所以 A boldsymbol{A} A 可逆。有 A − 1 = 1 ∣ A ∣ A ∗ = ( 5 − 2 −

    2023年04月11日
    浏览(41)
  • 3.3 伴随矩阵法求逆矩阵

      逆矩阵指的是另一个矩阵和自己相乘会变成单位矩阵,符号是右上角一个 − 1 -1 − 1 ,就是: A A − 1 = A − 1 A = I AA^{-1}=A^{-1}A=I A A − 1 = A − 1 A = I   例如以下两个矩阵就是互为逆矩阵: ( − 1 1 0 0 − 3 2 1 0 1 1 0 − 1 4 − 4 − 1 1 ) ( 1 1 1 1 2 1 1 1 − 1 2 1 1 3 2 1 2 ) = ( 1 0

    2024年02月09日
    浏览(52)
  • Matlab求矩阵的逆(三种方法)

    说明:若所求矩阵为非奇异矩阵(可逆矩阵),则可以精确求得其逆矩阵;若所求矩阵为奇异矩阵,则所求出的逆矩阵是近似的(不精确)。 下面以矩阵A为例。 inv()方法 A − 1 A^{-1} A − 1 方法 eye()/A方法 示例: 示例: eye(N):返回N*N的单位矩阵 eye(M,N):返回M*N的单位矩阵 示例:

    2024年02月11日
    浏览(52)
  • 科学计算器如何求矩阵的逆

    大学本科买了四年的计算器不会求逆,到了研究生了好好研究下这个功能,终于终于会用了,以往 对着那个矩阵功能都发懵😂,记录一下这个史诗无敌隐藏功能 要是早知道本科线代就轻松了,不过现在知道也不晚,起码还有个高工考试hhh

    2023年04月21日
    浏览(30)
  • C语言求矩阵的逆(伴随法)

    之前介绍了C语言用代数余子式求行列式 本次开始介绍如何用公式法对矩阵求逆,并用C语言将其实现。 之前程序有点小bug,已于2022年11月29日修改。 更新:          伴随法只适合求低阶矩阵的逆,对于相对高阶(20维以上)对矩阵求逆用高斯法求解效率更高,此外本文中

    2024年02月02日
    浏览(44)
  • R语言矩阵、向量操作(矩阵乘法,向量内积、外积(叉乘),矩阵转置,矩阵的逆)

    创建两个四维矩阵 A 与 B,A 按列填充,B 按行填充 : 创建两个 n 维向量 x 和 y : 使用 t(矩阵、向量名) 即可: 输出如下: 使用 %*% 符号即可: 输出如下: 在R语言中,两个矩阵、向量的内积并不只是简单的 * 号就完事了,而是有以下两种求法: 或者 其结果如下: (注意区分

    2024年02月12日
    浏览(32)
  • 线性代数——求逆矩阵

    利用计算技巧凑出公式:两边加E、提取公因式、没有公因式可提时利用隐形的E=AA^(-1),因为E可看作系数1 主对角线有矩阵(副对角线是0矩阵),则分别逆后放在原位置 副对角线有矩阵(主对角线是0矩阵),则分别逆后互换位置

    2024年02月11日
    浏览(52)
  • Maple矩阵求逆

    如何使用Maple进行矩阵求逆 调用包 输入我想要的矩阵: 然后我使用了Inverse命令 结果并没有生成矩阵的逆。 查了一下说明手册加上 mod 好像也不行,而且手册的mod的参数我也没有看懂。 上网查了一下解决方法: 改用命令 MatrixInverse 就可以了。 完美解决!

    2024年02月11日
    浏览(82)
  • 线性代数|例题:利用伴随矩阵求逆矩阵

    【例1:同济线代习题二 9.1】求下列矩阵的逆矩阵: A = ( 1 2 2 5 ) boldsymbol{A} = begin{pmatrix} 1 2 \\\\ 2 5 end{pmatrix} A = ( 1 2 ​ 2 5 ​ ) 解答 因为 ∣ A ∣ = 5 − 4 = 1 ≠ 0 |boldsymbol{A}| = 5 - 4 = 1 ne 0 ∣ A ∣ = 5 − 4 = 1  = 0 ,所以 A boldsymbol{A} A 可逆。有 A − 1 = 1 ∣ A ∣ A ∗ = ( 5 − 2 −

    2024年02月08日
    浏览(40)
  • 线代——求逆矩阵的快捷方法

    通常,求逆矩阵有两种方法: 方法一: 方法二: 但是,对于 特殊矩阵 ,如: A = [ a b c d ] A = begin{bmatrix} a b\\\\ c d end{bmatrix} A = [ a c ​ b d ​ ] ,其逆矩阵 A − 1 = 1 a d − b c [ d − b − c a ] A^{-1}=frac{1}{ad-bc}begin{bmatrix} d -b\\\\ -c a end{bmatrix} A − 1 = a d − b c 1 ​ [ d − c ​ − b a

    2023年04月08日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包