Java - Matriz numeros complejos java

 
Vista:
sin imagen de perfil

Matriz numeros complejos java

Publicado por Adrian (5 intervenciones) el 12/12/2014 16:54:21
Buenas!
Tengo que hacer un proyecto que consiste en una calculadora matricial de números complejos
Para un trabajo anterior conseguí hacer la aplicación para operar con complejos, pero el trabajar con arrays bidimensionales me está matando.
Si alguien tiene alguna idea, será bien recibida.
MUCHAS GRACIAS!!
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
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Matriz numeros complejos java

Publicado por Andrés (340 intervenciones) el 12/12/2014 22:36:13
Qué operaciones implementaste? De estas operaciones, todas estarían incluidas en la "calcladora matricial"? Pero en realidad son vectores cierto?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Matriz numeros complejos java

Publicado por Adrian (5 intervenciones) el 13/12/2014 17:15:21
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
public class Complejo{
    public double real;
    public double imag;
 
    public Complejo() {
        real=0.0;
        imag=0.0;
    }
 
    public Complejo(double real, double imag){
        this.real=real;
        this.imag=imag;
    }
 
    public static Complejo conjugado(Complejo c){
        return new Complejo(c.real, -c.imag);
    }
 
    public static double  mostrarParteReal(Complejo x){
        return x.real;
    }
 
    public static double mostrarParteImag(Complejo x){
        return x.imag;
    }
 
    public static Complejo inversoMultiplicativo(Complejo c1){
        double x=(c1.real/(c1.real*c1.real+c1.imag*c1.imag));
        double y=-(c1.imag/(c1.real*c1.real+c1.imag*c1.imag));
        return new Complejo(x,y);
    }
 
    public static Complejo inversoAditivo(Complejo c1){
        double x=-(c1.real);
        double y=-(c1.imag);
        return new Complejo(x,y);
    }
 
    public double modulo(){
        return Math.sqrt(real*real+imag*imag);
    }
 
    public double argumento(){
        double angulo=Math.atan2(imag, real);
        if(angulo<0)  angulo=2*Math.PI+angulo;
        return angulo*180/Math.PI;
    }
    public String binomicaPolar(Complejo c1){
        double x=Math.sqrt((real*real)+(imag*imag));
        double y=Math.atan(imag/real);
        return "z="+x+"*cos("+y+")+i*sen("+y+"))";
    }
 
    public static Complejo polarBinomica(Complejo c1){
        double x=c1.real*(Math.cos(c1.imag));
        double y=c1.imag*(Math.sin(c1.imag));
        return new Complejo(x,y);
    }
 
    public static boolean complejoIguales(Complejo c1, Complejo c2){
        return((c1.real==c2.real)&&(c1.imag==c2.imag));
 
    }
 
    public  static boolean complejoSimilares(Complejo c1, Complejo c2){
        return((c1.real==c2.real)||(c1.imag==c2.imag));
 
    }
 
    public static Complejo suma(Complejo c1, Complejo c2){
        double x=c1.real+c2.real;
        double y=c1.imag+c2.imag;
        return new Complejo(x, y);
    }
 
    public static Complejo resta(Complejo c1, Complejo c2){
        double x=c1.real-c2.real;
        double y=c1.imag-c2.imag;
        return new Complejo(x, y);
    }
 
    public static Complejo producto(Complejo c1, Complejo c2){
        double x=c1.real*c2.real-c1.imag*c2.imag;
        double y=c1.real*c2.imag+c1.imag*c2.real;
        return new Complejo(x, y);
    }
 
 
    public static Complejo cociente(Complejo c1, Complejo c2)throws ExcepcionDivideCero{
        double aux, x, y;
        if(c2.modulo()==0.0){
            throw new ExcepcionDivideCero("Divide entre cero");
        }else{
            aux=c2.real*c2.real+c2.imag*c2.imag;
            x=(c1.real*c2.real+c1.imag*c2.imag)/aux;
            y=(c1.imag*c2.real-c1.real*c2.imag)/aux;
        }
        return new Complejo(x, y);
    }
 
 
 
 
 
    private static double potencia(double base, int exponente){
        double resultado=1.0;
        for(int i=0; i<exponente; i++){
            resultado*=base;
        }
        return resultado;
    }
 
    private static double combinatorio(int m, int n){
        long num=1;
        long den=1;
        for(int i=m; i>m-n; i--){
            num*=i;
        }
        for(int i=2; i<=n; i++){
            den*=i;
        }
        return (double)num/den;
    }
 
