miércoles, 28 de octubre de 2009

Prueba de programación

Matrices.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Christian Arias
*/
public class Matrices {
public int numeroFilas;
public int numeroColumnas;
public double [][] matriz;

public Matrices(){

}
public Matrices (int nf, int nc){

numeroFilas = nf;
numeroColumnas = nc;
matriz = new double[numeroFilas][numeroColumnas];

for(int i=0; i < numeroFilas; i++)
for (int j=0; j < numeroColumnas; j++)
matriz[i][j]=0;
}
public Matrices transpuesta(){
Matrices resultado;
resultado= new Matrices(this.numeroColumnas, this.numeroFilas);
for (int i=0; i < this.numeroFilas;i++)
for(int j=0; j < this.numeroColumnas;j++)
resultado.matriz[j][i]=this.matriz[i][j];
return resultado;
}
public Matrices multiplicacion(Matrices b){
Matrices resultado;
if(this.numeroColumnas == b.numeroFilas){
resultado= new Matrices(this.numeroFilas, b.numeroColumnas);

for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < b.numeroColumnas; j++){
for(int k=0; k < this.numeroColumnas; k++)
resultado.matriz[i][j]+= (this.matriz[i][k]* b.matriz[k][j]);
}
}
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;

}


public String toString(){
String aux="[";
for (int i=0; i < numeroFilas; i++){
for (int j=0; j < numeroColumnas; j++ ){
aux+= matriz[i][j]+" ";
}
aux += "\n";
}
aux+="]";
return aux;
}


}

PruebaMatriz.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Christian Arias
*/
public class PruebaMatriz {
public static void main(String args[]){
Matrices a= new Matrices(1, 5);
a.matriz[0][0]= 1; a.matriz[0][1]=-1; a.matriz[0][2]=0;
a.matriz[0][3]= 1; a.matriz[0][4]=1;
System.out.println(a.toString());

Matrices b= new Matrices(5, 3);
b.matriz[0][0]= 6; b.matriz[0][1]=-2; b.matriz[0][2]=5;
b.matriz[1][0]= 4; b.matriz[1][1]=2; b.matriz[1][2]=-1;
b.matriz[2][0]= 0; b.matriz[2][1]=1; b.matriz[2][2]=1;
b.matriz[3][0]= -2; b.matriz[3][1]=-2; b.matriz[3][2]=0;
b.matriz[4][0]= 1; b.matriz[4][1]=0; b.matriz[4][2]=-1;
System.out.println(b.toString());

Matrices c= new Matrices(2, 3);
c.matriz[0][0]= 2; c.matriz[0][1]=2; c.matriz[0][2]=1;
c.matriz[1][0]= 3; c.matriz[1][1]=-1;c.matriz[1][2]=0;
System.out.println(c.toString());


Matrices e= new Matrices (3,3);
e.matriz[0][0]= -1; e.matriz[0][1]=0; e.matriz[0][2]=-1;
e.matriz[1][0]= 1; e.matriz[1][1]=0; e.matriz[1][2]=1;
e.matriz[2][0]= -1; e.matriz[2][1]=1; e.matriz[2][2]=-1;
System.out.println(e.toString());



Matrices f;
f= b.transpuesta();
System.out.println(f.toString());


Matrices h;
h= a.transpuesta();
System.out.println(h.toString());

Matrices n;
n=a.multiplicacion(b);
System.out.println(n.toString());

Matrices g;
g= c.multiplicacion(e);
System.out.println(g.toString());


Matrices i;
i= e.multiplicacion(f);
System.out.println(i.toString());

Matrices m;
m= c.multiplicacion(e);
System.out.println(m.toString());

Matrices r;
r= g.multiplicacion(f);
System.out.println("Multiplicacion de C*E*Bt=\n"+r.toString());

r= n.multiplicacion(e);
System.out.println("Multiplicacion de A*B*E=\n"+r.toString());

r= i.multiplicacion(h);
System.out.println("Multiplicacion de E*Bt*At=\n"+r.toString());

}




}

Resultados de las operaciones

Multiplicacion de C*E*Bt=
[-13.0 -1.0 0.0 0.0 0.0
-44.0 -12.0 -4.0 8.0 0.0
]
Multiplicacion de A*B*E=
[-12.0 5.0 -12.0
]
Multiplicacion de E*Bt*At=
[-6.0
6.0
-12.0
]

No hay comentarios:

Publicar un comentario