Skip to content

Commit 791628c

Browse files
committed
add gaussjordan for steps of a matrix inversion
1 parent d4885ab commit 791628c

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
### 0.2.4 (Upcoming Release)
1+
### 0.2.5 (Upcoming Release)
2+
3+
4+
### 0.2.4
25

36
- Rename `mmethodcorrection()` to `mmethodcorrection!()` just because the function mutates its input.
47
- Add documentation for `simplexiterations`.
5-
- Add additional tests for Simplex
8+
- Add additional tests for Simplex.
9+
- Add `gaussjordan()` method to calculate the inverse of a matrix with iteration steps.
610

711

812
### 0.2.3

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OperationsResearchModels"
22
uuid = "8042aa49-e599-49ec-a8f7-b5b80cc82a88"
33
authors = ["Mehmet Hakan Satman <mhsatman@gmail.com>"]
4-
version = "0.2.3"
4+
version = "0.2.4"
55

66
[deps]
77
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"

docs/src/algorithms.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ OperationsResearchModels.travelingsalesman
8787
OperationsResearchModels.createsimplexproblem
8888
```
8989

90+
### Gauss Jordan steps for matrix inversion
91+
```@docs
92+
OperationsResearchModels.gaussjordan
93+
```
94+

src/OperationsResearchModels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import .Latex: latex
5858
import .Johnsons: JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
5959
import .RandomKeyGA: Chromosome, run_ga
6060
import .TravelingSalesman: TravelinSalesmenResult, travelingsalesman
61-
import .Simplex: SimplexProblem, simplexiterations, createsimplexproblem
61+
import .Simplex: SimplexProblem, simplexiterations, createsimplexproblem, gaussjordan
6262

6363
export TransportationProblem, TransportationResult, balance, isbalanced, northwestcorner
6464
export Connection, ShortestPathResult, MaximumFlowResult, nodes
@@ -76,7 +76,7 @@ export latex
7676
export Chromosome, run_ga
7777
export JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
7878
export TravelinSalesmenResult, travelingsalesman
79-
export simplexiterations, SimplexProblem, createsimplexproblem
79+
export simplexiterations, SimplexProblem, createsimplexproblem, gaussjordan
8080

8181
export JuMP, HiGHS
8282

src/simplex.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export simplexiterations
1212
export simplexpretty
1313
export createsimplexproblem
1414
export solve!
15+
export gaussjordan
1516

1617
import ..Utility: numround
1718

@@ -545,5 +546,52 @@ function createsimplexproblem(
545546
return s
546547
end
547548

549+
"""
550+
gaussjordan(A::Matrix; verbose::Bool = true)::Matrix
551+
552+
Description:
553+
554+
Attaches an Identity matrix to the right of the given matrix A and applies the Gauss-Jordan elimination method to find the inverse of the given matrix.
555+
556+
Arguments:
557+
558+
- A::Matrix: The matrix to find the inverse.
559+
- verbose::Bool: If true, the intermediate steps are displayed. Default is true.
560+
561+
Returns:
562+
563+
The inverse of the given matrix.
564+
565+
Example:
566+
567+
```julia
568+
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 10.0]
569+
julia> invA = gaussjordan(A, verbose = false)
570+
3×3 Matrix{Float64}:
571+
-0.666667 -1.33333 1.0
572+
-0.666667 3.66667 -2.0
573+
1.0 -2.0 1.0
574+
```
575+
"""
576+
function gaussjordan(A::Matrix{Float64}; verbose::Bool = true)::Matrix{Float64}
577+
n, p = size(A)
578+
b = zeros(Float64,n, n)
579+
for i in 1:n
580+
b[i, i] = 1.0
581+
end
582+
Z = Float64[A b]
583+
@inbounds @fastmath @simd for i in 1:n
584+
@fastmath @simd for j in 1:n
585+
if i != j
586+
Z[j, :] .= view(Z, j, :) .- (Z[j, i] / Z[i, i]) .* view(Z, i, :)
587+
else
588+
Z[j, :] .= (1.0 / Z[i, j]) .* view(Z, j, :)
589+
end
590+
verbose && display(Z)
591+
end
592+
end
593+
return Z[:, (p+1):end]
594+
end
595+
548596

549597
end # end of module Simplex

test/testsimplex.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
@testset "Simplex" begin
2+
3+
@testset "Gauss Jordan" begin
4+
5+
m = Float64[1.0 2 7; -1 6 5; 9 8 -3]
6+
7+
expected = [
8+
0.142157 -0.151961 0.0784314;
9+
-0.102941 0.161765 0.0294118;
10+
0.151961 -0.0245098 -0.0196078
11+
]
12+
13+
result = gaussjordan(m, verbose = false)
14+
15+
@test isapprox(result, expected, atol = 0.0001)
16+
end
17+
218
@testset "Maximization Problem" begin
319

420
eps = 0.0001

0 commit comments

Comments
 (0)