    public static Complejo potencia(Complejo c, int exponente){
        double x=0.0, y=0.0;
        int signo;
        for(int i=0; i<=exponente; i++){
            signo=(i%2==0)?+1:-1;
 
            x+=combinatorio(exponente, 2*i)*potencia(c.real, exponente-2*i)*potencia(c.imag, 2*i)*signo;
            if(exponente==2*i)  break;
 
            y+=combinatorio(exponente, 2*i+1)*potencia(c.real, exponente-(2*i+1))*potencia(c.imag, 2*i+1)*signo;
        }
        return new Complejo(x, y);
    }
 
    public static Complejo mostrarComplejo(Complejo c){
        return new Complejo(c.real, c.imag);
    }
 
    public String toString(){
        if(imag>0)     return new String((double)Math.round(100*real)/100+" + "+(double)Math.round(100*imag)/100+"*i");
        return new String((double)Math.round(100*real)/100+" - "+(double)Math.round(-100*imag)/100+"*i");
    }
}
 
class ExcepcionDivideCero extends Exception {
    public ExcepcionDivideCero() {
        super();
    }
 
    public ExcepcionDivideCero(String s) {
        super(s);
    }
}


ESTO ES LA CLASE COMPLEJO, Y LUEGO ESTA LA CLASE CALCULADORA:
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Matriz numeros complejos java

Publicado por Adrian (5 intervenciones) el 13/12/2014 17:16:10
Y LUEGO ESTA LA CLASE APP:
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package complejo;
import java.util.*;
public class Aplicacion{
    Scanner teclado = new Scanner(System.in);
 
