Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void gaussJordan(BigRational[][] matrix) {
- final int rows = matrix.length, cols = matrix[0].length;
- int row = 0, col = 0, i, j;
- BigRational[] swapBuffer;
- BigRational multKoef;
- while (row < rows && col < cols) {
- i = row; //pos of non-null row
- while (i < rows && matrix[i][col].equals(BigRational.ZERO)) {
- i++;
- }
- if (i == rows) {
- //no pivots at all
- col++;
- continue;
- }
- if (i != row) {
- //need to swap
- swapBuffer = matrix[i];
- matrix[i] = matrix[row];
- matrix[row] = swapBuffer;
- }
- //set pivot to 1
- multKoef = matrix[row][col].reciprocal();
- matrix[row][col] = BigRational.ONE;
- for (j = col + 1; j < cols; j++) {
- matrix[row][j] = matrix[row][j].times(multKoef);
- }
- //set 0 over and under pivot
- for (i = 0; i < rows; i++) {
- if (i == row) {
- continue;
- }
- if (matrix[i][col].equals(BigRational.ZERO)) {
- continue;
- }
- multKoef = matrix[i][col].negate();
- matrix[i][col] = BigRational.ZERO;
- for (j = col + 1; j < cols; j++) {
- matrix[i][j] = matrix[i][j].plus(matrix[row][j].times(multKoef));
- }
- }
- //now move to next pivot
- col++;
- row++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement