I need help with writing a generic implementation of Gaussian Elimination in Jav
ID: 3828823 • Letter: I
Question
I need help with writing a generic implementation of Gaussian Elimination in Java. I have an normal implementation, but I do not know how to make it to where it is Generic and not type-specific. Please help. Below is the code to make generic:
public class GaussianElimination {
private static final double EPSILON = 1e-10;
// Gaussian elimination with partial pivoting
public static double[] lsolve(double[][] A, double[] b) {
int n = b.length;
for (int p = 0; p < n; p++) {
// find pivot row and swap
int max = p;
for (int i = p + 1; i < n; i++) {
if (Math.abs(A[i][p]) > Math.abs(A[max][p])) {
max = i;
}
}
double[] temp = A[p]; A[p] = A[max]; A[max] = temp;
double t = b[p]; b[p] = b[max]; b[max] = t;
// singular or nearly singular
if (Math.abs(A[p][p]) <= EPSILON) {
throw new ArithmeticException("Matrix is singular or nearly singular");
}
// pivot within A and b
for (int i = p + 1; i < n; i++) {
double alpha = A[i][p] / A[p][p];
b[i] -= alpha * b[p];
for (int j = p; j < n; j++) {
A[i][j] -= alpha * A[p][j];
}
}
}
// back substitution
double[] x = new double[n];
for (int i = n - 1; i >= 0; i--) {
double sum = 0.0;
for (int j = i + 1; j < n; j++) {
sum += A[i][j] * x[j];
}
x[i] = (b[i] - sum) / A[i][i];
}
return x;
}
// sample client
public static void main(String[] args) {
int n = 3;
double[][] A = {
{ 0, 1, 1 },
{ 2, 4, -2 },
{ 0, 3, 15 }
};
double[] b = { 4, 2, 36 };
double[] x = lsolve(A, b);
// print results
for (int i = 0; i < n; i++) {
StdOut.println(x[i]);
}
}
}
Explanation / Answer
you used in your program partial pivoting of guassian elimination.
here, I use implementation of guassian that will reduced row:
import java.util.Scanner;
/** Class GaussianElimination **/
public class GaussianElimination
{
public void solve(double[][] A, double[] B)
{
int N = B.length;
for (int k = 0; k < N; k++)
{
/** find pivot row **/
int max = k;
for (int i = k + 1; i < N; i++)
if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
max = i;
/** swap row in A matrix **/
double[] temp = A[k];
A[k] = A[max];
A[max] = temp;
/** swap corresponding values in constants matrix **/
double t = B[k];
B[k] = B[max];
B[max] = t;
/** pivot within A and B **/
for (int i = k + 1; i < N; i++)
{
double factor = A[i][k] / A[k][k];
B[i] -= factor * B[k];
for (int j = k; j < N; j++)
A[i][j] -= factor * A[k][j];
}
}
/** Print row echelon form **/
printRowEchelonForm(A, B);
/** back substitution **/
double[] solution = new double[N];
for (int i = N - 1; i >= 0; i--)
{
double sum = 0.0;
for (int j = i + 1; j < N; j++)
sum += A[i][j] * solution[j];
solution[i] = (B[i] - sum) / A[i][i];
}
/** Print solution **/
printSolution(solution);
}
/** function to print in row echleon form **/
public void printRowEchelonForm(double[][] A, double[] B)
{
int N = B.length;
System.out.println(" Row Echelon form : ");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.printf("%.3f ", A[i][j]);
System.out.printf("| %.3f ", B[i]);
}
System.out.println();
}
/** function to print solution **/
public void printSolution(double[] sol)
{
int N = sol.length;
System.out.println(" Solution : ");
for (int i = 0; i < N; i++)
System.out.printf("%.3f ", sol[i]);
System.out.println();
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Gaussian Elimination Algorithm Test ");
/** Make an object of GaussianElimination class **/
GaussianElimination ge = new GaussianElimination();
System.out.println(" Enter number of variables");
int N = scan.nextInt();
double[] B = new double[N];
double[][] A = new double[N][N];
System.out.println(" Enter "+ N +" equations coefficients ");
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = scan.nextDouble();
System.out.println(" Enter "+ N +" solutions");
for (int i = 0; i < N; i++)
B[i] = scan.nextDouble();
ge.solve(A,B);
}
}