RikuNoctis

Matrix Operations

May 5th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.34 KB | None | 0 0
  1. package OperacionesMatriz;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Matriz{
  8.     static BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));        
  9.     public static int opc, fila, colum, fila2, colum2;
  10.     public static double [][] m1;
  11.     public static double [][] m2;    
  12.    
  13.     public static void main(String[] args) throws IOException{
  14.         do{
  15.             System.out.println("Opciones:\n");
  16.             System.out.println("1.- Introducir Matriz A");
  17.             System.out.println("2.- Introducir Matriz B");
  18.             System.out.println("3.- A + B");
  19.             System.out.println("4.- A - B");
  20.             System.out.println("5.- A * B");
  21.             System.out.println("6.- Det(A)");
  22.             System.out.println("7.- Det(B)");
  23.             System.out.println("8.- Simet(A)");
  24.             System.out.println("9.- Simet(B)");
  25.             System.out.println("0.- SALIR\n");
  26.             opc = Integer.parseInt(entrada.readLine());
  27.            
  28.             switch(opc){
  29.                 case 1: matriz1();
  30.                         break;
  31.                 case 2: matriz2();
  32.                         break;
  33.                 case 3: sumar();
  34.                         break;
  35.                 case 4: restar();
  36.                         break;
  37.                 case 5: multi();
  38.                         break;
  39.                 case 6: deta();
  40.                         break;
  41.                 case 7: detb();
  42.                         break;
  43.                 case 8: simA();
  44.                         break;
  45.                 case 9: simB();
  46.                         break;
  47.                 //default:
  48.                 //  System.out.println("Opción incorrecta. \n\n\n|=======================================================|\n\n\n");
  49.                 //  break;
  50.             }
  51.         }
  52.         while(opc != 0);      
  53.     }
  54.    
  55.     private static void matriz1() throws IOException{
  56.         System.out.print("Introduce tamaño de las filas:\n");
  57.         fila = Integer.parseInt(entrada.readLine());
  58.         System.out.print("Introduce tamaño de las columnas:\n");
  59.         colum = Integer.parseInt(entrada.readLine());
  60.         m1 = new double [fila][colum];
  61.         for(int i = 0; i < fila; i++){
  62.             for(int j = 0; j < colum; j++){
  63.                 System.out.print("Valor de la Matriz A en: [" + (i + 1) + "," + (j + 1) + "]\n");
  64.                 m1[i][j] = Double.parseDouble(entrada.readLine());
  65.             }
  66.         }
  67.     }
  68.    
  69.     private static void matriz2() throws IOException{
  70.         System.out.print("Introduce tamaño de las filas:\n");
  71.         fila2 = Integer.parseInt(entrada.readLine());
  72.         System.out.print("Introduce tamaño de las columnas:\n");
  73.         colum2 = Integer.parseInt(entrada.readLine());
  74.         m2= new double [fila2][colum2];
  75.         for(int i = 0; i < fila2; i++){
  76.             for(int j = 0; j < colum2; j++){
  77.                 System.out.print("Valor de la Matriz B en: [" + (i + 1) + "," + (j + 1) + "]\n");
  78.                 m2[i][j] = Double.parseDouble(entrada.readLine());
  79.             }
  80.         }
  81.     }
  82.    
  83.     private static void sumar() throws IOException{
  84.         System.out.print("Suma de Matriz:\n");
  85.         if(fila == fila2 && colum == colum2){
  86.             for(int x = 0; x < fila; x++){
  87.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  88.                 for(int y = 0; y < colum; y++){
  89.                     System.out.print((m1[x][y]) + (m2[x][y]) + "\t|\t");
  90.                 }
  91.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  92.             }
  93.         System.out.println("\n"); //Controla el espacio entre la función sumar() y el menú de opciones.
  94.         }
  95.         else{
  96.             System.out.print("No se pueden sumar las Matrices.\n\n\n|=======================================================|\n\n\n");
  97.         }
  98.     }
  99.  
  100.     private static void sumar2(){ //Cambiar función en main() para no utilizar IOException.
  101.         System.out.print("Suma de Matriz sin IOException:\n");
  102.         if(fila == fila2 && colum == colum2){
  103.             for(int x = 0; x < fila; x++){
  104.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  105.                 for(int y = 0; y < colum; y++){
  106.                     System.out.print((m1[x][y]) + (m2[x][y]) + "\t|\t");
  107.                 }
  108.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  109.             }
  110.         System.out.println("\n"); //Controla el espacio entre la función sumar2() y el menú de opciones.
  111.         }
  112.         else{
  113.             System.out.print("No se pueden sumar las Matrices.\n\n\n|=======================================================|\n\n\n");
  114.         }
  115.     }
  116.  
  117.     private static void restar(){ //Función resta (Matriz A - Matriz B). Cambiar en el main().
  118.         System.out.print("Resta de Matriz:\n");
  119.         if(fila == fila2 && colum == colum2){
  120.             for(int x = 0; x < fila; x++){
  121.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  122.                 for(int y = 0; y < colum; y++){
  123.                     System.out.print((m1[x][y]) - (m2[x][y]) + "\t|\t");
  124.                 }
  125.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  126.             }
  127.         System.out.println("\n"); //Controla el espacio entre la función restar() y el menú de opciones.
  128.         }
  129.         else{
  130.             System.out.print("No se pueden restar las Matrices.\n\n\n|=======================================================|\n\n\n");
  131.         }
  132.     }
  133.  
  134.     private static void restar2(){ //Función resta con los operandos invertidos (Matriz B - Matriz A). Cambiar en el main().
  135.         System.out.print("Resta de Matriz:\n");
  136.         if(fila == fila2 && colum == colum2){
  137.             for(int x = 0; x < fila; x++){
  138.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  139.                 for(int y = 0; y < colum; y++){
  140.                     System.out.print((m2[x][y]) - (m1[x][y]) + "\t|\t");
  141.                 }
  142.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  143.             }
  144.         System.out.println("\n"); //Controla el espacio entre la función restar2() y el menú de opciones.
  145.         }
  146.         else{
  147.             System.out.print("No se pueden restar las Matrices.\n\n\n|=======================================================|\n\n\n");
  148.         }
  149.     }
  150.  
  151.     private static void multi() throws IOException{
  152.         System.out.print("Multiplicación de Matriz:\n");
  153.         if(colum == fila2){
  154.             double [][] r1 = new double[fila][colum2];
  155.             for(int x = 0; x < fila; x++){
  156.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  157.                 for(int y = 0; y < colum2; y++){
  158.                     for(int m = 0; m < colum; m++){
  159.                         r1[x][y] += m1[x][m] * m2[m][y];
  160.                     }
  161.                     System.out.print(r1[x][y] + "\t|\t");
  162.                 }
  163.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  164.             }
  165.         System.out.println("\n"); //Controla el espacio entre la función multi() y el menú de opciones.
  166.         }
  167.         else{
  168.             System.out.print("No se pueden multiplicar las Matrices.\n\n\n|=======================================================|\n\n\n");
  169.             String a = entrada.readLine();
  170.         }
  171.     }
  172.    
  173.     private static void multi2() throws IOException{ //Función multi() invertida.
  174.         System.out.print("Multiplicación de Matriz:\n");
  175.         if(colum2 == fila){
  176.             double [][] r1 = new double[fila2][colum];
  177.             for(int x = 0; x < fila2; x++){
  178.                 System.out.print("|\t"); //Agrega el carácter "|" al inicio de las filas de la Matriz, mientras que agrega una tabulación "\t".
  179.                 for(int y = 0; y < colum; y++){
  180.                     for(int m = 0; m < colum2; m++){
  181.                         r1[x][y] += m2[x][m] * m1[m][y];
  182.                     }
  183.                     System.out.print(r1[x][y] + "\t|\t");
  184.                 }
  185.             System.out.print("\n"); //Controla la separación entre filas de la Matriz.
  186.             }
  187.         System.out.println("\n"); //Controla el espacio entre la función multi() y el menú de opciones.
  188.         }
  189.         else{
  190.             System.out.print("No se pueden multiplicar las Matrices.\n\n\n|=======================================================|\n\n\n");
  191.             String a = entrada.readLine();
  192.         }
  193.     }
  194.  
  195.     private static void deta() throws IOException{ //Determinante de Matriz A.
  196.         if(fila == colum){
  197.             System.out.print("La determinante es: " + determinante(m1));
  198.             String a = entrada.readLine();
  199.         }
  200.         else{
  201.             System.out.print("La Matriz no tiene determinante.");
  202.             String a = entrada.readLine();
  203.         }
  204.     }
  205.    
  206.     private static void detb() throws IOException{ //Determinante de Matriz B.
  207.         if(fila2 == colum2){
  208.             System.out.print("La determinante es: " + determinante(m2));
  209.             String a = entrada.readLine();
  210.         }
  211.         else{
  212.             System.out.print("La Matriz no tiene determinante.");
  213.             String a = entrada.readLine();
  214.         }
  215.     }
  216.  
  217.     public static double determinante(double[][] matriz){ //Función para calcular Determinantes de Matriz A y Matriz B.
  218.         double det;
  219.         if(matriz.length == 2){
  220.             det = (matriz[0][0] * matriz[1][1]) - (matriz[1][0] * matriz[0][1]);
  221.             return det;
  222.         }
  223.         double suma = 0;
  224.         for(int i = 0; i < matriz.length; i++){
  225.             double[][] nm = new double[matriz.length - 1][matriz.length - 1];
  226.             for(int j = 0; j < matriz.length; j++){
  227.                 if(j != i){
  228.                     for(int k = 1; k < matriz.length; k++){
  229.                         int indice =- 1;
  230.                         if(j < i)
  231.                             indice = j;
  232.                         else if(j > i)
  233.                             indice = j - 1;
  234.                             nm[indice][k - 1] = matriz[j][k];
  235.                     }
  236.                 }
  237.             }
  238.             if(i % 2 == 0)
  239.                 suma += matriz[i][0] * determinante(nm);
  240.             else
  241.                 suma -= matriz[i][0] * determinante(nm);
  242.         }
  243.         return suma;
  244.     }
  245.    
  246.     private static void traa() throws IOException{ //Transpuesta de Matriz A. No usada en el programa.
  247.         System.out.print("La Matriz original:");
  248.         System.out.print("\n");
  249.         for(int x = 0; x < fila; x++){
  250.             for(int y = 0; y < colum; y++){
  251.                 System.out.print(m1[x][y] + "\t|\t");
  252.             }
  253.             System.out.print("\n");
  254.         }
  255.         System.out.print("\n\n");
  256.         System.out.print("La Matriz transpuesta:");
  257.         System.out.print("\n");
  258.         for(int x = 0; x < colum; x++){
  259.             for(int y = 0; y < fila; y++){
  260.                 System.out.print(m1[y][x] + "\t|\t");
  261.             }
  262.             System.out.print("\n");
  263.         }
  264.         String a = entrada.readLine();
  265.     }
  266.    
  267.     private static void trab() throws IOException{ //Transpuesta de Matriz B. No usada en el programa.
  268.         System.out.print("La Matriz original:");
  269.         System.out.print("\n");
  270.         for(int x = 0; x < fila2; x++){
  271.             for(int y = 0; y < colum2; y++){
  272.                 System.out.print(m2[x][y] + "\t|\t");
  273.             }
  274.             System.out.print("\n");
  275.         }
  276.         System.out.print("\n\n");
  277.         System.out.print("La Matriz transpuesta:");
  278.         System.out.print("\n");
  279.         for(int x = 0; x < colum2; x++){
  280.             for(int y = 0; y < fila2; y++){
  281.                 System.out.print(m2[y][x] + "\t|\t");
  282.             }
  283.             System.out.print("\n");
  284.         }
  285.         String a = entrada.readLine();
  286.     }
  287.    
  288.     private static void simA() throws IOException{ //Simetría de Matriz A.
  289.         boolean esSimetrica = true;
  290.         for(int i = 0; i < m1.length; i++){
  291.             for(int j = 0; j < m1[0].length; j++){
  292.                 if(m1[i][j] != m1[j][i]){
  293.                     System.out.println("La Matriz A no es simétrica.\n\n\n");
  294.                     esSimetrica = false;
  295.                     break;
  296.                 }
  297.             }
  298.             if(!esSimetrica){
  299.                 break;
  300.             }
  301.         }
  302.         if(esSimetrica){
  303.             System.out.println("La Matriz A es simétrica.\n\n\n");
  304.         }
  305.     }
  306.    
  307.     private static void simB() throws IOException{ //Simetría de Matriz B.
  308.         boolean esSimetrica = true;
  309.         for(int i = 0; i < m2.length; i++){
  310.             for(int j = 0; j < m2[0].length; j++){
  311.                 if(m2[i][j] != m2[j][i]){
  312.                     System.out.println("La Matriz B no es simétrica.\n\n\n");
  313.                     esSimetrica = false;
  314.                     break;
  315.                 }
  316.             }
  317.             if(!esSimetrica){
  318.                 break;
  319.             }
  320.         }
  321.         if(esSimetrica){
  322.             System.out.println("La Matriz B es simétrica.\n\n\n");
  323.         }
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment