Skip to content

Instantly share code, notes, and snippets.

@wagfim
Created November 27, 2019 20:04
Show Gist options
  • Save wagfim/03772fb8ecbe298bd40c6a0a06789e58 to your computer and use it in GitHub Desktop.
Save wagfim/03772fb8ecbe298bd40c6a0a06789e58 to your computer and use it in GitHub Desktop.
Gauss Jordan Solver
//package gaussjordan;
import java.util.Scanner;
class Main {
static int M = 10; //??
// funcao principal
public static void main(String[] args) {
Scanner leitor = new Scanner(System.in);
int n = 3, flag = 0;
String pares;
int contador = 0;
/*
float a[][] = {{0, 2, 1, 4},
{1, 1, 2, 6},
{2, 1, 1, 7}};
*/
//
System.out.println("Informe os pares de pontos, utilize apenas espaços. Pressione Enter ao terminar:");
pares = leitor.nextLine();
pares = pares.replaceAll(",", ".");
String pontos[] = pares.split(" ");
for (String ponto : pontos) {
contador++;
}
System.out.println(contador);
double listaDePontos[] = new double[contador];
for (int i = 0; i < pontos.length; i++) {
String teste[] = pontos[i].split("/");
if (teste.length > 1) {
double aux = (double) (Double.valueOf(teste[0]) / Double.valueOf(teste[1]));
listaDePontos[i] = aux;
} else {
Double d = Double.valueOf(pontos[i]);
listaDePontos[i] = d;
}
}
System.out.println("fim preenchimento lista de pontos");
/*
//System.out.println(pares);
for (int i = 0; i < pares.length(); i++) {
if(!Character.toString(pares.charAt(i)).equals(" ")) {
contador++;
}
}
int listaPontos[] = new int[contador];
int k = 0;
for (int i = 0; i < pares.length(); i++) {
String aux = Character.toString(pares.charAt(i));
if(!aux.equals(" ")) {
listaPontos[k++] = Integer.parseInt(aux);
}
}
*/
int ordemMatriz = contador / 2;
System.out.println("Qual a ordem da matriz");
// n = Integer.parseInt(leitor.next());
n = 3;
float a[][] = new float[ordemMatriz][ordemMatriz + 1];
int potencia = ordemMatriz - 1;
int k = 0;
int linhas = a.length - 1;
int colunas = a[0].length - 1;
for (int i = 0; i <= linhas; i++) {
for (int j = 0; j <= colunas; j++) {
///*
if (j != colunas) {
if(potencia > 0) {
double aux = Math.pow(listaDePontos[k], potencia--);
a[i][j] = (float) aux;
} else {
a[i][j] = (float)listaDePontos[k];
}
} else {
k++;
a[i][j] = (float) listaDePontos[k];
k++;
}
//*/
/*
if (j == colunas) {
k++;
}
a[i][j] = pontos[k];
System.out.print(a[i][j] + " ");
*/
}
potencia = ordemMatriz - 1;
}
System.out.println("fim preenchimento matriz");
/*
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.println("Informe o binomio " + (j + 1) + " da linha " + (i + 1));
a[i][j] = leitor.nextFloat();
}
}
*/
// ordem da matriz
// executa operacao
flag = executaOperacoes(a, n);
if (flag == 1) {
flag = checarConsistencia(a, n, flag);
}
// exibe a matriz final
System.out.println("\nMatriz final: ");
exibeMatriz(a, n);
System.out.println("");
// soluções, caso se aplique
PrintResult(a, n, flag);
}
// exibe a matriz
static void exibeMatriz(float a[][], int n) {
String vermelhoCiano = "\033[31;46m";
String reset = "\033[0m";
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
if (i == j) {
System.out.print(vermelhoCiano + a[i][j] + reset + " | ");
} else {
System.out.print(a[i][j] + " | ");
}
}
System.out.println();
}
}
//funcao para reduzir a matriz
// escalonação por linha
static int executaOperacoes(float a[][], int n) {
int i, j, k = 0, c, flag = 0, m = 0;
float pro = 0;
// operações elementares
for (i = 0; i < n - 1; i++) {
if (a[i][i] == 0) {
c = 1;
int teste = i+c;
while (a[i + c][i] == 0 && (i + c) < n) {
c++;
}
if ((i + c) == n) {
flag = 1;
break;
}
for (j = i, k = 0; k <= n; k++) {
float temp = a[j][k];
a[j][k] = a[j + c][k];
a[j + c][k] = temp;
}
}
for (j = 0; j < n; j++) {
// exclui diagonal principal
if (i != j) {
// converte matriz para linha reduzida
// escalonamento (diagonal )
float p = a[j][i] / a[i][i];
for (k = 0; k <= n; k++) {
a[j][k] = a[j][k] - (a[i][k]) * p;
}
}
}
}
return flag;
}
//funcao que exibe o resultado desejado se soluções unicas existem
//caso contrario exibe sem solução ou infinitas soluções
//a depender da entrada
static void PrintResult(float a[][], int n, int flag) {
System.out.print("Resultado: ");
if (flag == 2) {
System.out.println("Infinitas soluções");
} else if (flag == 3) {
System.out.println("Não existe solução");
} //exibe a solução exibindo as constante por suas respectivas diagonais
//carece de correção para 1
else {
char letra = 'x';
for (int i = 0; i < n; i++) {
System.out.print(letra++ + ": " + a[i][n] / a[i][i] + " ");
}
}
}
//checa infinatas soluções ou sem solução
static int checarConsistencia(float a[][], int n, int flag) {
int i, j;
float soma;
// flag == 2 solução infinita
// flag == 3 sem solução
flag = 3;
for (i = 0; i < n; i++) {
soma = 0;
for (j = 0; j < n; j++) {
soma = soma + a[i][j];
}
if (soma == a[i][j]) {
flag = 2;
}
}
return flag;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment