Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public class DailyChallenge170221 {
  2. public static void main(String[] args) {
  3. Matrix m = new Matrix( new double[][]
  4. {{7,5,-3,16},
  5. {3,-5,2,-8},
  6. {5,3,-7,0}}
  7. );
  8.  
  9.  
  10. System.out.println(solveSystemOfLinearEquations(m));
  11. }
  12.  
  13. public static class Matrix {
  14. final int M;
  15. final int N;
  16. final double[][] data;
  17.  
  18. public Matrix(double[][] data) {
  19. if( data == null) {
  20. this.M=0; this.N=0; this.data = new double[0][0];
  21. }
  22. else {
  23. this.M = data.length;
  24.  
  25. int l = 0;
  26. for( int i=0; i<this.M; ++i)
  27. if( data[i]!= null && data[i].length > l) l = data[i].length;
  28. this.N = l;
  29.  
  30. this.data = new double[M][N];
  31.  
  32. for( int i=0; i<this.M; ++i) {
  33. if( data[i] == null) continue;
  34.  
  35. for( int j=0; j<data[i].length; ++j) {
  36. this.data[i][j] = data[i][j];
  37. }
  38. }
  39. }
  40. }
  41.  
  42. public Matrix(int M, int N) {
  43. this.M = M;
  44. this.N = N;
  45. data = new double[M][N];
  46. }
  47. }
  48.  
  49. /** The last column in the matrix represents the constant, each other column
  50. * is a distinct variable, call it x_i where i is the column number.
  51. */
  52. public static String solveSystemOfLinearEquations( Matrix matrix) {
  53. Matrix m = gausianElimination(matrix);
  54. String str = "";
  55.  
  56. for( int row=m.M-1; row >= 0; --row) {
  57. boolean valid = false;
  58. for( int col=0; col < m.N-1; ++col) {
  59. if( m.data[row][col] == 1) {
  60. if( valid) return "There are no Real solutions.";
  61. else valid = true;
  62. }
  63. }
  64. if( valid) break;
  65. if( !valid && m.data[row][m.N-1] != 0) return "There are no Real solutions.";
  66. }
  67.  
  68. for( int col=0; col<m.N-1; ++col) {
  69. int row;
  70. for( row=0; row<m.M; ++row) {
  71. if( m.data[row][col] == 1) {
  72. str += "x_"+col+" = "+m.data[row][m.N-1]+"\n";
  73. break;
  74. }
  75. }
  76. if( row == m.M) {
  77. str += "x_"+col+" = any number\n";
  78. }
  79. }
  80. return str;
  81. }
  82.  
  83. public static Matrix gausianElimination( Matrix matrix) {
  84. Matrix m = new Matrix(matrix.data);
  85. int nextPrimaryRow=0;
  86. for( int col=0; col < m.N-1; ++col) {
  87. // Find the index for use in gausian elimination
  88. int indexRow;
  89. for( indexRow=0; indexRow<m.M; ++indexRow) {
  90. boolean good = true;
  91. for( int icol=0; icol<col; ++icol) {
  92. if( m.data[indexRow][icol] != 0) {
  93. good = false;
  94. break;
  95. }
  96. }
  97.  
  98.  
  99. if( good && m.data[indexRow][col] != 0)
  100. break;
  101. }
  102. if( indexRow == m.M) continue; // All-0 column
  103.  
  104. for( int row=0; row<m.M; ++row) {
  105. if( row!=indexRow) {
  106. double factor = -m.data[row][col]/m.data[indexRow][col];
  107. m.data[row][col] = 0; // explicit to prevent rounding errors
  108. for( int icol=col+1; icol<m.N; ++icol) {
  109. m.data[row][icol] += m.data[indexRow][icol] * factor;
  110. }
  111. }
  112. }
  113. // Scale the row to 1 AFTER above calculation to minimize generation loss of precision
  114. double factor = 1 / m.data[indexRow][col];
  115. m.data[indexRow][col] = 1; // explicit to prevent rounding errors
  116. for( int icol=col+1; icol < m.N; ++icol) {
  117. m.data[indexRow][icol] *= factor;
  118. }
  119.  
  120. // Swap the rows so that it's in triangular order
  121. if( indexRow != nextPrimaryRow) {
  122. for( int icol=0; icol < m.M; ++icol) {
  123. double t = m.data[indexRow][icol];
  124. m.data[indexRow][icol] = m.data[nextPrimaryRow][icol];
  125. m.data[nextPrimaryRow][icol] = t;
  126. }
  127. }
  128. nextPrimaryRow++;
  129. }
  130.  
  131. return m;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement