Git Product home page Git Product logo

gauss_jordan_elimination's Introduction

Gauss-Jordan Elimination

This repo implements from scratch the Gaussian Elimination algorithm in Julia Lang.

If you need to refresh your memory about the technique, I suggest you read the wikipedia page of the method, which has a great introduction and applications in Linear Algebra disciplines.

Example of the operation

Function implemented

function gauss_jordan(A::Matrix{T}) where {T<:Number}
    
    # convert to float to avoid InexactError: Int64()
    (T <: Integer) && (A = convert.(Float64, A))

    # check if matrix is singular
    m, n = size(A)
    if m == n
        @assert det(A)  0.0 "Must insert a non-singular matrix"
    else
        @assert det(A[:,1:end-1])  0.0 "Must insert a non-singular matrix or a system matrix [A b]"
    end

    for i  axes(A, 1)
        if A[i,i] == 0.0                            # check if need swap rows
            swap_rows(i, m)
        end

        @. A[i,:] = A[i,:] / A[i,i]                 # divide pivot line by pivot element

        for j  axes(A, 1)                          # iterate each line for each pivot column, except pivot line
            if j  i                                # jump pivot line
                @. A[j,:] = A[j,:] - A[i,:]*A[j,i]  # apply gauss jordan in each line
            end
        end
    end

    return A
end

function swap_rows(i::T, nlinha::T) where {T<:Integer}
    for n  (i+1):nlinha        # iterate over lines above to check if could be swap
        if A[n,i]  0.0         # condition to swap row
            L = copy(A[i,:])    # copy line to swap
            A[i,:] = A[n,:]     # swap occur
            A[n,:] = L
            break
        end
    end
end

Benchmark inputing Int vs Float

gauss_jordan_elimination's People

Contributors

augustocl avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

gauss_jordan_elimination's Issues

This code works fast

I try this and it is faster than the usual A \ b

Capture d’écran_2022-07-24_20-26-47

Can you create the code like this for finding determinant with Cramer's rule?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.