-
Notifications
You must be signed in to change notification settings - Fork 12
My realization #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0c0c9a8
b2ac507
797d597
7b6458a
8427d7d
01210a2
3344170
2afc654
69924b2
1bee331
8fa0ab6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 3 | ||
| 1 1 2 9 | ||
| 2 4 -3 1 | ||
| 3 6 -5 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1.0 2.0 3.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>hyperskill</groupId> | ||
| <artifactId>ru.jnster</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package GaussianElimintation; | ||
|
|
||
| /** | ||
| * Класс для нахлждения вектора решений системы уравнений методом Гаусса | ||
| */ | ||
|
|
||
| public class GaussianElimintation { | ||
| private Matrix matrix; | ||
| private double result[]; | ||
| private int N; | ||
|
|
||
| public GaussianElimintation(double[][] matrix) { | ||
| setMatrix(matrix); | ||
| } | ||
|
|
||
| public GaussianElimintation(Double[][] matrix) { | ||
| setMatrix(matrix); | ||
| } | ||
|
|
||
| public GaussianElimintation(Matrix matrix) { | ||
| setMatrix(matrix); | ||
| } | ||
|
|
||
| public double[] findAnswer() { | ||
| double coefficient; | ||
| result = new double[N--]; | ||
| for (int counter = 0; counter < N; counter++) { | ||
| for (int next = counter + 1; next <= N; next++) { | ||
| coefficient = matrix.getElement(next, counter) / matrix.getElement(counter, counter) * -1; | ||
| matrix.addRow(coefficient, counter, next, counter); | ||
| } | ||
| } | ||
| for (int counter = N; counter >= 0; counter--) { | ||
| result[counter] = (matrix.getElement(counter, N + 1) - calculate(counter)) / matrix.getElement(counter, counter); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Проверяет является ли матрица квадратной. Вектор значение после знака равенства не учитывается. | ||
| * | ||
| * @param matrix Матрица для проверки, включая столбец значений после знака раменства. | ||
| * @return true - матрица квадратная, false - нет | ||
| */ | ||
| public static boolean isSquareMatrix(Matrix matrix) { | ||
| boolean result = true; | ||
| int N = matrix.getN(); | ||
| for (Row row : matrix.getRows()) { | ||
| result &= row.getDoubleList().size() == N + 1; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Вычисление известной части уравнения | ||
| * @param stage индекс после которого происходит расчёт | ||
| * @return сумма известных частей полинома | ||
| */ | ||
| private double calculate(int stage) { | ||
| double result = 0.0; | ||
| if (stage != N) { | ||
| for (int counter = stage + 1; counter <= N; counter++) { | ||
| result += matrix.getElement(stage, counter) * this.result[counter]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public void setMatrix(double[][] matrix) { | ||
| setMatrix(new Matrix(matrix)); | ||
| } | ||
|
|
||
| public void setMatrix(Double[][] matrix) { | ||
| setMatrix(new Matrix(matrix)); | ||
| } | ||
|
|
||
| public void setMatrix(Matrix matrix) { | ||
| try { | ||
| if (!isSquareMatrix(matrix)) throw new RuntimeException("This matrix is not square matrix."); | ||
| } catch (RuntimeException re) { | ||
| re.printStackTrace(); | ||
| } | ||
| this.matrix = matrix; | ||
| N = this.matrix.getN(); | ||
| result = null; | ||
| } | ||
|
|
||
| public Matrix getMatrix() { | ||
| return matrix; | ||
| } | ||
|
|
||
| public double[] getResult() { | ||
| if (result == null) result = findAnswer(); | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package GaussianElimintation; | ||
|
|
||
| import java.io.*; | ||
|
|
||
| /** | ||
| * Обязательные аргументы запуска это флаги "-in" и "-out", после которых следует имя файла с исходными данными или | ||
| * имя файла для вывода значения сооттветственно. | ||
| */ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| BufferedReader in = null; | ||
| BufferedWriter out = null; | ||
| double[][] matrix; | ||
| double[] result; | ||
| String[] buffer; | ||
| int N; | ||
| GaussianElimintation gaussianElimintation; | ||
|
|
||
| try { | ||
| for (int counter = 0; counter < 4; counter++) { | ||
| if (args[counter].equals("-in")) { | ||
| in = new BufferedReader(new FileReader(args[++counter])); | ||
| } | ||
| if (args[counter].equals("-out")) { | ||
| out = new BufferedWriter(new FileWriter(args[++counter])); | ||
| } | ||
| } | ||
|
|
||
| if (in == null || out == null) return; | ||
|
|
||
| buffer = in.readLine().split(" "); | ||
| N = Integer.parseInt(buffer[0]); | ||
| matrix = new double[N][]; | ||
|
|
||
| for (int row = 0; row < N; row++) { | ||
| matrix[row] = new double[N + 1]; | ||
| buffer = in.readLine().split(" "); | ||
| for (int column = 0; column <= N; column++) { | ||
| matrix[row][column] = Double.parseDouble(buffer[column]); | ||
| } | ||
| } | ||
| in.close(); | ||
| gaussianElimintation = new GaussianElimintation(matrix); | ||
| result = gaussianElimintation.findAnswer(); | ||
| for (int counter = 0; counter < N; counter++) { | ||
| out.append(Double.toString(result[counter]) + " "); | ||
| } | ||
| out.close(); | ||
| System.out.println("Completed!"); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it is better to use this try-catch more "local" to encapsulate only those code which can really produce IOException |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, your code looks very readable! Congratulations!