Java - Ayuda matrices

 
Vista:

Ayuda matrices

Publicado por José Luis (2 intervenciones) el 08/10/2017 20:11:12
con esta clase matriz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public class Matriz {
    // Solo matrices cuadradas;
    public int dimension ;
    private double[][] matriz;
    //Si solo metes dimension al inicializa a cero
    public Matriz(int dimension) {
        this.dimension = dimension;
        this.matriz = new double[this.dimension][this.dimension];
        for(int i = 0; i < dimension; i++){
            for(int j = 0; j < dimension; j++){
                this.matriz[i][j]=0.0;
            }
        }
    }
    //Se puede inicializar en base a una matriz
    public Matriz(double[][] matriz) {
        this.matriz = matriz;
        this.dimension = matriz.length;
    }
 
 
 
    public void imprimirMatriz(){
        System.out.println();
        for(int i = 0; i < dimension; i++){
            for(int j =0; j < dimension; j++){
               if (matriz[i][j] >= 0) {
                   System.out.print(" ");
                   System.out.printf("%.2f", matriz[i][j] );
                   System.out.print(" ");
               }
               else {
                   System.out.printf("%.2f", matriz[i][j] );
                   System.out.print(" ");
               }
            }
            System.out.println();
        }
        System.out.println();
    }
    public double determinante(){
        double determinante = 0;
        if (this.dimension == 2){
            determinante = this.matriz[0][0] * this.matriz[1][1] - this.matriz[0][1] * this.matriz[1][0];
            return determinante;
        }
        for (int i = 0; i < this.dimension; i++){
            double [][] matrizNueva = new double [this.dimension - 1][this.dimension - 1];
            for (int j = 0; j < this.dimension; j++){
                if (j != i){
                    for(int k=1; k<this.dimension; k++){
                        int indice=-1;
                        if(j<i)
                            indice=j;
                        else if(j>i)
                            indice=j-1;
                        matrizNueva[indice][k-1]=this.matriz[j][k];
                    }
                }
            }
            if(i%2==0)
                determinante += matriz[i][0] * new Matriz(matrizNueva).determinante();
            else
                determinante -= matriz[i][0] * new Matriz(matrizNueva).determinante();
        }
        return determinante;
    }
 
    public Matriz traspuesta(){
        double [][] valoresResultado = new double[this.dimension][this.dimension];
        for(int i = 0; i < this.dimension; i++){
            for(int j = 0; j < this.dimension; j++){
                valoresResultado[i][j] = this.matriz[j][i];
            }
        }
        Matriz matrizResultado = new Matriz(valoresResultado);
        return matrizResultado;
    }
 
 
    static private double [][] quitarColumna (double [][] m1, int columna){
        double [][] resultado = new double [m1.length][m1.length - 1];
        for(int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1.length; j++) {
                if (j < columna )
                {
                    resultado[i][j] = m1[i][j];
                }
                else if (j > columna){
                    resultado[i][j-1] = m1[i][j];
                }
            }
        }
        return resultado;
    }
    static private double [][] quitarFila (double [][] m1, int fila){
        double [][] resultado = new double [m1.length - 1][m1.length];
        for(int i = 0; i < m1.length; i++) {
            if (i < fila) {
                for (int j = 0; j < m1.length-1; j++) {
                    resultado[i][j] = m1[i][j];
                }
            } else if (i > fila) {
                for (int j = 0; j < m1.length-1; j++) {
                    resultado[i-1][j] = m1[i][j];
                }
            }
        }
 
        return resultado;
    }
 
    public Matriz cofactores(){
        double [][] valoresResultado = new double[this.dimension][this.dimension];
        if (this.dimension == 2){
            valoresResultado[0][0] = this.matriz[1][1];
            valoresResultado[1][1] = this.matriz[0][0];
            valoresResultado[1][0] = -this.matriz[0][1];
            valoresResultado[0][1] = -this.matriz[1][0];
            Matriz matrizResultado = new Matriz(valoresResultado);
            return matrizResultado;
        }
        for(int i = 0; i < this.dimension; i++){
            for(int j = 0; j < this.dimension; j++){
                int indice = (i+j)%2;
                indice = (int)Math.pow(-1, indice);
                double [][] valoresSinColumna = quitarColumna(this.matriz, i);
                Matriz matrizSinColumna = new Matriz(valoresSinColumna);
                double [][] valoresSinColumnaSinFila = quitarFila(matrizSinColumna.matriz, j);
                valoresResultado[i][j] = indice * new Matriz(valoresSinColumnaSinFila).determinante();
            }
        }
        Matriz matrizResultado = new Matriz(valoresResultado);
        return matrizResultado;
    }
 
    public Matriz copia(){
        double [][] valoresResultado = new double[this.dimension][this.dimension];
        for(int i = 0; i < this.dimension; i++){
            for(int j = 0; j < this.dimension; j++){
                valoresResultado[i][j] = this.matriz[i][j];
            }
        }
        Matriz matrizResultado = new Matriz(valoresResultado);
        return matrizResultado;
    }
 
    public Matriz adjunta(){
        Matriz matrizResultado = this.copia();
        return matrizResultado.traspuesta().cofactores();
    }
 
    private Matriz MultiplicarPorEscalar(double escalar){
        Matriz matrizResultado = this.copia();
        for(int i = 0; i < this.dimension; i++){
            for(int j = 0; j < this.dimension; j++){
                matrizResultado.matriz[i][j] = escalar * matrizResultado.matriz[i][j];
            }
        }
        return matrizResultado;
    }
 
    public Matriz inversa(){
        Matriz matrizResultado = this.copia();
        return matrizResultado.adjunta().traspuesta().MultiplicarPorEscalar(1 / matrizResultado.determinante());
    }
 
    public Matriz productoMatrices(Matriz m1) {
    if (m1.dimension != this.dimension) throw new RuntimeException("No se pueden multiplicar las matrices");
    Matriz matrizResultado = new Matriz(this.dimension);
    for (int i = 0; i < matrizResultado.dimension; i++){
        for(int j = 0; j < matrizResultado.dimension; j++){
            for (int k=0; k<matrizResultado.dimension; k++) {
                matrizResultado.matriz [i][j] +=  this.matriz[i][k]*m1.matriz[k][j];
            }
        }
    }
    return matrizResultado;
    }
}

al hacer en el main
1
2
3
double[][] valores  = {{1, 7, 3},{4, 6, 6}, {7, 8, 9}};
        Matriz matrizRellena = new Matriz(valores);
         matrizRellena.productoMatrices(matrizRellena.inversa()).imprimirMatriz()

me imprime
1,00 0,00 0,00
-0,00 1,00 0,00
0,00 0,00 1,00
entiendo que es por los errores de representacion con las variables con coma flotante. Me gustaria saber si hay alguna solucion.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder