Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Operaciones con Matrices.

Estas en el tema de Operaciones con Matrices. en el foro de C/C++ en Foros del Web. Les dejo un programa que hice hace un tiempo ... @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C: Ver original /*  * Nombre de programa: Operaciones con Matrices  * ...
  #1 (permalink)  
Antiguo 21/05/2011, 10:01
_Lx
 
Fecha de Ingreso: mayo-2011
Mensajes: 17
Antigüedad: 12 años, 11 meses
Puntos: 11
Información Operaciones con Matrices.


Les dejo un programa que hice hace un tiempo ...

Código C:
Ver original
  1. /*
  2.  * Nombre de programa: Operaciones con Matrices
  3.  * Descripcion: ...
  4.  *
  5.  *
  6.  * Autor: _Lx
  7.  * Fecha: N/A
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <conio.h>
  14.  
  15. int const LIMIT = 20 ;
  16. /*
  17. int const L1 = 20;
  18. int const L2 = 20;
  19.  
  20. void LEER_MTRZ( float X[L_1][L_2], int *DIM_X_A, int *DIM_X_B );
  21. void  MST_MTRZ( float X[L_1][L_2], int DIM_X_A, int DIM_X_B );
  22. */
  23. void LEERC_MTRZ( float X[LIMIT][LIMIT], int *DIM_X );
  24. void MSTC_MTRZ ( float X[LIMIT][LIMIT], int DIM_X );
  25. float DET_MTRZ ( float X[LIMIT][LIMIT], int DIM_X );
  26.  
  27. void TRANS_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT],
  28.                  int DIM_X_A, int DIM_X_B, int *DIM_Y_A, int *DIM_Y_B);
  29.                  
  30. int COFAC_MTRZ ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y );
  31. int ADJ_MTRZ   ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y );
  32. int INV_MTRZ   ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y );
  33.  
  34. int main()
  35. {  
  36.    float MTRZ_1[LIMIT][LIMIT], MTRZ_2[LIMIT][LIMIT];
  37.    int DIM_1, DIM_2, C, PX, PY;
  38.    
  39.    system("title Matrices ... ");
  40.    system("color 3f");  
  41.          
  42.    LEERC_MTRZ(MTRZ_1, &DIM_1);
  43.    MSTC_MTRZ( MTRZ_1, DIM_1 );
  44.    
  45.    if( DIM_1 > 0 ){
  46.    
  47.       printf("\nLa determinante de la matriz es: %.14lf\n", DET_MTRZ(MTRZ_1, DIM_1));  
  48.    }
  49.    if(INV_MTRZ( MTRZ_1, MTRZ_2, DIM_1, &DIM_2)){
  50.    
  51.       MSTC_MTRZ( MTRZ_2, DIM_2 );
  52.       printf("\nLa determinante de la matriz inversa es: %.14lf\n\n", DET_MTRZ(MTRZ_2, DIM_2));
  53.    }
  54.    system("pause");
  55.    return(0);  
  56. }
  57.  
  58. void LEERC_MTRZ( float X[LIMIT][LIMIT], int *DIM_X ){
  59.    
  60.    int DIM;
  61.    float TEMP;
  62.    
  63.    printf("\nIngrese orden de la matriz cuadrada: ");
  64.    scanf("%d", &DIM);
  65.    
  66.    printf("\n");
  67.    
  68.    if( DIM < LIMIT){
  69.      
  70.       if( DIM > 0 )
  71.      
  72.          printf("Ingresando elementos ... \n\n");
  73.                
  74.       for( int i = 0 ; i < DIM ; i++ ){
  75.          
  76.          for( int j = 0 ; j < DIM ; j++ ){
  77.            
  78.             printf("[%d][%d]: ", i+1, j+1);
  79.             scanf("%f", &TEMP);
  80.            
  81.             X[i][j] = TEMP;            
  82.          }
  83.       }
  84.       *DIM_X = DIM;
  85.    }
  86.    else{
  87.      
  88.       printf("Orden fuera de rango ... \n\n");
  89.       system("pause");
  90.       exit(0);
  91.    }                
  92. }
  93.  
  94. void MSTC_MTRZ( float X[LIMIT][LIMIT], int DIM_X ){
  95.    
  96.    if( DIM_X <= 0 )
  97.      
  98.       printf("\nLa matriz no tiene elementos\n\n");
  99.    
  100.    else{
  101.      
  102.       if( DIM_X <= 5 ){
  103.      
  104.          printf("\nMostrando Matriz (%d x %d):\n\n      ", DIM_X, DIM_X);
  105.    
  106.          for( int i = 0 ; i < DIM_X ; i++ ){
  107.          
  108.             printf("         [%2d] ", i+1);
  109.          }
  110.          printf("\n\n");
  111.    
  112.          for( int i = 0 ; i < DIM_X ; i++ ){
  113.          
  114.             printf("[%2d] {", i+1);
  115.          
  116.             for( int j = 0 ; j < DIM_X ; j++ ){
  117.            
  118.                printf("%14.3f", X[i][j]);          
  119.             }
  120.             printf(" }\n");
  121.          }
  122.       }
  123.    }  
  124. }
  125. /*
  126. void LEER_MTRZ( float X[L_1][L_2], int *DIM_X_A, int *DIM_X_B ){
  127.    
  128.    int DIM_1, DIM_2;
  129.    float TEMP;
  130.    
  131.    printf("\nIngrese la 1ra dimension de la matriz: ");
  132.    scanf("%d", &DIM_1);
  133.    
  134.    printf("\nIngrese la 2da dimension de la matriz: ");
  135.    scanf("%d", &DIM_2);
  136.    
  137.    printf("\n");
  138.    
  139.    if( DIM_1 < L_1 && DIM_2 < L_2){
  140.      
  141.       if( DIM_1 > 0 && DIM_2 > 0 ){
  142.      
  143.          printf("Ingresando elementos ... \n\n");
  144.                
  145.       for( int i = 0 ; i < DIM_1 ; i++ ){
  146.          
  147.          for( int j = 0 ; j < DIM_2 ; j++ ){
  148.            
  149.             printf("[%d][%d]: ", i+1, j+1);
  150.             scanf("%f", &TEMP);
  151.            
  152.             X[i][j] = TEMP;            
  153.          }
  154.       }
  155.       *DIM_X_A = DIM_1;
  156.       *DIM_X_B = DIM_2;
  157.    }
  158.    else{
  159.      
  160.       printf("Dimension(es) fuera de rango ...\n\n");
  161.       system("pause");
  162.       exit(0);
  163.    }                  
  164. }
  165.  
  166. void MST_MTRZ( float X[L_1][L_2], int DIM_X_A, int DIM_X_B ){
  167.    
  168.    if( DIM_X_A <= 0 || DIM_X_B <= 0)
  169.      
  170.       printf("\n\nLa matriz no tiene elementos");
  171.    
  172.    else{
  173.    
  174.       printf("\nMostrando Matriz:\n\n");
  175.    
  176.       for( int i = 0 ; i < DIM_X_A ; i++ ){
  177.          
  178.          printf("{ ");
  179.          
  180.          for( int j = 0 ; j < DIM_X_B ; j++ ){
  181.            
  182.             printf("%9.2f", X[i][j]);          
  183.          }
  184.          printf(" }\n");
  185.       }
  186.    }  
  187. }
  188. */
  189. void ELIMFC_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int FILA, int COLUM ){
  190.    
  191.    for( int i = FILA + 1 ; i < DIM_X ; i++ ){
  192.      
  193.       for( int j = COLUM + 1 ; j < DIM_X ; j++ ){
  194.          
  195.          Y[i-1][j-1] = X[i][j];        
  196.       }
  197.    }  
  198.    if( COLUM > 0 ){
  199.      
  200.       for( int i = FILA + 1 ; i < DIM_X ; i++ ){
  201.      
  202.          for( int j = 0; j < COLUM ; j++ ){
  203.          
  204.             Y[i-1][j] = X[i][j];    
  205.          }
  206.       }
  207.    }  
  208.    if( FILA > 0 ){
  209.    
  210.       for( int i = 0 ; i < FILA ; i++ ){
  211.      
  212.          for( int j = COLUM + 1 ; j < DIM_X ; j++ ){
  213.          
  214.             Y[i][j-1] = X[i][j];        
  215.          }
  216.       }  
  217.       if( COLUM > 0 ){
  218.      
  219.          for( int i = 0 ; i < FILA ; i++ ){
  220.      
  221.             for( int j = 0; j < COLUM ; j++ ){
  222.          
  223.                Y[i][j] = X[i][j];
  224.             }
  225.          }
  226.       }
  227.    }  
  228. }
  229.  
  230. float POW( float X, int N){
  231.    
  232.    float P = 1.;
  233.    
  234.    for( int i = 1 ; i <= N ; i++ ){
  235.      
  236.       P = P * X ;
  237.    }
  238.    return P;
  239. }
  240.  
  241. float DET_MTRZ( float X[LIMIT][LIMIT], int DIM_X){
  242.    
  243.    // Prb17.
  244.    
  245.    float DET = 0;
  246.    
  247.    float A[LIMIT][LIMIT];
  248.    
  249.    if( DIM_X == 1 )
  250.    
  251.       DET = X[0][0];
  252.    
  253.    else if( DIM_X == 2 )
  254.      
  255.       DET = X[0][0] * X[1][1] - X[0][1] * X[1][0] ;
  256.      
  257.    else if( DIM_X > 2 ){
  258.      
  259.       for( int i = 0 ; i < DIM_X ; i++ ){
  260.          
  261.          ELIMFC_MTRZ( X , A, DIM_X, 0, i);
  262.          
  263.          DET+= X[0][i] * POW( -1, i)* DET_MTRZ( A , DIM_X-1);                  
  264.       }
  265.    }
  266.    return DET;
  267. }
  268.  
  269. void TRANS_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT],
  270.                  int DIM_X_A, int DIM_X_B, int *DIM_Y_A, int *DIM_Y_B){
  271.    
  272.    // Prb11b.                  
  273.    // No es necesario que la matriz sea cuadrática para esta función.
  274.    
  275.    for( int i = 0 ; i < DIM_X_A ; i++ ){
  276.      
  277.       for( int j = 0 ; j < DIM_X_B ; j++ ){
  278.          
  279.          Y[j][i] = X[i][j];            
  280.       }
  281.    }
  282.    *DIM_Y_A = DIM_X_B;
  283.    *DIM_Y_B = DIM_X_A;      
  284. }
  285.  
  286. int COFAC_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y ){
  287.    
  288.    float A[LIMIT][LIMIT];
  289.    int R = 0;
  290.    
  291.    if( DIM_X > 1){
  292.    
  293.       for( int i = 0 ; i < DIM_X ; i++ ){
  294.      
  295.          for( int j = 0 ; j < DIM_X ; j++ ){
  296.          
  297.             ELIMFC_MTRZ( X , A, DIM_X, i, j);
  298.          
  299.             Y[i][j] = POW( -1, i + j )* DET_MTRZ( A , DIM_X-1);                        
  300.          }
  301.       }
  302.       R = 1;
  303.    }
  304.    *DIM_Y = DIM_X;
  305.    
  306.    return R;
  307. }
  308.  
  309. int ADJ_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y ){
  310.    
  311.    float Z[LIMIT][LIMIT];
  312.    int DIM_Z, R = 0;
  313.    
  314.    if( DIM_X > 1 ){
  315.    
  316.       COFAC_MTRZ( X , Z , DIM_X, &DIM_Z );
  317.       TRANS_MTRZ( Z , Y , DIM_Z, DIM_Z, DIM_Y, DIM_Y );
  318.      
  319.       R = 1;
  320.    }
  321.    else  
  322.      
  323.       *DIM_Y = DIM_Z;
  324.      
  325.    return R;
  326. }
  327.  
  328. int INV_MTRZ( float X[LIMIT][LIMIT], float Y[LIMIT][LIMIT], int DIM_X, int *DIM_Y ){
  329.    
  330.    // Prb12=16.
  331.    
  332.    float Z[LIMIT][LIMIT];
  333.    int DIM_Z, R = 0;  
  334.    
  335.    float DET = DET_MTRZ( X , DIM_X );
  336.    
  337.    if( DET != 0. || DIM_X < 1){
  338.      
  339.       if( DIM_X > 1 ){
  340.          
  341.          ADJ_MTRZ( X , Z , DIM_X, &DIM_Z );
  342.      
  343.          printf("\n\nCalculando la matriz inversa ... \n");
  344.        
  345.          for( int i = 0 ; i < DIM_Z ; i++ ){
  346.      
  347.             for( int j = 0 ; j < DIM_Z ; j++ ){
  348.          
  349.                Y[i][j] = Z[i][j] / DET;
  350.             }
  351.          }
  352.          R = 1;
  353.       }
  354.       else
  355.          
  356.          printf("\nLa matriz no tiene inversa ( Orden menor que 2 ).\n\n\n");
  357.    }
  358.    else
  359.    
  360.       printf("\nLa matriz no tiene inversa ( Determinante = 0 ).\n\n\n");
  361.    
  362.    *DIM_Y = DIM_Z;
  363.    
  364.    return R;
  365. }

Espero les sirva ...
  #2 (permalink)  
Antiguo 21/05/2011, 19:54
 
Fecha de Ingreso: septiembre-2007
Ubicación: PyRoot
Mensajes: 1.515
Antigüedad: 16 años, 7 meses
Puntos: 188
Respuesta: Operaciones con Matrices.

Esta excelente programa, mas preciso el calculo de matriz inversa.

Genial. Eres muy buen programado muchas felicidades.

Etiquetas: matrices, operaciones
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 18:44.