document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* AUTOR: d3n3k4 (Dnk!)
  2.  * WEB: http://d3n3k4.blogspot.com/
  3.  * FECHA: 01/DIC/2010
  4.  * DESCRIPCION:
  5.  *  - Contiene varias clases que realizan calculos matriciales como:
  6.  *      - suma y resta de matrices.
  7.  *      - producto de matrices.
  8.  *      - calcular la matriz transpuesta de una matriz.
  9.  *      - comprobar si dos matrices son iguales.
  10.  *      - comprobar si una matriz es cuadrada o no.
  11.  *      - comprobar si una matriz es simetrica.
  12.  *  - Se iran aƱadiendo mas funcionalidades, como multiplicacion por una constante,
  13.  *    calcular determinante, rango, etc... y seran expuestas en mi web.
  14.  * NOTA: Este codigo es libre y puede ser usado,modificado... siempre y cuando se
  15.  *  mantenga los creditos y comentarios del autor.
  16.  */
  17. import java.util.Scanner;
  18.  
  19. public class CalC {
  20.     public static int[][] leeMatriz(int nFila, int nCol){
  21.         int[][] matriz = new int[nFila][nCol];
  22.         Scanner entrada = new Scanner(System.in);
  23.        
  24.         for (int i = 0; i < nFila; i++) {
  25.             for (int j = 0; j < nCol; j++) {
  26.                 matriz[i][j] = entrada.nextInt();
  27.             }
  28.         }
  29.         return matriz;
  30.     }
  31.     public static String escribeMatriz(int[][] matriz) {
  32.         String mat = "";
  33.         if (matriz != null) {
  34.             for (int i = 0; i < matriz.length; i++) {
  35.                 mat += "|   ";
  36.                 for (int j = 0; j < matriz[0].length; j++) {
  37.                     mat += matriz[i][j] + "  ";
  38.                 }
  39.                 mat += "   |\\n";
  40.             }
  41.         }
  42.         return mat;
  43.     }
  44.     public static int[][] sumaMatrices(int[][] mat1,int[][] mat2,boolean resta) {
  45.         int[][] matSal = null;
  46.        
  47.         if (mat1.length == mat2.length && mat1[0].length == mat2[0].length) {
  48.             if (resta == true) { //cambia de signo mat2 para despues sea restada.
  49.                 for (int i = 0; i < mat2.length; i++) {
  50.                     for (int j = 0; j < mat2[0].length;j++) {
  51.                         mat2[i][j] = mat2[i][j] * -1;
  52.                     }
  53.                 }
  54.             }
  55.             matSal = new int[mat1.length][mat1[0].length];
  56.             for (int i = 0; i < mat1.length; i++) {
  57.                 for (int j = 0; j < mat1[0].length; j++) {
  58.                     matSal[i][j] = mat1[i][j] + mat2[i][j];
  59.                 }
  60.             }
  61.         }
  62.         return matSal;
  63.     }
  64.     public static int[][] transpuesta(int[][] mat1) {
  65.         int[][] M_trans = new int[mat1[0].length][mat1.length];
  66.        
  67.         for (int i = 0; i < M_trans.length; i++) {
  68.             for (int j = 0; j < M_trans[0].length;j++) {
  69.                 M_trans[i][j] = mat1[j][i];
  70.             }
  71.         }
  72.         return M_trans;
  73.     }
  74.     public static int[][] productoMatriz(int[][] mat1,int[][] mat2) {
  75.         int[][] matSal = null;
  76.        
  77.         if (mat1[0].length == mat2.length) {
  78.             matSal = new int[mat1.length][mat2[0].length];
  79.            
  80.             for (int i = 0; i < matSal.length; i++) {
  81.                 for (int j = 0; j < matSal[0].length; j++) {
  82.                     for (int k = 0; k < mat2.length; k++) {
  83.                         matSal[i][j] += mat1[i][k] * mat2[k][j];
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         return matSal;
  89.     }
  90.     public static boolean esIgual(int[][] mat1,int[][] mat2) {
  91.         boolean igual = false;
  92.        
  93.         if (mat1.length == mat2.length && mat1[0].length == mat2[0].length) {
  94.             igual = true;
  95.             for (int i = 0; i < mat1.length; i++) {
  96.                 for (int j = 0; j < mat1.length; j++) {
  97.                     if (mat1[i][j] != mat2[i][j]) {
  98.                         igual = false;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.         return igual;
  104.     }
  105.     public static boolean esCuadrada(int[][] mat1) {
  106.         boolean cuadrada = false;
  107.         if (mat1.length == mat1[0].length) {
  108.             cuadrada = true;
  109.         }
  110.         return cuadrada;
  111.     }
  112.     public static boolean esSimetrica(int[][] mat1) {
  113.         boolean simetrica = false;
  114.         int[][] mat2 = transpuesta(mat1);
  115.         if (esIgual(mat1,mat2)) {
  116.             simetrica = true;
  117.         }
  118.         return simetrica;
  119.     }
  120.     public static void main(String[] args) {
  121.         /*int[][] matriz1,matriz2,resultado = null;
  122.         Scanner entrada = new Scanner(System.in);
  123.         int filas = 0, columnas = 0;*/
  124.        
  125.         /*System.out.println("Introduce el numero de filas");
  126.         filas = entrada.nextInt();
  127.         System.out.println("Introduce el numero de columnas");
  128.         columnas = entrada.nextInt();
  129.         System.out.println("Introduce la primera matriz:");
  130.         matriz1 = leeMatriz(filas,columnas);*/
  131.        
  132.         /*System.out.println("Introduce el numero de filas");
  133.         filas = entrada.nextInt();
  134.         System.out.println("Introduce el numero de columnas");
  135.         columnas = entrada.nextInt();
  136.         System.out.println("Introduce la primera matriz:");
  137.         matriz2 = leeMatriz(filas,columnas);*/
  138.        
  139.         //resultado = sumaMatrices(matriz1,matriz2,true);
  140.         //resultado = transpuesta(matriz1);
  141.         //resultado = productoMatriz(matriz1,matriz2);
  142.         /*if (resultado == null) {
  143.             System.out.println("Error");
  144.         }*/
  145.         /*if (esIgual(matriz1,matriz2)) {
  146.             System.out.println("Son iguales");
  147.         } else {
  148.             System.out.println("No son iguales");
  149.         }*/
  150.         /*if (esCuadrada(matriz1)) {
  151.             System.out.println("Es cuadrada");
  152.         } else {
  153.             System.out.println("No es cuadrada");
  154.         }*/
  155.         /*if (esSimetrica(matriz1)) {
  156.             System.out.println("Es simetrica");
  157.         } else {
  158.             System.out.println("No es simetrica");
  159.         }*/
  160.         //System.out.print(escribeMatriz(resultado));
  161.     }
  162.  
  163. }
');