Created
October 26, 2019 22:39
-
-
Save draganczukp/8f98ab08a959d33d85d3facae777f66a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.Scanner; | |
import java.util.stream.Collectors; | |
public class Zad3 { | |
public static void main(String[] args) { | |
System.out.print("Podaj rozmiar: "); | |
int n; | |
Random random = new Random(System.nanoTime()); | |
try (Scanner scanner = new Scanner(System.in)) { | |
n = scanner.nextInt(); | |
} | |
double[][] matrix = createMatrix(n, random); | |
System.out.println(Arrays.stream(matrix).map(Arrays::toString).collect(Collectors.joining("\n"))); | |
double[][] inverse = inverse(matrix); | |
System.out.println("Inverse"); | |
System.out.println(Arrays.stream(inverse).map(Arrays::toString).collect(Collectors.joining("\n"))); | |
} | |
private static double[][] createMatrix(int n, Random random) { | |
double[][] result = new double[n][n]; | |
for (int y = 0; y < result.length; y++) { | |
for (int x = 0; x < result[y].length; x++) { | |
result[y][x] = random.nextDouble() * 10; | |
} | |
} | |
return result; | |
} | |
private static double[][] inverse(double[][] matrix) { | |
double[][] inverse; | |
double[][] attached = new double[matrix.length][matrix.length]; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix.length; j++) { | |
double[][] temp = new double[matrix.length - 1][matrix.length - 1]; | |
int a = 0, b = 0; | |
for (int w = 0; w < matrix.length; w++) { | |
for (int z = 0; z < matrix.length; z++) { | |
if (w != i && z != j) { | |
if (b >= temp.length) { | |
b = 0; | |
a++; | |
} | |
temp[a][b] = matrix[w][z]; | |
b++; | |
} | |
} | |
} | |
double det = det(temp); | |
if ((i + j) % 2 != 0) { | |
if (det > 0) { | |
det -= 2 * det; | |
} else { | |
det -= 2 * det; | |
} | |
} | |
attached[i][j] = det; | |
} | |
} | |
attached = transpose(attached); | |
inverse = multiply(1 / det(matrix), attached); | |
return inverse; | |
} | |
private static double[][] multiply(double n, double[][] matrix) { | |
double[][] result = new double[matrix.length][matrix[0].length]; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix[0].length; j++) { | |
result[i][j] = (matrix[i][j] * n); | |
} | |
} | |
return result; | |
} | |
private static double[][] transpose(double[][] matrix) { | |
double[][] result = new double[matrix[0].length][matrix.length]; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix[0].length; j++) { | |
result[j][i] = matrix[i][j]; | |
} | |
} | |
return result; | |
} | |
private static double det(double[][] matrix) { | |
double det = 0; | |
double[][] newMatrix = new double[matrix.length + (matrix.length - 1)][matrix[0].length]; | |
for (int i = 0, _i = 0; i < newMatrix.length; i++, _i++) { | |
for (int j = 0; j < matrix[0].length; j++) { | |
if (_i < matrix.length) { | |
newMatrix[i][j] = matrix[_i][j]; | |
} else { | |
_i = 0; | |
newMatrix[i][j] = matrix[_i][j]; | |
} | |
} | |
} | |
double product = 1; | |
int _i; | |
for (int i = 0; i < matrix.length; i++) { | |
_i = i; | |
for (int j = 0; j < matrix[0].length; j++) { | |
product *= newMatrix[_i][j]; | |
_i++; | |
} | |
det += product; | |
product = 1; | |
} | |
product = 1; | |
for (int i = 0; i < matrix.length; i++) { | |
_i = i; | |
for (int j = matrix[0].length - 1; j >= 0; j--) { | |
product *= newMatrix[_i][j]; | |
_i++; | |
} | |
det -= product; | |
product = 1; | |
} | |
return det; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment