Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added LU decomposition. #11

Merged
merged 4 commits into from
Jun 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions matrix/lu_decompose.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using LinearAlgebra # to test against the inbuilt function
using Test

"""
Decomposes a `n x n` non singular matrix into a lower triangular matrix (L) and an upper triangular matrix (U)
"""
function lu_decompose(mat)
n = mat |> size |> first
L = zeros(n,n)
U = zeros(n,n)

for i in 1:n
for j in i:n
s = 0
for k in 1:i
s += L[i,k] * U[k,j]
end
U[i,j] = mat[i,j] - s
end

for k in i:n
if i == k
L[i,i] = 1
else
s = 0
for j in 1:i
s += L[k,j] * U[j,i]
end
L[k,i] = (mat[k,i] - s) / U[i,i]
end
end
end

return L,U
end

mat = [
2 -1 -2;
-4 6 3;
-4 -2 8
]

L = [
1 0 0;
-2 1 0;
-2 -1 1
]
U = [
2 -1 -2;
0 4 -1;
0 0 3
]

@testset "LU decomposition" begin
@test lu_decompose(mat) == (L,U)
end