Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DailyChallenge170221 {
- public static void main(String[] args) {
- Matrix m = new Matrix( new double[][]
- {{7,5,-3,16},
- {3,-5,2,-8},
- {5,3,-7,0}}
- );
- System.out.println(solveSystemOfLinearEquations(m));
- }
- public static class Matrix {
- final int M;
- final int N;
- final double[][] data;
- public Matrix(double[][] data) {
- if( data == null) {
- this.M=0; this.N=0; this.data = new double[0][0];
- }
- else {
- this.M = data.length;
- int l = 0;
- for( int i=0; i<this.M; ++i)
- if( data[i]!= null && data[i].length > l) l = data[i].length;
- this.N = l;
- this.data = new double[M][N];
- for( int i=0; i<this.M; ++i) {
- if( data[i] == null) continue;
- for( int j=0; j<data[i].length; ++j) {
- this.data[i][j] = data[i][j];
- }
- }
- }
- }
- public Matrix(int M, int N) {
- this.M = M;
- this.N = N;
- data = new double[M][N];
- }
- }
- /** The last column in the matrix represents the constant, each other column
- * is a distinct variable, call it x_i where i is the column number.
- */
- public static String solveSystemOfLinearEquations( Matrix matrix) {
- Matrix m = gausianElimination(matrix);
- String str = "";
- for( int row=m.M-1; row >= 0; --row) {
- boolean valid = false;
- for( int col=0; col < m.N-1; ++col) {
- if( m.data[row][col] == 1) {
- if( valid) return "There are no Real solutions.";
- else valid = true;
- }
- }
- if( valid) break;
- if( !valid && m.data[row][m.N-1] != 0) return "There are no Real solutions.";
- }
- for( int col=0; col<m.N-1; ++col) {
- int row;
- for( row=0; row<m.M; ++row) {
- if( m.data[row][col] == 1) {
- str += "x_"+col+" = "+m.data[row][m.N-1]+"\n";
- break;
- }
- }
- if( row == m.M) {
- str += "x_"+col+" = any number\n";
- }
- }
- return str;
- }
- public static Matrix gausianElimination( Matrix matrix) {
- Matrix m = new Matrix(matrix.data);
- int nextPrimaryRow=0;
- for( int col=0; col < m.N-1; ++col) {
- // Find the index for use in gausian elimination
- int indexRow;
- for( indexRow=0; indexRow<m.M; ++indexRow) {
- boolean good = true;
- for( int icol=0; icol<col; ++icol) {
- if( m.data[indexRow][icol] != 0) {
- good = false;
- break;
- }
- }
- if( good && m.data[indexRow][col] != 0)
- break;
- }
- if( indexRow == m.M) continue; // All-0 column
- for( int row=0; row<m.M; ++row) {
- if( row!=indexRow) {
- double factor = -m.data[row][col]/m.data[indexRow][col];
- m.data[row][col] = 0; // explicit to prevent rounding errors
- for( int icol=col+1; icol<m.N; ++icol) {
- m.data[row][icol] += m.data[indexRow][icol] * factor;
- }
- }
- }
- // Scale the row to 1 AFTER above calculation to minimize generation loss of precision
- double factor = 1 / m.data[indexRow][col];
- m.data[indexRow][col] = 1; // explicit to prevent rounding errors
- for( int icol=col+1; icol < m.N; ++icol) {
- m.data[indexRow][icol] *= factor;
- }
- // Swap the rows so that it's in triangular order
- if( indexRow != nextPrimaryRow) {
- for( int icol=0; icol < m.M; ++icol) {
- double t = m.data[indexRow][icol];
- m.data[indexRow][icol] = m.data[nextPrimaryRow][icol];
- m.data[nextPrimaryRow][icol] = t;
- }
- }
- nextPrimaryRow++;
- }
- return m;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement