Advertisement
Guest User

Clase Matriz con template

a guest
Oct 4th, 2011
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <iomanip>
  6. #include <cstdlib>
  7. #include <cassert>
  8. #include <cmath>
  9.  
  10. template<typename T> class Matriz;
  11. template<typename T> ostream& operator << (ostream &salida, Matriz<T> &z);
  12. template<typename T> istream& operator >> (istream &entrada, Matriz<T> &z);
  13.  
  14. template<typename T> Matriz<T> operator + (Matriz<T> x, Matriz<T> y);
  15. template<typename T> Matriz<T> operator - (Matriz<T> x, Matriz<T> y);
  16. template<typename T> Matriz<T> operator * (Matriz<T> x, Matriz<T> y);
  17. template<typename T> Matriz<T> operator * (T x, Matriz<T> y);
  18.  
  19. template <class T>
  20.  
  21. class Matriz {
  22.  
  23. private:
  24.  
  25.     T **a;
  26.     int rows, cols;
  27.  
  28. public:
  29.  
  30.     Matriz(int m, int n);
  31.     ~Matriz();  // Destructor de la clase
  32.  
  33.     void initMatriz(T **x);
  34.     void inverMat(T **a);
  35.     void autovecMat(T **a);
  36.     void autovalMat(T **a);
  37.     void transpMat(Matriz<T> x);
  38.    
  39.     T deterMat();
  40.  
  41.     friend ostream& operator << <> (ostream &salida, Matriz<T> &z); //sobrecarga del operador <<
  42.     friend istream& operator >> <> (istream &entrada, Matriz<T> &z); //sobrecarga del operador >>
  43.  
  44.     friend Matriz   operator  + <> (Matriz<T> x, Matriz<T> y); //sobrecarga al operador + para sumar matrices
  45.     friend Matriz   operator  - <> (Matriz<T> x, Matriz<T> y); //sobrecarga al operador - para sumar matrices
  46.     friend Matriz   operator  * <> (Matriz<T> x, Matriz<T> y); //sobrecarga al operador * para multiplicar matrices
  47.     friend Matriz   operator  * <> (T x, Matriz<T> y); //sobrecarga al operador * para multiplicar escalar*matriz
  48.  
  49.  
  50. };
  51.  
  52. //Constructor por parametros
  53.  
  54. template<typename T>
  55.  
  56. Matriz<T>::Matriz(int m , int n){
  57.  
  58.     int i;
  59.  
  60.     rows = m;
  61.  
  62.     cols = n;
  63.  
  64.     a = new T*[rows];
  65.  
  66.     for(i = 0; i< rows; i++){
  67.  
  68.         a[i] = new T[cols];
  69.  
  70.     }
  71.  
  72. }
  73.  
  74. template <typename T>
  75.  
  76. Matriz<T>::~Matriz() {}
  77.  
  78. template<typename T>
  79.  
  80. void Matriz<T>::initMatriz(T **x){
  81.  
  82.     int i,j;
  83.  
  84.     for(i=0; i < rows; i++){
  85.  
  86.             for(j=0; j < cols; j++){
  87.  
  88.                 a[i][j] = x[i][j];
  89.        
  90.         }
  91.     }
  92.    
  93.  
  94. }
  95.  
  96. template<typename T>
  97.  
  98. istream &operator >> (istream &entrada, Matriz<T> &z){//sobrecarga del operador >>
  99.  
  100.     cout << "\nIntroduzca los valores a traves del teclado:\n\n";
  101.  
  102.     int i, j;
  103.  
  104.     for (i = 0; i < z.rows; i++) {
  105.  
  106.         for (j = 0; j < z.cols; j++) {
  107.  
  108.             cout << "A[" << i+1 <<"][" << j+1 << "] = ? ";
  109.  
  110.             entrada >> z.a[i][j];
  111.  
  112.         }
  113.      
  114.     }
  115.      
  116.     return entrada;
  117.  
  118. }
  119.  
  120. template<typename T>
  121.  
  122. ostream &operator << (ostream &salida, Matriz<T> &z){//sobrecarga del operador <<
  123.  
  124.     salida.setf(ios::fixed);
  125.     salida.precision(4);   
  126.  
  127.     int i, j;
  128.  
  129.     for (i = 0; i < z.rows; i++) {
  130.  
  131.         for (j = 0; j < z.cols; j++) {
  132.  
  133.             salida << setw(10) << z.a[i][j];
  134.  
  135.         }
  136.  
  137.         salida << "\n";
  138.      
  139.     }
  140.      
  141.     return salida;
  142.  
  143. }
  144.  
  145. template<typename T>
  146.  
  147. Matriz<T> operator +(Matriz<T> x, Matriz<T> y){//sobrecarga al operador + para sumar matrices
  148.  
  149.     assert(x.rows == y.rows || x.cols == y.cols);
  150.  
  151.     Matriz<T> sum_mat(x.rows, x.cols);
  152.  
  153.     int i,j;
  154.  
  155.     for(i=0; i < x.rows; i++){
  156.  
  157.         for(j=0; j < x.cols; j++){
  158.  
  159.             sum_mat.a[i][j] = x.a[i][j] + y.a[i][j];
  160.        
  161.         }
  162.     }
  163.    
  164.     return sum_mat;
  165.  
  166. }
  167.  
  168. template<typename T>
  169.  
  170. Matriz<T> operator -(Matriz<T> x, Matriz<T> y){//sobrecarga al operador + para restar matrices
  171.  
  172.     assert(x.rows == y.rows || x.cols == y.cols);
  173.  
  174.     Matriz<T> rest_mat(x.rows, x.cols);
  175.  
  176.     int i,j;
  177.  
  178.     for(i=0; i < x.rows; i++){
  179.  
  180.         for(j=0; j < x.cols; j++){
  181.  
  182.             rest_mat.a[i][j] = x.a[i][j] + y.a[i][j];
  183.        
  184.         }
  185.     }
  186.    
  187.     return rest_mat;
  188.  
  189. }
  190.  
  191. template<typename T>
  192.  
  193. Matriz<T> operator *(Matriz<T> x, Matriz<T> y){//sobrecarga al operador * para multiplicar matrices
  194.  
  195.     assert(x.cols == y.rows);
  196.  
  197.     Matriz<T> mult_mat(x.rows, y.cols);
  198.  
  199.     int i,j,k;
  200.  
  201.     for(i=0; i < x.rows; i++){
  202.  
  203.         for(j=0; j < x.cols; j++){
  204.  
  205.             mult_mat.a[i][j] = 0;
  206.  
  207.             for(k=0; k < x.cols; k++){
  208.  
  209.                 mult_mat.a[i][j] += x.a[i][k] * y.a[k][j];
  210.            
  211.             }
  212.        
  213.         }
  214.     }
  215.    
  216.     return mult_mat;
  217.  
  218. }
  219.  
  220. template<typename T>
  221.  
  222. Matriz<T> operator *(T x, Matriz<T> y){//sobrecarga al operador * para multiplicar escalar*matriz
  223.  
  224.     Matriz<T> esc_mat(y.rows, y.cols);
  225.  
  226.     int i,j;
  227.  
  228.     for(i=0; i < y.rows; i++){
  229.  
  230.         for(j=0; j < y.cols; j++){
  231.  
  232.             esc_mat.a[i][j] = x*y.a[i][j];
  233.        
  234.         }
  235.     }
  236.    
  237.     return esc_mat;
  238.  
  239. }
  240.  
  241. template<typename T>
  242.  
  243. void Matriz<T>::inverMat(T **f) {
  244.  
  245. // Algoritmo para la eliminación simple de Gauss
  246.  
  247.     int i, j, k, n= rows, m = cols;
  248.  
  249.     assert (rows == cols);
  250.  
  251.     double factor;
  252.  
  253.     double **L, **ainv, **z, *D, *X;
  254.    
  255.     X = new double [n]; D = new double [n];
  256.  
  257.     L = new double* [n]; ainv = new double* [n]; z = new double* [n];
  258.    
  259.     for (j = 0; j < n; j++) {
  260.  
  261.         L[j] = new double [n]; ainv[j] = new double [n]; z[j] = new double [n];
  262.  
  263.     }
  264.  
  265.     for(i=0; i < n; i++){
  266.  
  267.         for(j=0; j < n; j++){
  268.  
  269.             z[i][j] = f[i][j];
  270.        
  271.         }
  272.     }
  273.  
  274.  
  275.     for (k = 0; k < n - 1; k++) {
  276.        
  277.         for (i = k+1; i < n;  i++) {
  278.  
  279.             factor = z[i][k]/z[k][k];
  280.  
  281.             for (j = k+1; j < n + 1; j++) {
  282.  
  283.                 z[i][j] = z[i][j] - factor * z[k][j];
  284.  
  285.             }
  286.        
  287.  
  288.  
  289.         }
  290.  
  291.     }
  292.  
  293. // Cálculo del determinante
  294.        
  295.         double determ;
  296.  
  297.         determ = 1.;
  298.  
  299.     for (i = 0; i < n; i++) {
  300.  
  301.         determ = determ * z[i][i];
  302.  
  303.     }
  304.  
  305.     assert(determ != 0);
  306.  
  307. // Rutina para determinar las matrices L (inferior) y U (superior) de la
  308. // descomposición LU
  309.  
  310.  
  311.         for (i = 0; i < n; i++) {
  312.  
  313.                for (j = 0; j < n; j++) {
  314.  
  315.                   if (i > j) {
  316.      
  317.                       L[i][j] = z[i][j]/z[j][j];
  318.  
  319.                       z[i][j] = 0;
  320.  
  321.                    }
  322.  
  323.  
  324.                }
  325.  
  326.         }
  327.  
  328.  
  329.        for (i = 0; i < n; i++) {
  330.  
  331.             for (j = 0; j < n; j++) {
  332.  
  333.                   L[j][j] = 1;
  334.  
  335.             }
  336.  
  337.         }
  338.  
  339.  
  340. // Implementación de la rutina para el cálculo de la inversa
  341.  
  342.  
  343.  for (k = 0; k < n; k++) {
  344.  
  345.  
  346. // Esta rutina inicializa los L[i][n] para ser utilizados con la matriz L
  347.  
  348.  
  349.           for (i = 0; i < n; i++) {
  350.  
  351.                if (i == k) L[i][n] = 1;
  352.  
  353.                else  L[i][n] = 0;
  354.  
  355.            }
  356.  
  357. // Esta función implementa la sustitución hacia adelante con la matriz L y los L[i][n]
  358. // que produce la rutina anterior
  359.  
  360.   double sum;
  361.  
  362.   D[0] = L[0][n];
  363.  
  364.   for (i = 1; i < n; i++) {
  365.  
  366.        sum = 0;
  367.  
  368.        for (j = 0; j < i; j++) {
  369.  
  370.             sum = sum + L[i][j]*D[j];
  371.  
  372.        }
  373.  
  374.         D[i] = L[i][n] - sum;
  375.  
  376.    }
  377.  
  378.  
  379.  
  380. // Esta rutina asigna los D[i] que produce forward para ser utilizados con la matriz U
  381.  
  382.   for (i = 0; i < n; i++) {
  383.  
  384.           z[i][n] = D[i];
  385.  
  386.   }
  387.  
  388. // Rutina que aplica la sustitución hacia atras
  389.  
  390.  
  391.  X[n-1] = z[n-1][n]/z[n-1][n-1];
  392.  
  393.  // Determinación de las raíces restantes
  394.  
  395.  
  396.   for (i = n - 2; i > -1; i--) {
  397.  
  398.         sum = 0;
  399.  
  400.         for (j = i+1; j < n; j++) {
  401.  
  402.               sum = sum + z[i][j]*X[j];
  403.  
  404.          }
  405.  
  406.          X[i] = (z[i][n] - sum)/z[i][i];
  407.  
  408.    }
  409.  
  410.  
  411. // Esta rutina asigna los X[i] que produce Sustituir como los elementos de la matriz inversa
  412.  
  413.   for (i = 0; i < n; i++) {
  414.  
  415.          ainv[i][k] = X[i];
  416.  
  417.   }
  418.  
  419.  }   // llave de cierre del for para k
  420.  
  421.     for(i=0; i < n; i++){
  422.  
  423.         for(j=0; j < n; j++){
  424.  
  425.             a[i][j] = ainv[i][j];
  426.        
  427.         }
  428.     }
  429.  
  430.     delete L, D, X, ainv, z;     
  431.  
  432. }
  433.  
  434. template<typename T>
  435.  
  436. T Matriz<T>::deterMat() {
  437.  
  438. // Algoritmo para la eliminación simple de Gauss
  439.  
  440.     int i, j, k, n=rows, m=cols;
  441.  
  442.     assert (rows == cols);
  443.  
  444.     double **b;
  445.  
  446.     b = new double* [n];
  447.  
  448.     for(j=0; j<n; j++){
  449.  
  450.         b[j] = new double [n];
  451.    
  452.     }  
  453.  
  454.     for(i=0; i<n; i++){
  455.  
  456.         for(j=0; j<n; j++){
  457.  
  458.             b[i][j] = a[i][j];
  459.            
  460.         }
  461.  
  462.     }
  463.  
  464.     double factor;
  465.  
  466.     for (k = 0; k < n - 1; k++) {
  467.        
  468.         for (i = k+1; i < n;  i++) {
  469.  
  470.             factor = b[i][k]/b[k][k];
  471.  
  472.             for (j = k+1; j < n + 1; j++) {
  473.  
  474.                 b[i][j] = b[i][j] - factor * b[k][j];
  475.  
  476.             }
  477.        
  478.  
  479.  
  480.         }
  481.  
  482.     }
  483.  
  484. // Cálculo del determinante
  485.  
  486.     double determ = 1.;
  487.  
  488.     for (i = 0; i < n; i++) {
  489.  
  490.         determ = determ * b[i][i];
  491.  
  492.     }
  493.  
  494.  
  495.     delete [] b;
  496.  
  497.     return determ;
  498.  
  499. }
  500.  
  501. template<typename T>
  502.  
  503. void Matriz<T>::autovecMat(T **m) {
  504.  
  505.    // Define las variables de tipo entero
  506.  
  507.    int i, j, ip, iq, nrot, n;
  508.  
  509.    n = rows;
  510.  
  511.    double **v, **f;
  512.  
  513.    v = new double *[n]; f = new double *[n];
  514.  
  515.    for(i = 0; i< rows; i++){
  516.  
  517.         v[i] = new double [n];
  518.         f[i] = new double [n];
  519.  
  520.     }
  521.  
  522.     for(i=0; i < n; i++){
  523.  
  524.         for(j=0; j < n; j++){
  525.  
  526.             f[i][j] = m[i][j];
  527.        
  528.         }
  529.    
  530.     }
  531.  
  532. // Define las variables de tipo doble
  533.  
  534.    double *b, *z, *d;
  535.  
  536.    b = new double [n]; z = new double [n]; d = new double [n];
  537.  
  538.    double c, g, h, s, sm, t, tau, theta, tresh;
  539.  
  540. // Inicializa a la matriz identidad
  541.  
  542.    for (ip = 0; ip < n; ip++) {
  543.  
  544.       for (iq = 0; iq < n; iq++) {
  545.  
  546.          v[ip][iq] = 0;
  547.  
  548.       }
  549.  
  550.       v[ip][ip] = 1;
  551.  
  552.    }
  553.  
  554. // Inicializa b y d a la diagonal de a
  555.  
  556.    for (ip = 0; ip < n; ip++) {  
  557.  
  558.       b[ip] = f[ip][ip];
  559.  
  560.       d[ip] = b[ip];
  561.  
  562.       z[ip] = 0;
  563.  
  564.    }
  565.  
  566.    nrot = 0;
  567.  
  568.    for (i = 0; i < 50; i++) {
  569.  
  570.       sm = 0;
  571.  
  572.       for (ip = 0; ip < n - 1; ip++) {
  573.  
  574.          for (iq = ip + 1; iq < n; iq++) {
  575.  
  576.             sm +=fabs(f[ip][iq]);
  577.  
  578.          }
  579.  
  580.       }
  581.  
  582.       if (sm == 0) break;
  583.  
  584.       if (i < 4)
  585.  
  586.          tresh = 0.2*sm/(n*n);
  587.  
  588.       else
  589.  
  590.          tresh = 0.0;
  591.  
  592.       for (ip =0; ip < n -1; ip++) {
  593.  
  594.          for (iq = ip + 1; iq < n; iq++) {
  595.  
  596.             g = 100.0*fabs(f[ip][iq]);
  597.  
  598.             if(i>4 && (double)(fabs(d[ip])+g) == (double)fabs(d[ip])
  599.  
  600.                && (double)(fabs(d[iq])+g) == (double)fabs(d[iq]))
  601.  
  602.                f[ip][iq] = 0.0;
  603.  
  604.             else if (fabs(f[ip][iq]) > tresh) {
  605.  
  606.                h = d[iq] - d[ip];
  607.  
  608.                if ((double)(fabs(h)+g) == (double)fabs(h))
  609.  
  610.                   t = (f[ip][iq])/h;   // t = 1/(2theta)
  611.  
  612.                else {
  613.  
  614.                   theta = 0.5*h/(f[ip][iq]);
  615.  
  616.                   t = 1.0/(fabs(theta)+sqrt(1.0+theta*theta));
  617.  
  618.                   if(theta < 0.0) t = -t;
  619.  
  620.                }
  621.  
  622.                   c = 1.0/sqrt(1+t*t);
  623.  
  624.                   s = t*c;
  625.  
  626.                   tau = s/(1.0+c);
  627.  
  628.                   h = t*f[ip][iq];
  629.  
  630.                   z[ip] -=h;
  631.  
  632.                   z[iq] +=h;
  633.  
  634.                   d[ip] -=h;
  635.  
  636.                   d[iq] +=h;
  637.  
  638.                   f[ip][iq] = 0.0;
  639.  
  640. // Varía desde 0 hasta  ip - 1
  641.  
  642.                for (j =0; j < ip; j++) {
  643.  
  644.                   g = f[j][ip];
  645.  
  646.                   h = f[j][iq];
  647.  
  648.                   f[j][ip] = g - s*(h+g*tau);
  649.  
  650.                   f[j][iq] = h + s*(g-h*tau);
  651.  
  652.                }
  653.  
  654. // Varía desde ip+1 hasta  iq - 1
  655.  
  656.                for (j =ip+1; j < iq; j++) {
  657.  
  658.                   g = f[ip][j];
  659.  
  660.                   h = f[j][iq];
  661.  
  662.                   f[ip][j] = g - s*(h+g*tau);
  663.  
  664.                   f[j][iq] = h + s*(g-h*tau);
  665.  
  666.                }
  667.  
  668.                for (j =iq+1; j < n; j++) {
  669.  
  670.                   g = f[ip][j];
  671.  
  672.                   h = f[iq][j];
  673.  
  674.                   f[ip][j] = g - s*(h+g*tau);
  675.  
  676.                   f[iq][j] = h + s*(g-h*tau);
  677.  
  678.                }
  679.  
  680.                for (j =0; j < n; j++) {
  681.  
  682.                   g = v[j][ip];
  683.  
  684.                   h = v[j][iq];
  685.  
  686.                   v[j][ip] = g - s*(h+g*tau);
  687.  
  688.                   v[j][iq] = h + s*(g-h*tau);
  689.  
  690.                   }
  691.  
  692.                ++(nrot);
  693.  
  694.             }
  695.  
  696.          }
  697.  
  698.       }
  699.  
  700.          for (ip = 0; ip < n; ip++) {
  701.  
  702.             b[ip] = b[ip]+z[ip];
  703.  
  704.             d[ip] = b[ip];
  705.  
  706.             z[ip] = 0.0;
  707.  
  708.          }
  709.  
  710.     for(i=0; i < n; i++){
  711.  
  712.         for(j=0; j < n; j++){
  713.  
  714.             a[i][j] = v[i][j];
  715.        
  716.         }
  717.    
  718.     }
  719.  
  720.  
  721.    }  
  722.  
  723.    delete [] v, f, b, z, d;
  724.  
  725.  
  726. }
  727.  
  728. template<typename T>
  729.  
  730. void Matriz<T>::autovalMat(T **m) {
  731.  
  732.    // Define las variables de tipo entero
  733.  
  734.    int i, j, ip, iq, nrot, n;
  735.  
  736.    n = rows;
  737.  
  738.    double **v, **f;
  739.  
  740.    v = new double *[n]; f = new double *[n];
  741.  
  742.    for(i = 0; i< rows; i++){
  743.  
  744.         v[i] = new double [n];
  745.         f[i] = new double [n];
  746.  
  747.     }
  748.  
  749.     for(i=0; i < n; i++){
  750.  
  751.         for(j=0; j < n; j++){
  752.  
  753.             f[i][j] = m[i][j];
  754.        
  755.         }
  756.    
  757.     }
  758.  
  759. // Define las variables de tipo doble
  760.  
  761.    double *b, *z, *d;
  762.  
  763.    b = new double [n]; z = new double [n]; d = new double [n];
  764.  
  765.    double c, g, h, s, sm, t, tau, theta, tresh;
  766.  
  767. // Inicializa a la matriz identidad
  768.  
  769.    for (ip = 0; ip < n; ip++) {
  770.  
  771.       for (iq = 0; iq < n; iq++) {
  772.  
  773.          v[ip][iq] = 0;
  774.  
  775.       }
  776.  
  777.       v[ip][ip] = 1;
  778.  
  779.    }
  780.  
  781. // Inicializa b y d a la diagonal de a
  782.  
  783.    for (ip = 0; ip < n; ip++) {  
  784.  
  785.       b[ip] = f[ip][ip];
  786.  
  787.       d[ip] = b[ip];
  788.  
  789.       z[ip] = 0;
  790.  
  791.    }
  792.  
  793.    nrot = 0;
  794.  
  795.    for (i = 0; i < 50; i++) {
  796.  
  797.       sm = 0;
  798.  
  799.       for (ip = 0; ip < n - 1; ip++) {
  800.  
  801.          for (iq = ip + 1; iq < n; iq++) {
  802.  
  803.             sm +=fabs(f[ip][iq]);
  804.  
  805.          }
  806.  
  807.       }
  808.  
  809.       if (sm == 0) break;
  810.  
  811.       if (i < 4)
  812.  
  813.          tresh = 0.2*sm/(n*n);
  814.  
  815.       else
  816.  
  817.          tresh = 0.0;
  818.  
  819.       for (ip =0; ip < n -1; ip++) {
  820.  
  821.          for (iq = ip + 1; iq < n; iq++) {
  822.  
  823.             g = 100.0*fabs(f[ip][iq]);
  824.  
  825.             if(i>4 && (double)(fabs(d[ip])+g) == (double)fabs(d[ip])
  826.  
  827.                && (double)(fabs(d[iq])+g) == (double)fabs(d[iq]))
  828.  
  829.                f[ip][iq] = 0.0;
  830.  
  831.             else if (fabs(f[ip][iq]) > tresh) {
  832.  
  833.                h = d[iq] - d[ip];
  834.  
  835.                if ((double)(fabs(h)+g) == (double)fabs(h))
  836.  
  837.                   t = (f[ip][iq])/h;   // t = 1/(2theta)
  838.  
  839.                else {
  840.  
  841.                   theta = 0.5*h/(f[ip][iq]);
  842.  
  843.                   t = 1.0/(fabs(theta)+sqrt(1.0+theta*theta));
  844.  
  845.                   if(theta < 0.0) t = -t;
  846.  
  847.                }
  848.  
  849.                   c = 1.0/sqrt(1+t*t);
  850.  
  851.                   s = t*c;
  852.  
  853.                   tau = s/(1.0+c);
  854.  
  855.                   h = t*f[ip][iq];
  856.  
  857.                   z[ip] -=h;
  858.  
  859.                   z[iq] +=h;
  860.  
  861.                   d[ip] -=h;
  862.  
  863.                   d[iq] +=h;
  864.  
  865.                   f[ip][iq] = 0.0;
  866.  
  867. // Varía desde 0 hasta  ip - 1
  868.  
  869.                for (j =0; j < ip; j++) {
  870.  
  871.                   g = f[j][ip];
  872.  
  873.                   h = f[j][iq];
  874.  
  875.                   f[j][ip] = g - s*(h+g*tau);
  876.  
  877.                   f[j][iq] = h + s*(g-h*tau);
  878.  
  879.                }
  880.  
  881. // Varía desde ip+1 hasta  iq - 1
  882.  
  883.                for (j =ip+1; j < iq; j++) {
  884.  
  885.                   g = f[ip][j];
  886.  
  887.                   h = f[j][iq];
  888.  
  889.                   f[ip][j] = g - s*(h+g*tau);
  890.  
  891.                   f[j][iq] = h + s*(g-h*tau);
  892.  
  893.                }
  894.  
  895.                for (j =iq+1; j < n; j++) {
  896.  
  897.                   g = f[ip][j];
  898.  
  899.                   h = f[iq][j];
  900.  
  901.                   f[ip][j] = g - s*(h+g*tau);
  902.  
  903.                   f[iq][j] = h + s*(g-h*tau);
  904.  
  905.                }
  906.  
  907.                for (j =0; j < n; j++) {
  908.  
  909.                   g = v[j][ip];
  910.  
  911.                   h = v[j][iq];
  912.  
  913.                   v[j][ip] = g - s*(h+g*tau);
  914.  
  915.                   v[j][iq] = h + s*(g-h*tau);
  916.  
  917.                   }
  918.  
  919.                ++(nrot);
  920.  
  921.             }
  922.  
  923.          }
  924.  
  925.       }
  926.  
  927.          for (ip = 0; ip < n; ip++) {
  928.  
  929.             b[ip] = b[ip]+z[ip];
  930.  
  931.             d[ip] = b[ip];
  932.  
  933.             z[ip] = 0.0;
  934.  
  935.          }
  936.  
  937.     for(i=0; i < n; i++){
  938.  
  939.         for(j=0; j < n; j++){
  940.        
  941.             if(i != j) a[i][j] = 0;
  942.  
  943.                 else a[i][i] = d[i];
  944.        
  945.         }
  946.    
  947.     }
  948.  
  949.  
  950.    }  
  951.  
  952.    delete [] v, f, b, z, d;
  953.  
  954.  
  955. }
  956.  
  957. template<typename T>
  958.  
  959. void Matriz<T>::transpMat(Matriz<T> x){
  960.  
  961. // Determina la transpuesta de una matriz
  962.  
  963.     int i,j;
  964.  
  965.     for(i = 0; i < x.rows; i++){
  966.  
  967.         for(j = 0; j < x.cols; j++){
  968.  
  969.             a[j][i] = x.a[i][j];
  970.  
  971.         }
  972.  
  973.     }
  974.  
  975.  
  976. }
  977.  
  978.  
  979.  
  980.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement