Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,101 @@
<<<<<<< HEAD

# Created by https://www.gitignore.io/api/java,intellij
# Edit at https://www.gitignore.io/?templates=java,intellij

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# IntelliJ IDEA folder
.idea/

#module
rujnster.iml

# Sonarlint plugin
.idea/sonarlint

### Java ###
# Compiled class file
*.class
=======
# Compiled class file
*.class
out/
>>>>>>> 1bee331b085ecfea6a7af0e9719010f1626939b0

# Log file
*.log
Expand All @@ -23,8 +118,12 @@ out/
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

<<<<<<< HEAD
# End of https://www.gitignore.io/api/java,intellij
=======
# Git
git.properties

# IntelliJ IDEA folder
.idea/
>>>>>>> 1bee331b085ecfea6a7af0e9719010f1626939b0
6 changes: 0 additions & 6 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/project.iml

This file was deleted.

4 changes: 4 additions & 0 deletions in.txt
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
1 change: 1 addition & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0 2.0 3.0
20 changes: 20 additions & 0 deletions pom.xml
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>
7 changes: 0 additions & 7 deletions src/solver/Main.java

This file was deleted.

96 changes: 96 additions & 0 deletions src/solver/java/GaussianElimintation/GaussianElimintation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package GaussianElimintation;

/**
* Класс для нахлждения вектора решений системы уравнений методом Гаусса
*/

public class GaussianElimintation {
Copy link

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!

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;
}
}
55 changes: 55 additions & 0 deletions src/solver/java/GaussianElimintation/Main.java
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();
Copy link

Choose a reason for hiding this comment

The 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

}
}
}
Loading