-
-
Notifications
You must be signed in to change notification settings - Fork 346
To add gaussian elimination Implementation #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 1 commit into
TheAlgorithms:master
from
NithinU2802:feature/to-add-gaussian-algorithm
Apr 4, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Gaussian Elimination Algorithm | ||
| # | ||
| # This algorithm is used to solve a system of linear equations of the form Ax = b. It works by | ||
| # converting the matrix into row echelon form using Gaussian elimination (with partial pivoting), | ||
| # and then applying back substitution to compute the solution vector x. | ||
| # | ||
| # Inputs: | ||
| # | ||
| # A: An n × n matrix containing the coefficients of the equations | ||
| # b: A vector of length n representing the right-hand side values | ||
| # | ||
| # Output: | ||
| # | ||
| # x: A vector of length n that contains the solution to the system | ||
| # | ||
| # Dependencies: None (uses base R functions) | ||
| # | ||
| # Note: This implementation assumes the matrix A is invertible. For real world scenarios, | ||
| # consider adding checks for singularity and numerical stability. | ||
|
|
||
| gaussian_elimination <- function(A, b) { | ||
|
|
||
| # To check if A is a square matrix | ||
| if (!is.matrix(A) || nrow(A) != ncol(A)) { | ||
| stop("A must be a square matrix") | ||
| } | ||
|
|
||
| n <- nrow(A) | ||
|
|
||
| # To check if b is a vector of correct length | ||
| if (!is.vector(b) || length(b) != n) { | ||
| stop("b must be a vector of length equal to the number of rows in A") | ||
| } | ||
|
|
||
| # To create augmented matrix [A|b] | ||
| Ab <- cbind(A, b) | ||
|
|
||
| # Forward elimination with partial pivoting | ||
| for (i in 1:(n-1)) { | ||
| # Find pivot row | ||
| pivot_row <- i | ||
| for (j in (i+1):n) { | ||
| if (abs(Ab[j, i]) > abs(Ab[pivot_row, i])) { | ||
| pivot_row <- j | ||
| } | ||
| } | ||
|
|
||
| # Swap rows if needed | ||
| if (pivot_row != i) { | ||
| temp <- Ab[i, ] | ||
| Ab[i, ] <- Ab[pivot_row, ] | ||
| Ab[pivot_row, ] <- temp | ||
| } | ||
|
|
||
| # looking for zero pivot | ||
| if (abs(Ab[i, i]) < .Machine$double.eps) { | ||
| stop("Matrix is singular or nearly singular") | ||
| } | ||
|
|
||
| # Eliminate below pivot | ||
| for (j in (i+1):n) { | ||
| factor <- Ab[j, i] / Ab[i, i] | ||
| Ab[j, ] <- Ab[j, ] - factor * Ab[i, ] | ||
| } | ||
| } | ||
|
|
||
| # To check last pivot | ||
| if (abs(Ab[n, n]) < .Machine$double.eps) { | ||
| stop("Matrix is singular or nearly singular") | ||
| } | ||
|
siriak marked this conversation as resolved.
|
||
|
|
||
| # Substitution Method | ||
| x <- numeric(n) | ||
| for (i in n:1) { | ||
| if (i < n) { | ||
| x[i] <- (Ab[i, n+1] - sum(Ab[i, (i+1):n] * x[(i+1):n])) / Ab[i, i] | ||
| } else { | ||
| x[i] <- Ab[i, n+1] / Ab[i, i] | ||
| } | ||
| } | ||
|
|
||
| return(x) | ||
| } | ||
|
|
||
| # Example | ||
| A <- matrix(c(2, 3, -1, | ||
| 4, 4, -3, | ||
| 2, -3, 1), nrow = 3, byrow = TRUE) | ||
|
|
||
| b <- c(5, 3, -1) | ||
|
|
||
| solution <- gaussian_elimination(A, b) | ||
|
|
||
| cat("Solution:\n") | ||
| cat(sprintf("x = %.6f\n", solution[1])) | ||
| cat(sprintf("y = %.6f\n", solution[2])) | ||
| cat(sprintf("z = %.6f\n", solution[3])) | ||
|
|
||
| verification <- A %*% solution | ||
| cat("\nVerification (Ax should equal b):\n") | ||
| cat(sprintf("Ax = [%.6f, %.6f, %.6f]\n", verification[1], verification[2], verification[3])) | ||
| cat(sprintf("b = [%.6f, %.6f, %.6f]\n", b[1], b[2], b[3])) | ||
|
siriak marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.