← Desenvolvimento

[JAVA/ÁLGEBRA] Característica de uma matriz

Lida 2713 vezes

Offline

johndays 
Membro
Mensagens 387 Gostos 1
Feedback +3

Troféus totais: 24
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts

Olá pessoal +t!

É o seguinte, vou tentar ser o mais claro e objectivo possível na apresentação da minha dúvida.

Tenho uma cadeira de programação onde tenho que desenvolver código em JAVA para calcular a característica de uma matriz, eu já ando há alguns dias à volta disto, e penso que tenho algo já consideravelmente agradável. No entanto, o meu código não funciona em todos os casos e queria saber se alguém pode ajudar-me.

Vou deixar algum código aqui, para o caso de detectarem alguma gralha, avisarem-me pf.

Dividi o código em vários métodos :

APRESENTAR MATRIZ - Nada de especial
Código: (java) [Seleccione]
public static void apresentarmatriz(double matriz[][]) {
        // Apresentação da matriz
        out.format("%n Matriz Condensada: %n");
        int f = 0;
        for (int l1 = 0; l1 < matriz.length; l1++) {
            out.format("%n");
            for (int c1 = 0; c1 < matriz[0].length; c1++) {

                out.format("%8s", matriz[l1][c1]);
            }
            out.format("%n");
        }
    }

CALCULAR O NÚMERO DA CARACTERÍSTICA - nada de especial

Código: (java) [Seleccione]
public static void numcar(double matriz[][]){
         //verifica qual o maior ou menor nr de linhas/nr de colunas
        int min;
        int max;
        if (matriz.length < matriz[0].length) {
            max = matriz[0].length;
            min = matriz.length;
        } else {
            max = matriz.length;
            min = matriz[0].length;
        }
   int contadores = 0;
            for (int car = 0; car < min; car++) {
                if (matriz[car][car] != 0) {
                    contadores++;
                }
            }

            out.format("A característica da matriz é de %d", contadores);
    }

CONDENSAR - Mais complicado, mas nada de outro mundo.
Código: (java) [Seleccione]
public static void condensar(double matriz[][]) {
        //verifica qual o maior ou menor nr de linhas/nr de colunas
        int min;
        int max;
        if (matriz.length < matriz[0].length) {
            max = matriz[0].length;
            min = matriz.length;
        } else {
            max = matriz.length;
            min = matriz[0].length;
        }

        for (int k = 0; k < min; k++) {
            if (matriz[k][k] != 0) {
                //colocar o pivot a 1
                double temp=matriz[k][k];
                for (int ccc = 0; ccc < matriz[0].length; ccc++) {
                    matriz[k][ccc] = matriz[k][ccc] /temp;
                }
                for (int z = k + 1; z < matriz.length; z++) {
                    if (matriz[z][k] != 0) {
                        double temp3;
                        int z2 = k;
                        temp3 = matriz[z][z2];
                        z2++;
                        for (int y = k; y < matriz[0].length; y++) {

                            matriz[z][y] = matriz[z][y] - (temp3 * matriz[k][y]);
                        }
                    }
                }
            }
 else{
                troca(matriz,k);
 }
        }

    }


TROCA MATRIZ - É aqui que acho que está a dita gralha, mas o que me irrita mais é o facto de este código funcionar para maior parte dos casos, só em casos específicos é que falha.
Código: (java) [Seleccione]
public static void troca(double matriz[][], int pivot){
        //verifica qual o maior ou menor nr de linhas/nr de colunas
        int min;
        int max;
        if (matriz.length < matriz[0].length) {
            max = matriz[0].length;
            min = matriz.length;
        } else {
            max = matriz.length;
            min = matriz[0].length;
        }
         boolean flagimp=false;
//Criar Matriz temporária
        double temp[][] = new double[matriz.length][matriz[0].length];
        for (int a = 0; a < matriz.length; a++) {
            for (int b = 0; b < matriz[0].length; b++) {
                temp[a] = matriz[a];
            }

        }
        ///////////////////////// troca linhas
        int numo=0;
        for (int k1=pivot;k1<min;k1++){
            if(matriz[pivot][k1]==0){
                numo++;
            }
        }
        if(numo != min-pivot){
        while (matriz[pivot][pivot] == 0) {

                    for (int c1 = 0; c1 < matriz[0].length; c1++) {
                        matriz[pivot][c1] = matriz[pivot + 1][c1];
                        if (matriz[pivot][pivot] != 0) {
                            matriz[pivot + 1][c1] = temp[pivot][c1];
                        }

                    }


                }
        }
 else {
             //Procurar por um elemento !=0 para trocar, já que na 1ªcoluna e 1ªlinha não existem elementos !=0 e encontrar a posição

                    int poscoluna = 0;
                    int poslinha = 0;
                    boolean flag = false;

                    for (int l = 0; l < matriz.length; l++) {
                        for (int c = 0; c < matriz[0].length; c++) {
                            if (matriz[l][c] != 0 && flag != true) {
                                poslinha = l;
                                poscoluna = c;
                                flag = true;

                            }
                        }
                    }

                    // Se todos os elementos da matriz forem 0, então a característica é 0

                    if (poslinha == 0 && poscoluna == 0) {



                    } else {
                        //Substituição
                        for (int l1 = 0; l1 < matriz.length; l1++) {
                            matriz[l1][0] = matriz[l1][poscoluna];
                            matriz[l1][poscoluna] = temp[l1][0];
                        }
                        //Criar Matriz temporária2
                        double temp2[][] = new double[matriz.length][matriz[0].length];
                        for (int a = 0; a < matriz.length; a++) {
                            for (int b = 0; b < matriz[0].length; b++) {
                                temp[a] = matriz[a];
                            }

                        }

                        for (int c1 = 0; c1 < matriz[0].length; c1++) {
                            matriz[0][c1] = matriz[poslinha][c1];
                            matriz[poslinha][c1] = temp2[0][c1];

                        }


                    }
 }



    }

Agradecia qualquer futura orientação caso consigam ajudar.

Obrigado.
Offline

johndays 
Membro
Mensagens 387 Gostos 1
Feedback +3

Troféus totais: 24
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts

Bem, já consegui resolver o problema.

Não vou colocar onde estavam os vários erros para não ser vítima de plágio neste trabalho. No entanto, vou deixar todo o código do 1º post com alguns erros, que já dá uma GRANDE ajuda a quem pesquisar pelo google sobre este assunto.

Abraço.