Skip to content

Commit 918cb57

Browse files
leiosjiegillet
authored andcommitted
updating gaussian elimination to be more general (#219)
* Revamping Gaussian Elimination Chapter to have both Gauss-Jordan elimination and back-substitution. * updating Gaussian Elimination code with new Gauss-Jordan method
1 parent 5c6c52d commit 918cb57

File tree

2 files changed

+228
-45
lines changed

2 files changed

+228
-45
lines changed

chapters/algorithms/gaussian_elimination/code/julia/gaussian_elimination.jl

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,44 @@ function gaussian_elimination(A::Array{Float64,2})
33
rows = size(A,1)
44
cols = size(A,2)
55

6+
# Row index
7+
row = 1
8+
69
# Main loop going through all columns
7-
for k = 1:min(rows,cols)
10+
for col = 1:(cols-1)
811

912
# Step 1: finding the maximum element for each column
10-
max_index = indmax(abs.(A[k:end,k])) + k-1
13+
max_index = indmax(abs.(A[row:end,col])) + row-1
1114

1215
# Check to make sure matrix is good!
13-
if (A[max_index, k] == 0)
14-
println("matrix is singular! End!")
15-
exit(0)
16+
if (A[max_index, col] == 0)
17+
println("matrix is singular!")
18+
continue
1619
end
1720

1821
# Step 2: swap row with highest value for that column to the top
1922
temp_vector = A[max_index, :]
20-
A[max_index, :] = A[k, :]
21-
A[k, :] = temp_vector
22-
#println(A)
23+
A[max_index, :] = A[row, :]
24+
A[row, :] = temp_vector
2325

2426
# Loop for all remaining rows
25-
for i = (k+1):rows
27+
for i = (row+1):rows
2628

2729
# Step 3: finding fraction
28-
fraction = A[i,k]/A[k,k]
30+
fraction = A[i,col]/A[row,col]
2931

3032
# loop through all columns for that row
31-
for j = (k+1):cols
33+
for j = (col+1):cols
3234

3335
# Step 4: re-evaluate each element
34-
A[i,j] -= A[k,j]*fraction
36+
A[i,j] -= A[row,j]*fraction
3537

3638
end
3739

3840
# Step 5: Set lower elements to 0
39-
A[i,k] = 0
41+
A[i,col] = 0
4042
end
43+
row += 1
4144
end
4245
end
4346

@@ -63,18 +66,51 @@ function back_substitution(A::Array{Float64,2})
6366
return soln
6467
end
6568

69+
70+
function gauss_jordan(A::Array{Float64,2})
71+
72+
rows = size(A,1)
73+
cols = size(A,2)
74+
75+
76+
# After this, we know what row to start on (r-1)
77+
# to go back through the matrix
78+
row = 1
79+
for col = 1:cols-1
80+
if (A[row, col] != 0)
81+
82+
# divide row by pivot and leaving pivot as 1
83+
for i = cols:-1:col
84+
A[row,i] /= A[row,col]
85+
end
86+
87+
# subtract value from above row and set values above pivot to 0
88+
for i = 1:row-1
89+
for j = cols:-1:col
90+
A[i,j] -= A[i,col]*A[row,j]
91+
end
92+
end
93+
row += 1
94+
end
95+
end
96+
97+
return A
98+
end
99+
66100
function main()
67101
A = [2. 3 4 6;
68102
1 2 3 4;
69103
3 -4 0 10]
70104

71105
gaussian_elimination(A)
72106
println(A)
107+
108+
reduced = gauss_jordan(A)
109+
println(reduced)
110+
73111
soln = back_substitution(A)
112+
println(soln)
74113

75-
for element in soln
76-
println(element)
77-
end
78114
end
79115

80116
main()

0 commit comments

Comments
 (0)