    public static int Menu(){
        Scanner teclado = new Scanner(System.in);
        int opcion;
        System.out.println();
        System.out.println("calculadora de numeros complejos");
        System.out.println();
        System.out.println("1)SUMAR");
        System.out.println("2)RESTAR");
        System.out.println("3)MULTIPLICAR");
        System.out.println("4)DIVIDIR");
        System.out.println("5)MODULO");
        System.out.println("6)ARGUMENTO");
        System.out.println("7)SABER SI SON IGUALES O NO");
        System.out.println("8)SABER SI SON SIMILARES O NO");
        System.out.println("9)CALCULAR EL COMPLEJO CONJUGADO");
        System.out.println("10)PARTE REAL DE UN COMPLEJO");
        System.out.println("11)PARTE IMAGINARIA DE UN COMPLEJO");
        System.out.println("12)POTENCIA DE UN COMPLEJO");
        System.out.println("13)INVERSO MULTIPLICATIVO");
        System.out.println("14)INVERSO ADITIVO");
        System.out.println("15)MOSTRAR COMPLEJO POR PANTALLA");
        System.out.println("16)PASAR A POLAR");
        System.out.println("17)PASAR A BINOMICA");
        System.out.println("18)SALIR");
        System.out.println();
        System.out.print(" OPCION: ");
        opcion = teclado.nextInt();
        return opcion;
    }
    public static void main(String[] args) {
        Scanner teclado = new Scanner(System.in);
        Complejo calculadora,c1,c2,resultado;
        Process p;
        calculadora = new Complejo();
        double real, imag;
        int opcion;
        double solucion;
        boolean iguales;
        int exp;
        String polar;
        do{
            opcion = Menu();
 
            switch(opcion){
                case 1:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el segundo real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el segundo imaginario: ");
                imag = teclado.nextDouble();
                c2=new Complejo(real, imag);
                resultado=Complejo.suma(c1, c2);
                System.out.println("Suma "+resultado);
 
                break;
                case 2:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el segundo real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el segundo imaginario: ");
                imag = teclado.nextDouble();
                c2=new Complejo(real, imag);
                resultado=Complejo.resta(c1, c2);
                System.out.println("rest "+resultado);
                break;
 
                case 3:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el segundo real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el segundo imaginario: ");
                imag = teclado.nextDouble();
                c2=new Complejo(real, imag);
                resultado=Complejo.producto(c1, c2);
                System.out.println("Producto "+resultado);
                break;
                case 4:
 
                try{
                    System.out.print("Escribe el primer real: ");
                    real = teclado.nextDouble();
                    System.out.print("Escribe el primer imaginario: ");
                    imag = teclado.nextDouble();
                    c1=new Complejo(real, imag);
                    System.out.print("Escribe el segundo real: ");
                    real = teclado.nextDouble();
                    System.out.print("Escribe el segundo imaginario: ");
                    imag = teclado.nextDouble();
                    c2=new Complejo(real, imag);
                    resultado=Complejo.cociente(c1, c2);
                    System.out.println("Division "+resultado);
                }catch(ExcepcionDivideCero ex){
                    System.out.println("Al calcular el cociente se ha producido una excepción\n "
                        +ex.getClass()+ " con el mensaje "+ ex.getMessage());
                }
                break;
                case 5:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                solucion=c1.modulo();
                System.out.println(solucion);
                break;
                case 6:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                solucion=c1.argumento();
                System.out.println(solucion);
                break;
                case 7:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el segundo real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el segundo imaginario: ");
                imag = teclado.nextDouble();
                c2=new Complejo(real, imag);
                iguales=Complejo.complejoIguales(c1, c2);
                System.out.println("¿Son iguales? "+iguales);
                break;
                case 8:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el segundo real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el segundo imaginario: ");
                imag = teclado.nextDouble();
                c2=new Complejo(real, imag);
                iguales=Complejo.complejoSimilares(c1, c2);
                System.out.println("¿Son similares? "+iguales);
                break;
                case 9:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                resultado=c1.conjugado(c1);
                System.out.println("Conjugado:"+resultado);
                break;
                case 10:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                solucion=c1.mostrarParteReal(c1);
                System.out.println("La parte real es:"+solucion);
                break;
                case 11:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                solucion=c1.mostrarParteImag(c1);
                System.out.println("La parte imaginaria es:"+solucion);
                break;
                case 12:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                System.out.print("Escribe el exponente: ");
                exp = teclado.nextInt();
                resultado=Complejo.potencia(c1, exp);
                System.out.println("Potencia "+resultado);
                break;
 
                case 13:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                resultado=c1.inversoMultiplicativo(c1);
                System.out.println("El inverso multiplicativo es:"+resultado);
                break;
                case 14:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                resultado=c1.inversoAditivo(c1);
                System.out.println("El inverso aditivo es:"+resultado);
                break;
                case 15:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                resultado=c1.mostrarComplejo(c1);
                System.out.println("Complejo:"+resultado);
                break;
                case 16:
                System.out.print("Escribe el primer real: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el primer imaginario: ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                polar=c1.binomicaPolar(c1);
                System.out.println("Forma polar:"+polar);
                break;
                case 17:
                System.out.print("Escribe el modulo: ");
                real = teclado.nextDouble();
                System.out.print("Escribe el argumento(grados): ");
                imag = teclado.nextDouble();
                c1=new Complejo(real, imag);
                resultado=c1.polarBinomica(c1);
                System.out.println("Forma binomica:"+resultado);
                break;
                case 18:
                System.out.println("Adiós");
 
                default:
                break;
 
 
            }
        }while(opcion!=18);
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Matriz numeros complejos java

Publicado por Andrés (340 intervenciones) el 13/12/2014 22:02:21
Interesante, yo te ayudo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Matriz numeros complejos java

Publicado por Adrian (5 intervenciones) el 13/12/2014 22:09:01
Muchas gracias, agradecería que me avisaras pronto con algo mas:)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Matriz numeros complejos java

Publicado por Andrés (340 intervenciones) el 13/12/2014 23:54:24
Podrías hecharle un ojo a lo siguiente please:

http://www.numbertheory.org/book/cha5.pdf
http://math.stackexchange.com/questions/180849/why-is-the-complex-number-z-abi-equivalent-to-the-matrix-form-left-begins
http://www.sosmath.com/matrix/complex/complex.html

O si tienes otra bibliografia please, share it with me!

No me queda claro lo siguiente:

Se me ocurrieron algunas soluciones;

0.- Hay que agregar un método que regrese un Complejo como una matriz double[][] a fuerza a menos que se desee una clase nueva que tenga una matriz en vez de dos double

1.- Dejar todo como esta y una vez que obtienes el resultado con tu clase actual, sólo pasar a representación matricial (se me hace trampa jojojoj)

2.- Implementar todo con matrices, pero aqui no veo el reuso de lo que ya tienes, por ejemplo la suma podria ser:

double[][] suma (Complejo c1, Comlejo c2) {
double[][] c1M = c1.getAsMaTrix();
double[][] c2M = c2.getAsMaTrix();

c1M[0][0] +=c2M[0][0];
c1M[1][0] +=c2M[1][0];

c1M[0][1] +=c2M[0][1];
c1M[1][1] +=c2M[1][1];

}

pero tambien podria ser dejar la suma como esta y sólo imprimir el complejo como matriz

espero haberme explicado, voy a salir, te leo en cuanto regrese
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar