Advertisement
cotolonco

Clase Matriz

Sep 12th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.36 KB | None | 0 0
  1. package matriz;
  2.  
  3. public class Matriz {
  4.  
  5.     private double matriz[][];
  6.     private int fila;
  7.     private int columna;
  8.    
  9.     public Matriz( int fila, int columna ){
  10.         if( fila >= 0 )
  11.             this.fila = fila;
  12.         else
  13.             this.fila = 1;
  14.         if( columna >= 0 )
  15.             this.columna = columna;
  16.         else
  17.             this.columna = 1;
  18.         matriz = new double[ this.fila ][ this.columna ];
  19.     }
  20.    
  21.     public Matriz( Matriz m ){
  22.         if ( m != null ){
  23.             this.fila = m.getFila();
  24.             this.columna = m.getColumna();
  25.             matriz = new double[ m.getFila() ][ m.getColumna() ];
  26.         }else{
  27.             this.fila = 1;
  28.             this.columna = 1;
  29.             matriz = new double[ 1 ][ 1 ];
  30.         }
  31.     }
  32.    
  33.     public void iniciarRandom(){
  34.         for( int i = 0; i < matriz.length; i++ ){
  35.             for( int j = 0; j < matriz[ i ].length; j++ ){
  36.                 matriz[ i ][ j ] = ( int ) ( Math.random() * 5 );
  37.             }
  38.         }
  39.     }
  40.    
  41.     public String dimension(){
  42.         return ( fila + " x " + columna );
  43.     }
  44.    
  45.     public int orden(){
  46.         if ( esCuadrada() )
  47.             return this.fila;
  48.         return 0;
  49.     }
  50.    
  51.     public boolean esIgual( Matriz m ){
  52.         if( this.fila != m.getFila() || this.columna != m.getColumna() )
  53.             return false;
  54.         for( int i = 0; i < matriz.length; i++ ){
  55.             for( int j = 0; j < matriz[ i ].length; j++ ){
  56.                 if( matriz[ i ][ j ] != m.matriz[ i ][ j ]){
  57.                     return false;
  58.                 }
  59.             }
  60.         }
  61.         return true;
  62.     }
  63.  
  64.     public String tipo(){
  65.         if ( esVectorFila() )
  66.             return "Matriz Fila";
  67.         if ( esVectorColumna() )
  68.             return "Matriz Columna";
  69.         if ( orden() == 0 )
  70.             return "Matriz Rectangular";
  71.         if ( esVacia() )
  72.             return "Matriz Nula";
  73.         String frase = "Matriz Cuadrada";
  74.         if( esCuadrada() ){
  75.             if ( esIdentidad() )
  76.                 frase += ", Identidad";
  77.             else if ( esEscalar() )
  78.                 frase += ", Escalar";
  79.             else if ( esDiagonal() )
  80.                 frase += ", Diagonal";
  81.             else if ( esTriangularSuperior( ) )
  82.                 frase += ", Triangular Superior";
  83.             else if ( esTriangularInferior() )
  84.                 frase += ", Triangular Inferior";
  85.            
  86.             if ( esSimetrica() )
  87.                 frase += ", Simetrica";
  88.             else if ( esAntisimetrica() )
  89.                 frase += ", AntiSimetrica";
  90.            
  91.             if ( esIdempotente())
  92.                 frase += ", Idempotente";
  93.            
  94.             if ( this.esInvolutiva() )
  95.                 frase += ", Involutiva";
  96.            
  97.             if ( esOrtogonal() )
  98.                 frase += ", Ortogonal";
  99.             return frase;      
  100.         }
  101.         return "No encontrado";
  102.     }
  103.    
  104.     public boolean esVacia(){
  105.         for( int i = 0; i < matriz.length; i++ ){
  106.             for( int j = 0; j < matriz[ j ].length; j++ ){
  107.                 if( matriz[ i ][ j ] != 0 )
  108.                     return false;
  109.             }
  110.         }
  111.         return true;
  112.     }
  113.    
  114.     public boolean esVectorFila(){
  115.         return ( fila == 1 );
  116.     }
  117.    
  118.     public boolean esVectorColumna(){
  119.         return ( columna == 1 );
  120.     }
  121.    
  122.     public boolean esCuadrada(){
  123.         return ( fila == columna );
  124.     }
  125.     /*
  126.     SI ES CUADRADA!!!!
  127.     */
  128.    
  129.     public boolean esTriangularInferior(){
  130.         if( !esCuadrada() )
  131.             return false;
  132.         for( int i = 0; i < matriz.length; i++ ){
  133.             for( int j = ( i + 1 ); j < matriz[ i ].length; j++ ){
  134.                 if ( matriz[ i ][ j ] != 0 )
  135.                     return false;
  136.             }
  137.         }
  138.         return true;
  139.     }
  140.    
  141.     public boolean esTriangularSuperior(){
  142.         if( !esCuadrada() )
  143.             return false;
  144.         for( int i = 1; i < matriz.length; i++ ){
  145.             for( int j = 0; j < i; j++ ){
  146.                 if ( matriz[ i ][ j ] != 0 )
  147.                     return false;
  148.             }
  149.         }
  150.         return true;
  151.     }
  152.    
  153.     public boolean esDiagonal(){
  154.         return ( esTriangularSuperior() && esTriangularInferior() );
  155.     }
  156.    
  157.     //matriz diagonal pero todos los de la diagonal iguales
  158.     public boolean esEscalar(){
  159.         if ( !esDiagonal() )
  160.             return false;
  161.         double elemento = matriz[ 0 ][ 0 ];
  162.         for( int i = 1; i < matriz.length; i++ ){
  163.             if ( elemento != matriz[ i ][ i ] )
  164.                 return false;
  165.             elemento = matriz[ i ][ i ];
  166.         }
  167.         return true;
  168.     }
  169.     //matriz diagonal pero todos los de la diagonal son 1
  170.     public boolean esIdentidad(){
  171.         if ( !esEscalar() )
  172.             return false;
  173.         return ( matriz[ 0 ][ 0 ] == 1 );
  174.     }
  175.    
  176.     //si tiene inversa
  177.     public boolean esRegular(){
  178.         return ( determinante() != 0.0 );
  179.     }
  180.     //si no tiene inversa
  181.     public boolean esSingular(){
  182.         return !esRegular();
  183.     }
  184.     //Si A^2 = A
  185.     public boolean esIdempotente(){
  186.         if ( !esCuadrada() )
  187.             return false;
  188.         Matriz m = copiar();
  189.         m.producto( m );
  190.         return ( m.esIgual( this ) );
  191.     }
  192.     // A^2 = I
  193.     public boolean esInvolutiva(){
  194.         if ( !esCuadrada() )
  195.             return false;
  196.         Matriz m = copiar();
  197.         m.producto( m );
  198.         return ( m.esIgual( m.getIdentidad() ) );
  199.     }
  200.    
  201.     // A = A^t
  202.     public boolean esSimetrica(){
  203.         Matriz t = copiar();
  204.         t.traspuesta();
  205.         return ( t.esIgual( this ) );
  206.     }
  207.     // A = - A ^t
  208.     public boolean esAntisimetrica(){
  209.         Matriz t = copiar();
  210.         t.traspuesta();
  211.         t.producto( -1.0 );
  212.         return ( t.esIgual( this ) );
  213.     }
  214.    
  215.     // A * A^t = I
  216.     public boolean esOrtogonal(){
  217.         Matriz t = copiar();
  218.         Matriz a = copiar();
  219.         t.traspuesta();
  220.         a.producto( t );
  221.         return ( a.esIgual( a.getIdentidad() ) );
  222.        
  223.     }
  224.    
  225.     /*
  226.     FIN SI ES CUADRADA!!!
  227.     */
  228.    
  229.     public void traspuesta(){
  230.         double copiaMatriz[][] = matriz;
  231.         setDimension( columna, fila );
  232.        
  233.         for( int i = 0; i < copiaMatriz.length; i++ ){
  234.             for( int j = 0; j < copiaMatriz[ i ].length; j++ ){
  235.                 matriz[ j ][ i ] = copiaMatriz[ i ][ j ];
  236.             }
  237.         }
  238.     }
  239.    
  240.     /* FALTA: */
  241.     public int rango(){
  242.         return 0;  
  243.     }
  244.    
  245.     public double determinante(){
  246.         if ( !esCuadrada() ){
  247.             System.err.println( "No se puede calcular determinante" );
  248.             return 0.0;
  249.         }
  250.         return 0.0;
  251.     }
  252.     /* FIN FALTA */
  253.    
  254.     public Matriz copiar( ){
  255.         Matriz m;
  256.         m = new Matriz( fila, columna );
  257.         for( int i = 0; i < matriz.length; i++ ){
  258.             for( int j = 0; j < matriz[ i ].length; j++ ){
  259.                 m.matriz[ i ][ j ] = matriz[ i ][ j ];
  260.             }
  261.         }
  262.         return m;
  263.     }
  264.    
  265.     public void copiar( Matriz m ){
  266.         setDimension( m.getFila(), m.getColumna() );
  267.         for( int i = 0; i < matriz.length; i++ ){
  268.             for( int j = 0; j < matriz[ i ].length; j++ ){
  269.                 matriz[ i ][ j ] = m.matriz[ i ][ j ];
  270.             }
  271.         }
  272.     }
  273.    
  274.     //Puede devolverse otra Matriz igual a gusto de uno.
  275.     public void sumar( Matriz m ){
  276.         if( fila != m.getFila() || columna != m.getColumna() ){
  277.             System.err.println( "No puede sumarse" );
  278.         }else{
  279.             for( int i = 0; i < matriz.length; i++ ){
  280.                 for( int j = 0; j < matriz[ i ].length; j++ ){
  281.                     matriz[ i ][ j ] += m.getMatriz()[ i ][ j ];
  282.                 }
  283.             }
  284.         }
  285.     }
  286.    
  287.     //Puede devolverse otra Matriz igual a gusto de uno.
  288.     public void restar( Matriz m ){
  289.         if( fila != m.getFila() || columna != m.getColumna() ){
  290.             System.err.println( "No puede restarse" );
  291.         }else{
  292.             for( int i = 0; i < matriz.length; i++ ){
  293.                 for( int j = 0; j < matriz[ i ].length; j++ ){
  294.                     matriz[ i ][ j ] -= m.getMatriz()[ i ][ j ];
  295.                 }
  296.             }
  297.         }
  298.     }
  299.    
  300.     public void producto( double escalar ){
  301.         //Evita multiplicar uno a cada uno ( mismo resultado )
  302.         if ( escalar != 1.0 ){
  303.             for( int i = 0; i < matriz.length; i++ ){
  304.                 for( int j = 0; j < matriz[ i ].length; j++ ){
  305.                     matriz[ i ][ j ] *= escalar;
  306.                 }
  307.             }
  308.         }
  309.     }
  310.    
  311.     public void producto( Matriz m ){
  312.         if ( columna != m.getFila() ){
  313.             System.err.println( "No pueden multiplicarse" );
  314.         }else{
  315.             double copiaMatriz[][] = matriz;
  316.             setDimension( fila, m.getColumna() );
  317.             double suma;
  318.             //Para cada fila de A
  319.             for( int i = 0; i < copiaMatriz.length; i++ ){
  320.                 //Para cada columna de B
  321.                 for( int j = 0; j < m.getColumna(); j++ ){
  322.                     suma = 0.0;
  323.                     //Se multiplica la fila A por las cada columna de B
  324.                     for( int k = 0; k < m.getFila(); k++ ){
  325.                        suma = suma + copiaMatriz[ i ][ k ] * m.matriz[ k ][ j ];
  326.                     }
  327.                     matriz[ i ][ j ] = suma;
  328.                 }
  329.             }
  330.         }
  331.     }
  332.    
  333.     public void ordenarFila( int fila ){
  334.         if( verificarFila( fila ) ){
  335.             double aux = 0;
  336.             for( int i = 0; i < columna - 1 ; i++ ){
  337.                 for( int j = ( i + 1 ); j < columna; j++ ){
  338.                     if ( matriz[ fila ][ i ] > matriz[ fila ][ j ] ){
  339.                         aux = matriz[ fila ][ i ];
  340.                         matriz[ fila ][ i ] = matriz[ fila ][ j ];
  341.                         matriz[ fila ][ j ] = aux;
  342.                     }
  343.                 }
  344.             }
  345.         }
  346.     }
  347.    
  348.     public void ordenarColumna( int columna ){
  349.         if( verificarColumna( columna ) ){
  350.             double aux;
  351.             for( int i = 0; i < fila - 1 ; i++ ){
  352.                 for( int j = ( i + 1 ); j < fila; j++ ){
  353.                     if ( matriz[ i ][ columna ] > matriz[ j ][ columna ] ){
  354.                         aux = matriz[ i ][ columna ];
  355.                         matriz[ i ][ columna ] = matriz[ j ][ columna ];
  356.                         matriz[ j ][ columna ] = aux;
  357.                     }
  358.                 }
  359.             }
  360.         }
  361.     }
  362.    
  363.     public void ordenarMatriz( ){
  364.         double aux;
  365.         //Para cada dato:
  366.         for( int i = 0; i < fila; i++ ){
  367.             for( int j = 0; j < columna; j++ ){
  368.                 //Se busca el menor y se cambian
  369.                 for( int x = 0; x < fila; x++ ){
  370.                     for( int y = 0; y < columna; y++ ){
  371.                         if ( matriz[ i ][ j ] < matriz[ x ][ y ] ){
  372.                             aux = matriz[ i ][ j ];
  373.                             matriz[ i ][ j ] = matriz[ x ][ y ];
  374.                             matriz[ x ][ y ]= aux;
  375.                         }
  376.                     }
  377.                 }
  378.             }
  379.         }
  380.     }
  381.    
  382.     public double sumarFila( int fila ){
  383.         double suma = 0.0;
  384.         if ( verificarFila( fila ) ){
  385.             for( int j = 0; j < columna; j++ )
  386.                 suma += matriz[ fila ][ j ];
  387.            
  388.         }
  389.         return suma;
  390.     }
  391.    
  392.     public double sumarColumna( int columna ){
  393.         double suma = 0.0;
  394.         if ( verificarColumna( columna ) ){
  395.             for( int i = 0; i < columna; i++ )
  396.                 suma += matriz[ i ][ columna ];
  397.            
  398.         }
  399.         return suma;
  400.     }
  401.    
  402.     public double sumarTotal( ){
  403.         double suma = 0.0;
  404.         for( int i = 0; i < fila; i++ ){
  405.             for( int j = 0; j < columna; j++ ){
  406.                 suma += matriz[ i ][ j ];
  407.             }
  408.         }
  409.         return suma;
  410.     }
  411.    
  412.     public boolean existeValor( int valor ){
  413.         for( int i = 0; i < fila; i++ ){
  414.             for( int j = 0; j < columna; j++ ){
  415.                 if( matriz[ i ][ j ] == valor )
  416.                     return true;
  417.             }
  418.         }
  419.         return false;
  420.     }
  421.    
  422.     public int vecesExiste( int valor ){
  423.         int veces = 0;
  424.         if ( existeValor( valor ) ){
  425.             for( int i = 0; i < fila; i++ ){
  426.                 for( int j = 0; j < columna; j++ ){
  427.                     if( matriz[ i ][ j ] == valor )
  428.                         veces++;
  429.                 }
  430.             }
  431.         }
  432.         return veces;
  433.     }
  434.    
  435.     public void insertarElemento( int elemento, int fila, int columna ){
  436.         if ( verificarFila( fila ) && verificarColumna( columna ) ){
  437.             matriz[ fila ][ columna ] = elemento;
  438.         }
  439.     }
  440.    
  441.     public void eliminarElemento( int elemento, int fila, int columna ){
  442.         if ( verificarFila( fila ) && verificarColumna( columna ) ){
  443.             matriz[ fila ][ columna ] = 0.0;
  444.         }
  445.     }
  446.    
  447.     public void vaciarMatriz(){
  448.         matriz = new double[ fila ][ columna ];
  449.     }
  450.    
  451.     public void cambiarFilas( int fila1, int fila2 ){
  452.         if ( verificarFila( fila1 ) && verificarFila( fila2 ) ){
  453.             double aux;
  454.             for( int j = 0; j < columna; j++ ){
  455.                 aux = matriz[ fila1 ][ j ];
  456.                 matriz[ fila1 ][ j ] = matriz[ fila2 ][ j ];
  457.                 matriz[ fila2 ][ j ] = aux;
  458.             }
  459.         }else{
  460.             System.err.println( "No pueden intercambiarse" );
  461.         }
  462.     }
  463.    
  464.     public void cambiarColumnas( int columna1, int columna2 ){
  465.         if ( verificarColumna( columna1 ) && verificarColumna( columna2 ) ){
  466.             double aux;
  467.             for( int i = 0; i < fila; i++ ){
  468.                 aux = matriz[ i ][ columna1 ];
  469.                 matriz[ i ][ columna1 ] = matriz[ i ][ columna2 ];
  470.                 matriz[ i ][ columna2 ] = aux;
  471.             }
  472.         }else{
  473.             System.err.println( "No pueden intercambiarse" );
  474.         }
  475.     }
  476.    
  477.     public void insertarFila(){
  478.         insertarFilas( 1 );
  479.     }
  480.    
  481.     public void insertarFilas( int n ){
  482.         if ( n > 0 )
  483.             setDimension( fila + n, columna );
  484.     }
  485.    
  486.     public void insertarColumna(){
  487.         insertarColumnas( 1 );
  488.     }
  489.    
  490.     public void insertarColumnas( int n ){
  491.         setDimension( fila, columna + n );
  492.     }
  493.    
  494.     public void eliminarFila(){
  495.         eliminarFilas( 1 );
  496.     }
  497.    
  498.     public void eliminarFilas( int n ){
  499.         setDimension( fila - n, columna);
  500.     }
  501.    
  502.     public void eliminarColumna(){
  503.         eliminarColumnas( 1 );
  504.     }
  505.    
  506.     public void eliminarColumnas( int n ){
  507.         setDimension( fila, columna - n );
  508.     }
  509.    
  510.     public double[][] getMatriz() {
  511.         return matriz;
  512.     }
  513.  
  514.     public void setMatriz( double[][] matriz ) {
  515.         this.matriz = matriz;
  516.     }
  517.  
  518.     public void setDimension( int fila, int columna ){
  519.         if ( fila > 0 )
  520.             this.fila = fila;
  521.         if( columna > 0 )
  522.             this.columna = columna;
  523.        
  524.         double copiaMatriz[][] = matriz;
  525.         matriz = new double[ this.fila ][ this.columna ];
  526.         for( int i = 0; i < copiaMatriz.length; i++ ){
  527.             for( int j = 0; j < copiaMatriz[ i ].length; j++ ){
  528.                 if ( i < this.fila && j < this.columna )
  529.                     matriz[ i ][ j ] = copiaMatriz[ i ][ j ];
  530.             }
  531.         }
  532.        
  533.     }
  534.    
  535.     public Matriz getIdentidad(){
  536.         Matriz identidad = new Matriz( fila, columna );
  537.         for( int i = 0; i < fila; i++ )
  538.             identidad.matriz[ i ][ i ] = 1;
  539.         return identidad;
  540.     }
  541.    
  542.     public double[] getVectorFila( int fila ){
  543.         if ( verificarFila( fila ) )
  544.             return matriz[ fila ];
  545.         return null;
  546.     }
  547.    
  548.     public double[] getVectorColumna( int columna ){
  549.         if( verificarColumna( columna ) ){
  550.             double vector[] = new double[ this.fila ];
  551.             for( int i = 0; i < vector.length; i++ )
  552.                 vector[ i ] = matriz[ i ][ columna ];
  553.             return vector;
  554.         }
  555.         return null;
  556.     }
  557.    
  558.     public boolean verificarColumna( int columna ){
  559.         return ( columna >= 0 && columna < this.columna );
  560.     }
  561.    
  562.     public boolean verificarFila( int fila ){
  563.         return ( fila >= 0 && fila < this.fila );
  564.     }
  565.    
  566.    
  567.     public int getFila() {
  568.         return fila;
  569.     }
  570.  
  571.     public void setFila(int fila) {
  572.         if ( fila > 0 )
  573.             this.fila = fila;
  574.     }
  575.  
  576.     public int getColumna() {
  577.         return columna;
  578.     }
  579.  
  580.     public void setColumna(int columna) {
  581.         if ( columna > 0 )
  582.             this.columna = columna;
  583.     }
  584.    
  585.     public void imprimir(){
  586.         System.out.println();
  587.         for( int i = 0; i < matriz.length; i++ ){
  588.             for( int j = 0; j < matriz[ i ].length; j++ ){
  589.                 System.out.print( matriz[ i ][ j ] + "  " );
  590.             }
  591.             System.out.println();
  592.         }
  593.         System.out.println();
  594.     }
  595.    
  596.     @Override
  597.     public String toString(){
  598.         return ( "Tipo: " + tipo() + ", Dimension: " + dimension() +
  599.                 ", Orden: " + orden() );
  600.     }
  601.    
  602.    
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement