Java код застрял при попытке найти RREF матрицы - PullRequest
0 голосов
/ 25 апреля 2020

Я пытаюсь сделать свой собственный алгоритм, чтобы превратить матрицу в RREF (эшелон с уменьшенным числом строк). Но он просто застрял во время выполнения и никогда не проходил. Я думаю, что здесь есть какая-то проблема с l oop, но я просто не знаю, как ее найти.

public class Matrix_solver {
    public double[][] solve(double[][] source_matrix) {

        int m = source_matrix.length; //length
        int n = source_matrix[0].length; //width
        int j = 0;
        int i = 0;
        int correct_pivot_row_coordinate = 0;

        while (j < n) {
            //find the pivot in the j column
            double[] entries_in_column_j = new double[m];
            while (i < m) {
                if (i <= m - 1) {
                    entries_in_column_j[i] = source_matrix[i][j];
                    i++;
                } else i = 0;
            }
            double pivot_value = new Max_finder().find(entries_in_column_j);

            if (pivot_value == 0) { // there's no pivot in the current j column
                j++;//jump to the next column
            } else if (j <= (m - 1) && correct_pivot_row_coordinate <= (n - 1) && correct_pivot_row_coordinate <= (m - 1)) {

                new row_swap().swap(source_matrix, new Max_finder().find_position(entries_in_column_j), correct_pivot_row_coordinate);

                new Matrix_eliminator().eliminate(source_matrix, correct_pivot_row_coordinate, j);

                for (int index = 0; index < n; index++) {
                    source_matrix[correct_pivot_row_coordinate][index] = source_matrix[correct_pivot_row_coordinate][index] / pivot_value;//make all pivots 1
                }
                correct_pivot_row_coordinate++;
                j++;
            }
        }
        return source_matrix;
    }
}

class Matrix_eliminator {
    public void eliminate(double[][] matrix, int pivot_row_coordinate, int pivot_column_coordinate) {
        for (int i = 0; i < matrix.length; i++) { // jump through each row
            while (i != pivot_row_coordinate) { // leave the row that has the pivot alone
                for (int j = 0; j < matrix[i].length; j++) { // scan through the rows needed to be modified
                    matrix[i][j] = (matrix[pivot_row_coordinate][pivot_column_coordinate] / matrix[i][pivot_column_coordinate]) * matrix[i][j];
                    matrix[i][j] = matrix[pivot_row_coordinate][j] - matrix[i][j]; // the process to make every thing in the pivot column turn into 0 but the pivot itself
                }
            }
        }
    }
}

class Max_finder {
    public double find(double[] array) {
        double max_value = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max_value) {
                max_value = array[i];
            }
        }
        return max_value;
    }

    public int find_position(double[] array) {
        double max_value = array[0];
        int position = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max_value) {
                position = i;
            }
        }
        return position;
    }
}

Я знаю, что мой код испорчен. Это буквально мой самый первый код проекта. Если вы заметили, где я запутался, пожалуйста, помогите. Большое спасибо за ваше время, читая мой грязный код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...