Skip to content

SawyDust1228/Fast-Matrix-Inverse-in-Pytorch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

本项目是Updating Inverse of a Matrix When a Column is Added/Removedpytorch版本源码

Function

Arguments

$$ X_0:X_0\ is\ new\ matrix\ [x\ v] \col:col\space is\space a\space index\space list\\ B : B\space is\space inverse \space of \space X \space with \ columns \space indexed \space by \space col\\ posX_0:posX_0\ is\ a\ index\ which\ the\ column\ to\ add\ or\ delete\\ add:add\ is\ a\ flag,\ True\ is\ add\ operation,\ False\ is\ delete\ operation $$

Result

Add Column

输入向量为一个正太分布随机的$(5, 5)$向量

a:
tensor([[ 0.6698, -0.0029,  0.2328, -0.5085, -0.8004],
        [ 0.0579,  0.7822, -0.9465,  0.7820, -0.2720],
        [-0.5849, -0.1487,  1.9924, -1.7099, -1.0297],
        [ 1.4416,  0.4577,  0.1933,  0.3135,  0.4863],
        [ 0.3968, -2.1672,  1.5479,  1.6054, -0.5875]])

我们定义矩阵的$inverse$如下:

$$ M.inverse() = (M^TM)^{-1} $$ 则我们有

a.inverse()
tensor([[ 0.5550, -0.4395, -0.3777, -0.4035, -0.1025],
        [-0.4395,  1.3245,  0.9747,  0.8849,  0.0074],
        [-0.3777,  0.9747,  0.9735,  0.6801,  0.3070],
        [-0.4035,  0.8849,  0.6801,  0.7912, -0.0355],
        [-0.1025,  0.0074,  0.3070, -0.0355,  0.8241]])

我们添加一个向量$v$到最后一列

v:
tensor([[-0.2405],
        [-0.2790],
        [-0.4025],
        [-0.6275],
        [-0.1568]])
a_add:
tensor([[ 0.6698, -0.0029,  0.2328, -0.5085, -0.8004, -0.2405],
        [ 0.0579,  0.7822, -0.9465,  0.7820, -0.2720, -0.2790],
        [-0.5849, -0.1487,  1.9924, -1.7099, -1.0297, -0.4025],
        [ 1.4416,  0.4577,  0.1933,  0.3135,  0.4863, -0.6275],
        [ 0.3968, -2.1672,  1.5479,  1.6054, -0.5875, -0.1568]])
a_add.inverse():
tensor([[ -232145.0625,  -621442.9375,  -514946.7188,  -321315.5312, 206630.1250, -1145618.3750],
        [ -621443.1250, -1663570.3750, -1378485.5000,  -860144.1875, 553139.1875, -3066765.2500],
        [ -514946.7500, -1378485.5000, -1142255.2500,  -712741.8750, 458348.3750, -2541215.5000],
        [ -321315.6250,  -860144.3750,  -712742.0625,  -444734.9062, 285999.0938, -1585662.6250],
        [  206630.1875,   553139.1875,   458348.4375,   285999.0625, -183918.5156,  1019702.5000],
        [-1145618.5000, -3066765.0000, -2541215.5000, -1585662.2500, 1019702.3125, -5653527.5000]])

通过调用函数$OneColInv$, 我们得到如下结果:

result:
tensor([[-2720239.7500, -2720240.7500, -2720240.7500, -2720240.7500, -2720240.2500,  -849924.6875],
        [-2720240.7500, -2720239.0000, -2720239.2500, -2720239.2500, -2720240.2500, -2275206.7500],
        [-2720240.7500, -2720239.2500, -2720239.2500, -2720239.5000, -2720240.0000, -1885306.2500],
        [-2720240.7500, -2720239.2500, -2720239.5000, -2720239.5000, -2720240.2500, -1176389.6250],
        [-2720240.2500, -2720240.2500, -2720240.0000, -2720240.2500, -2720239.5000,   756508.5625],
        [ -849924.6875, -2275206.7500, -1885306.2500, -1176389.6250, 756508.5625, -4194304.0000]])

Delete Column

a:
tensor([[ 0.6698, -0.0029,  0.2328, -0.5085, -0.8004],
        [ 0.0579,  0.7822, -0.9465,  0.7820, -0.2720],
        [-0.5849, -0.1487,  1.9924, -1.7099, -1.0297],
        [ 1.4416,  0.4577,  0.1933,  0.3135,  0.4863],
        [ 0.3968, -2.1672,  1.5479,  1.6054, -0.5875]])
a.inverse():
tensor([[ 0.5550, -0.4395, -0.3777, -0.4035, -0.1025],
        [-0.4395,  1.3245,  0.9747,  0.8849,  0.0074],
        [-0.3777,  0.9747,  0.9735,  0.6801,  0.3070],
        [-0.4035,  0.8849,  0.6801,  0.7912, -0.0355],
        [-0.1025,  0.0074,  0.3070, -0.0355,  0.8241]])

我们删除最后一列

a_del : 
tensor([[ 0.6698, -0.0029,  0.2328, -0.5085],
        [ 0.0579,  0.7822, -0.9465,  0.7820],
        [-0.5849, -0.1487,  1.9924, -1.7099],
        [ 1.4416,  0.4577,  0.1933,  0.3135],
        [ 0.3968, -2.1672,  1.5479,  1.6054]])
a_del.inverse():
tensor([[ 0.5422, -0.4386, -0.3395, -0.4079],
        [-0.4386,  1.3244,  0.9719,  0.8853],
        [-0.3395,  0.9719,  0.8591,  0.6933],
        [-0.4079,  0.8853,  0.6933,  0.7897]])

我们调用$OneColInv$函数, 得到删除最后一列后的结果

result:
tensor([[ 0.4263, -0.5682, -0.5064, -0.5322],
        [-0.5682,  1.1957,  0.8460,  0.7562],
        [-0.5064,  0.8460,  0.8448,  0.5514],
        [-0.5322,  0.7562,  0.5514,  0.6625]])

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published