package org.example.Math; public class GaussSolver { public static double[]…
- User
package org.example.Math;
public class GaussSolver { public static double[] solve(double[][] A, double[] b) { int n = b.length; for (int pivot = 0; pivot < n; pivot++) { int max = pivot; for (int i = pivot + 1; i < n; i++) { if (Math.abs(A[i][pivot]) > Math.abs(A[max][pivot])) { max = i; } } double[] temp = A[pivot]; A[pivot] = A[max]; A[max] = temp; double t = b[pivot]; b[pivot] = b[max]; b[max] = t;
if (Math.abs(A[pivot][pivot]) <= 1e-10) throw new ArithmeticException("No solution or infinite solutions"); for (int i = pivot + 1; i < n; i++) { double factor = A[i][pivot] / A[pivot][pivot]; b[i] -= factor * b[pivot]; for (int j = pivot; j < n; j++) { A[i][j] -= factor * A[pivot][j]; } } } 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; }} упрости метод гауса
- Assistant