Skip to content

Commit 24744af

Browse files
Add LU factorization implementation
- Include LU factorization module in SimpleLinearAlgebra - Update Makefile to use wildcard patterns for source and test files - Add LU factorization test suite to test runner
1 parent 3fed097 commit 24744af

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
jl:=julia
22

3-
init: src/SimpleLinearAlgebra.jl
3+
init: src/*jl
44
$(jl) -e 'd=pwd(); @assert isdir(d);\
55
using Pkg: activate, instantiate, develop, precompile; \
66
activate(d); instantiate(); activate(joinpath(d, "examples")); \
77
develop(path=d); instantiate(); precompile();'
88
@echo "Environment initialized at: $$PWD"
99

10-
update: src/SimpleLinearAlgebra.jl
10+
update: src/*jl
1111
$(jl) -e 'd=pwd(); @assert isdir(d);\
1212
using Pkg: activate, update, develop, precompile; \
1313
activate(d); update(); activate(joinpath(d, "examples")); \
1414
update(); precompile();'
1515
@echo "Environment updated at: $$PWD"
1616

17-
test: test/runtests.jl
17+
test: test/*jl
1818
@echo "Running tests at: $$PWD"
1919
$(jl) -e 'd=pwd(); @assert isdir(d);\
2020
using Pkg: activate, test; \

src/SimpleLinearAlgebra.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ module SimpleLinearAlgebra
1111

1212
include("forward_substitution.jl")
1313
include("back_substitution.jl")
14+
include("lufact.jl")
1415
end

src/lufact.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
struct LUFactorizationProblem <: LinearSystemProblemMixin
2+
a::AbstractMatrix
3+
tol::Real
4+
end
5+
6+
struct LUFactorizationSolution <: LinearSystemSolutionMixin
7+
l::AbstractMatrix
8+
u::AbstractMatrix
9+
10+
function LUFactorizationSolution(l, u)
11+
if !istril(l)
12+
ArgumentError("Matrix must be lower triangular") |> throw
13+
end
14+
15+
if !istriu(u)
16+
ArgumentError("Matrix must be upper triangular") |> throw
17+
end
18+
19+
return new(l, u)
20+
end
21+
end
22+
23+
LUFactorization = LUFactorizationProblem
24+
25+
function elemination_step(a::AbstractMatrix{T}, k::Integer) where T
26+
# given a matrix A, perform the k-th elimination step.
27+
# M A = N, which M is a lower triangular matrix with ones on the diagonal,
28+
# and N is the matrix A with the k-th column eliminated.
29+
# The inverse of M is simply given by Minv = 2I - m
30+
31+
n = size(a, 1)
32+
@assert size(a, 2) == n
33+
@assert 1 <= k <= n-1
34+
35+
m = identity_matrix(T, n)
36+
for i in k+1:n
37+
m[i, k] = -a[i, k] / a[k, k]
38+
end
39+
return m
40+
end
41+
42+
function kernel(prob::LUFactorizationProblem)
43+
u = copy(prob.a)
44+
n = size(u, 1)
45+
@assert size(u, 2) == n
46+
47+
tol = prob.tol
48+
49+
l = zero(u)
50+
51+
l[1:n+1:end] .= 1
52+
53+
for k in 1:n-1
54+
55+
if abs(u[k, k]) < tol
56+
ArgumentError("Gaussian elimination failed") |> throw
57+
end
58+
59+
for i=k+1:n
60+
l[i, k] = u[i, k] / u[k, k]
61+
end
62+
63+
for j in k+1:n
64+
for i in k+1:n
65+
u[i, j] -= l[i, k] * u[k, j]
66+
end
67+
end
68+
end
69+
70+
return LUFactorizationSolution(l, triu(u))
71+
end
72+
73+
export LUFactorization, kernel

test/lufact.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@testset "LU factorization" begin
2+
n = 10
3+
tol = 1e-10
4+
a = rand(n, n)
5+
6+
p = LUFactorization(a, tol)
7+
s = kernel(p)
8+
l = s.l
9+
u = s.u
10+
11+
@test istril(l)
12+
@test istriu(u)
13+
@test maximum(abs, a - l * u) < tol
14+
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ end
88
@testset "backward substitution" begin
99
include("backward_substitution.jl")
1010
end
11+
12+
@testset "LU factorization" begin
13+
include("lufact.jl")
14+
end

0 commit comments

Comments
 (0)