Skip to content

Commit 58a34bc

Browse files
Refactor project structure and simplify substitution methods
- Remove LU factorization, partial pivoting LU, and QR factorization files - Add default tolerance of 1e-10 to forward and back substitution methods - Update test cases to use default tolerance and `isapprox` - Simplify module imports and exports - Modify test runner to reflect new file structure
1 parent e43604d commit 58a34bc

16 files changed

+413
-299
lines changed

src/SimpleLinearAlgebra.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module SimpleLinearAlgebra
1616

1717
include("forward-substitution.jl")
1818
include("back-substitution.jl")
19-
include("lu-factorization.jl")
20-
include("partial-pivoting-lu-factorization.jl")
21-
include("qr-factorization.jl")
19+
include("lu.jl")
20+
include("qr.jl")
21+
22+
export kernel
2223
end

src/back-substitution.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct BackSubstitutionProblem <: ProblemMixin
33
b::AbstractVector
44
tol::Real
55

6-
function BackSubstitutionProblem(u, b, tol)
6+
function BackSubstitutionProblem(u, b, tol=1e-10)
77
prob = new(u, b, tol)
88
@assert istriu(u) "matrix must be upper triangular"
99
return prob
@@ -14,8 +14,6 @@ struct BackSubstitutionSolution <: SolutionMixin
1414
x::AbstractVector
1515
end
1616

17-
BackSubstitution = BackSubstitutionProblem
18-
1917
function kernel(prob::BackSubstitutionProblem)
2018
u = prob.u
2119
b = copy(prob.b)
@@ -39,4 +37,5 @@ function kernel(prob::BackSubstitutionProblem)
3937
return BackSubstitutionSolution(x)
4038
end
4139

42-
export BackSubstitution, kernel
40+
BackSubstitution = BackSubstitutionProblem
41+
export BackSubstitution

src/forward-substitution.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ struct ForwardSubstitutionProblem <: ProblemMixin
33
b::AbstractVector
44
tol::Real
55

6-
function ForwardSubstitutionProblem(l, b, tol)
6+
function ForwardSubstitutionProblem(l, b, tol=1e-10)
77
prob = new(l, b, tol)
88
@assert istril(l) "matrix must be lower triangular"
99
return prob
1010
end
1111
end
1212

13-
ForwardSubstitution = ForwardSubstitutionProblem
14-
1513
struct ForwardSubstitutionSolution <: SolutionMixin
1614
x::AbstractVector
1715
end
@@ -37,4 +35,5 @@ function kernel(prob::ForwardSubstitutionProblem)
3735
return ForwardSubstitutionSolution(x)
3836
end
3937

40-
export ForwardSubstitution, kernel
38+
ForwardSubstitution = ForwardSubstitutionProblem
39+
export ForwardSubstitution

src/lu-factorization.jl

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/lu.jl

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
abstract type LUFactorizationProblemMixin <: ProblemMixin end
2+
3+
module LU
4+
using LinearAlgebra
5+
import SimpleLinearAlgebra.LUFactorizationProblemMixin
6+
import SimpleLinearAlgebra.SolutionMixin
7+
8+
struct LUFactorizationSolution <: SolutionMixin
9+
l::AbstractMatrix
10+
u::AbstractMatrix
11+
function LUFactorizationSolution(l, u, p=nothing)
12+
@assert istril(l) "matrix must be lower triangular"
13+
@assert istriu(u) "matrix must be upper triangular"
14+
soln = new(l, u)
15+
return soln
16+
end
17+
end
18+
19+
struct Version1 <: LUFactorizationProblemMixin
20+
a::AbstractMatrix
21+
tol::Real
22+
function Version1(a, tol=1e-10)
23+
prob = new(a, tol)
24+
return prob
25+
end
26+
end
27+
28+
function kernel(prob::Version1)
29+
tol = prob.tol
30+
u = copy(prob.a)
31+
n = size(u, 1)
32+
33+
l = Matrix{Float64}(I, n, n)
34+
35+
for k in 1:n-1
36+
@assert abs(u[k, k]) > tol "Gaussian elimination failed"
37+
38+
m_k = Matrix{Float64}(I, n, n)
39+
m_k[k+1:n, k] = -u[k+1:n, k] / u[k, k]
40+
41+
l = l * inv(m_k)
42+
u = m_k * u
43+
end
44+
45+
soln = LUFactorizationSolution(tril(l), triu(u))
46+
return soln
47+
end
48+
49+
struct Version2 <: LUFactorizationProblemMixin
50+
a::AbstractMatrix
51+
tol::Real
52+
function Version2(a, tol=1e-10)
53+
prob = new(a, tol)
54+
return prob
55+
end
56+
end
57+
58+
function kernel(prob::Version2)
59+
tol = prob.tol
60+
u = copy(prob.a)
61+
n = size(u, 1)
62+
63+
# initialize l to be the identity matrix
64+
l = Matrix{Float64}(I, n, n)
65+
66+
for k in 1:n-1
67+
@assert abs(u[k, k]) > tol "Gaussian elimination failed"
68+
l[k+1:n, k] = u[k+1:n, k] / u[k, k]
69+
u[k+1:n, k+1:n] -= l[k+1:n, k] * u[k, k+1:n]'
70+
end
71+
72+
soln = LUFactorizationSolution(tril(l), triu(u))
73+
return soln
74+
end
75+
76+
struct PartialPivotingLUFactorizationSolution <: SolutionMixin
77+
l::AbstractMatrix
78+
u::AbstractMatrix
79+
p::AbstractVector
80+
function PartialPivotingLUFactorizationSolution(l, u, p)
81+
@assert istril(l) "matrix must be lower triangular"
82+
@assert istriu(u) "matrix must be upper triangular"
83+
@assert isperm(p) "permutation vector must be a permutation"
84+
soln = new(l, u, p)
85+
return soln
86+
end
87+
end
88+
89+
struct PartialPivoting <: LUFactorizationProblemMixin
90+
a::AbstractMatrix
91+
tol::Real
92+
function PartialPivoting(a, tol=1e-10)
93+
prob = new(a, tol)
94+
return prob
95+
end
96+
end
97+
98+
function kernel(prob::PartialPivoting)
99+
tol = prob.tol
100+
u = copy(prob.a)
101+
n = size(u, 1)
102+
103+
l = zeros(n, n)
104+
p = collect(1:n)
105+
106+
for k in 1:n-1
107+
v, x = findmax(i->abs(u[i, k]), k:n)
108+
x += k - 1
109+
110+
if x != k
111+
u[k, :], u[x, :] = u[x, :], u[k, :]
112+
l[k, :], l[x, :] = l[x, :], l[k, :]
113+
p[k], p[x] = p[x], p[k]
114+
end
115+
116+
if abs(u[k, k]) < tol
117+
continue
118+
end
119+
120+
l[k, k] = 1.0
121+
l[k+1:n, k] = u[k+1:n, k] / u[k, k]
122+
u[k+1:n, k+1:n] -= l[k+1:n, k] * u[k, k+1:n]'
123+
end
124+
125+
l[n, n] = 1.0
126+
soln = PartialPivotingLUFactorizationSolution(tril(l), triu(u), p)
127+
return soln
128+
end
129+
end
130+
131+
kernel(prob::LUFactorizationProblemMixin) = LU.kernel(prob)
132+
133+
LUFactorization = LU.Version2
134+
export LUFactorization

src/partial-pivoting-lu-factorization.jl

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/qr-factorization.jl

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)