Let  be a square matrix of size  that is invertible, and let  and   be non-zero column vectors of size  . The matrix  is invertible if and only if the equation is true. In such a case, the inverse of   can be determined using the following method:
The above formula can be coded in Julia as
using LinearAlgebra
N = 4
A = rand(N,N)
Ainv = inv(A)
println("A = ")
display(Ainv)
u = rand(N,1)
v = rand(N,1)
B = A + u*v'
println("A + u*v' = ")
display(B)
Binv = inv(B)
if !isequal(-1,dot(v,Ainv*u))
C = Ainv - Ainv*u*v'*Ainv/(1 + dot(v,Ainv*u))
end
println("Crude Inverse")
display(Binv)
println("Inverse calculated with Matrix Inversion Lemma")
display(C)
println("Are the matrices equal")
isapprox(Binv,C)
Top comments (0)