Last active
August 29, 2015 14:13
-
-
Save celestocalculus/7124c5693a033a21f264 to your computer and use it in GitHub Desktop.
Matrix multiplication using Gaussian elimination.
This file contains 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
/* | |
* Date created, Sunday 26th March, 2009. | |
* Coded by: Celestine Ezeokoye | |
*/ | |
package gaussianelimination; | |
import java.util.Scanner; | |
public class GaussianElimination { | |
public static void main(String[] args) { | |
double[][] values = null; | |
double[] rhs = null; | |
acceptInput(values, rhs); | |
} | |
private static void acceptInput(double[][] values, double[] answerPart) { | |
Scanner input = new Scanner(System.in); | |
System.out.print("Enter the size of the array: "); | |
int size = input.nextInt(); | |
values = new double[size][size]; | |
answerPart = new double[size]; | |
for (int i = 0; i < values.length; i++) { | |
for (int j = 0; j < values[i].length; j++) { | |
System.out.print("Enter the value for index (" + (i + 1) + "," + (j + 1) + "): "); | |
values[i][j] = input.nextDouble(); | |
} | |
System.out.print("Enter value for the augmented part at row (" + (i + 1) + "): "); | |
answerPart[i] = input.nextDouble(); | |
} | |
reduceRows(values, answerPart); | |
} | |
private static double[] backSubstitute(double[][] values, double[] answerPart) { | |
double[] answers = new double[answerPart.length]; | |
for (int i = values.length - 1; i >= 0; i--) { | |
double temp = 0; | |
for (int j = values[i].length - 1; j >= i; j--) { | |
if (j == i) { | |
temp = (answerPart[i] - temp) / values[i][j]; | |
} else { | |
temp += values[i][j] * answers[j]; | |
} | |
} | |
answers[i] = temp; | |
} | |
System.out.println("The answers are: \n"); | |
for (int i = 0; i < answers.length; i++) { | |
System.out.printf("X%d is %.2f\n", (i + 1), answers[i]); | |
} | |
System.out.println(); | |
return answers; | |
} | |
private static double[] reduceRows(double[][] values, double[] answerPart) { | |
System.out.println("Reducing rows...\n"); | |
double pivot = 0.0, pivotRatio = 0.0; | |
for (int i = 1; i < values.length; i++) { | |
for (int j = i; j < values.length; j++) { | |
pivot = values[j][i - 1]; | |
pivotRatio = pivot / values[i - 1][i - 1]; | |
for (int k = i - 1; k < values[j].length; k++) { | |
values[j][k] = (pivotRatio * values[i - 1][k]) - values[j][k]; | |
} | |
answerPart[j] = (pivotRatio * answerPart[i - 1]) - answerPart[j]; | |
} | |
} | |
System.out.println("The Augmented reduced rows are:\n"); | |
for (int i = 0; i < values.length; i++) { | |
for (int j = 0; j < values[i].length; j++) { | |
if (j != values[i].length - 1) { | |
System.out.printf("%.2f,\t", values[i][j]); | |
} else { | |
System.out.printf("%.2f|\t", values[i][j]); | |
} | |
} | |
System.out.printf("%.2f\n", answerPart[i]); | |
} | |
System.out.println(); | |
return backSubstitute(values, answerPart); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment