Skip to content

Commit

Permalink
add gaussjordan for steps of a matrix inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jbytecode committed Jan 10, 2025
1 parent d4885ab commit 791628c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
### 0.2.4 (Upcoming Release)
### 0.2.5 (Upcoming Release)


### 0.2.4

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


### 0.2.3
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OperationsResearchModels"
uuid = "8042aa49-e599-49ec-a8f7-b5b80cc82a88"
authors = ["Mehmet Hakan Satman <[email protected]>"]
version = "0.2.3"
version = "0.2.4"

[deps]
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
Expand Down
5 changes: 5 additions & 0 deletions docs/src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ OperationsResearchModels.travelingsalesman
OperationsResearchModels.createsimplexproblem
```

### Gauss Jordan steps for matrix inversion
```@docs
OperationsResearchModels.gaussjordan
```

4 changes: 2 additions & 2 deletions src/OperationsResearchModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import .Latex: latex
import .Johnsons: JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
import .RandomKeyGA: Chromosome, run_ga
import .TravelingSalesman: TravelinSalesmenResult, travelingsalesman
import .Simplex: SimplexProblem, simplexiterations, createsimplexproblem
import .Simplex: SimplexProblem, simplexiterations, createsimplexproblem, gaussjordan

export TransportationProblem, TransportationResult, balance, isbalanced, northwestcorner
export Connection, ShortestPathResult, MaximumFlowResult, nodes
Expand All @@ -76,7 +76,7 @@ export latex
export Chromosome, run_ga
export JohnsonResult, johnsons, JohnsonException, makespan, johnsons_ga
export TravelinSalesmenResult, travelingsalesman
export simplexiterations, SimplexProblem, createsimplexproblem
export simplexiterations, SimplexProblem, createsimplexproblem, gaussjordan

export JuMP, HiGHS

Expand Down
48 changes: 48 additions & 0 deletions src/simplex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export simplexiterations
export simplexpretty
export createsimplexproblem
export solve!
export gaussjordan

import ..Utility: numround

Expand Down Expand Up @@ -545,5 +546,52 @@ function createsimplexproblem(
return s
end

"""
gaussjordan(A::Matrix; verbose::Bool = true)::Matrix
Description:
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.
Arguments:
- A::Matrix: The matrix to find the inverse.
- verbose::Bool: If true, the intermediate steps are displayed. Default is true.
Returns:
The inverse of the given matrix.
Example:
```julia
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 10.0]
julia> invA = gaussjordan(A, verbose = false)
3×3 Matrix{Float64}:
-0.666667 -1.33333 1.0
-0.666667 3.66667 -2.0
1.0 -2.0 1.0
```
"""
function gaussjordan(A::Matrix{Float64}; verbose::Bool = true)::Matrix{Float64}
n, p = size(A)
b = zeros(Float64,n, n)
for i in 1:n
b[i, i] = 1.0
end
Z = Float64[A b]
@inbounds @fastmath @simd for i in 1:n
@fastmath @simd for j in 1:n
if i != j
Z[j, :] .= view(Z, j, :) .- (Z[j, i] / Z[i, i]) .* view(Z, i, :)
else
Z[j, :] .= (1.0 / Z[i, j]) .* view(Z, j, :)
end
verbose && display(Z)
end
end
return Z[:, (p+1):end]
end


end # end of module Simplex
16 changes: 16 additions & 0 deletions test/testsimplex.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
@testset "Simplex" begin

@testset "Gauss Jordan" begin

m = Float64[1.0 2 7; -1 6 5; 9 8 -3]

expected = [
0.142157 -0.151961 0.0784314;
-0.102941 0.161765 0.0294118;
0.151961 -0.0245098 -0.0196078
]

result = gaussjordan(m, verbose = false)

@test isapprox(result, expected, atol = 0.0001)
end

@testset "Maximization Problem" begin

eps = 0.0001
Expand Down

2 comments on commit 791628c

@jbytecode
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/122744

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.4 -m "<description of version>" 791628c620f2ad15d6049a174df27b8c63519982
git push origin v0.2.4

Please sign in to comment.