Ver Mensaje Individual
  #6 (permalink)  
Antiguo 05/06/2012, 14:14
mikelromero
 
Fecha de Ingreso: junio-2012
Mensajes: 7
Antigüedad: 12 años, 8 meses
Puntos: 0
Respuesta: Transposicion de una matriz MxN

He solucionado el problema anterior, era un problema al hacer el printf de la matriz, y ahora me calcula toda la matriz transpuesta, con las columnas y las filas bien. Pero el contenido de las celdas no es bueno. Empieza rellenando los valores de la fila1 con los valores de la columna1 de la matriz original, pero en el 4º o el 5º valor cambia de columna y empieza a coger los valores de la columna 2 de la matriz original... No se si me he explicado bien :s
Agradeceria mucho si alguien me pudiera ayudar, ya que es para un proyecto de clase. Este el codigo corregido:

Código c:
Ver original
  1. #include <stdio.h>
  2. #include <QtCore/QCoreApplication>
  3. #include <iostream>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7.  
  8. void affichage_matrice(double**matrice, int n);
  9. double* algo_solution (double **U, double**L, double*b, int n);
  10. double** cholesky (double **matrice, int taille);
  11. double* fcourbe(double *x,double *y,double *v,int longueur);
  12. double* gnewton(double *x,double *y, double *v, int longueur, int longueur1);
  13. double** transposee(double **chol,int col, int fil);
  14.  
  15.  
  16. int main(int argc, char *argv[]){
  17.     QCoreApplication a(argc, argv);
  18.     int longueur;
  19.     int longueur1;
  20.    
  21.     double *x;
  22.     double *y;
  23.     double *v;
  24.  
  25.  
  26.     //Allocation de memoire dynamique
  27.     x =(double*)malloc(sizeof(double)*longueur);
  28.     y =(double*)malloc(sizeof(double)*longueur);
  29.     v =(double*)malloc(sizeof(double)*longueur1);
  30.  
  31.    longueur=10;
  32.     longueur1=4;
  33.  
  34.     x[0]=0.1;
  35.     x[1]=0.3;
  36.     x[2]=0.7;
  37.     x[3]=1.2;
  38.     x[4]=1.6;
  39.     x[5]=2.2;
  40.     x[6]=2.7;
  41.     x[7]=3.1;
  42.     x[8]=3.5;
  43.     x[9]=3.9;
  44.  
  45.     y[0]=0.558;
  46.     y[1]=0.569;
  47.     y[2]=0.176;
  48.     y[3]=-0.207;
  49.     y[4]=-0.133;
  50.     y[5]=0.132;
  51.     y[6]=0.055;
  52.     y[7]=-0.09;
  53.     y[8]=-0.069;
  54.     y[9]=0.027;
  55.  
  56.     v[0]=1;
  57.     v[1]=2;
  58.     v[2]=2;
  59.     v[3]=1;
  60.  
  61.  
  62.  
  63.  
  64.     double *model;
  65.     model=gnewton(x,y,v,longueur,longueur1);
  66.  
  67.     int z;
  68.     printf("Le vecteur v optimise est:\n");
  69.     for(z=0;z<longueur1;z++){
  70.         printf("%lf " ,model[z]);
  71.         printf("\n");
  72.     }
  73.  
  74.  
  75.     return (int)a.exec;
  76.  
  77.  
  78.  
  79. }
  80.  
  81. double** cholesky (double **matrice, int taille){
  82.  
  83.     int i, j, m;
  84.     double b, a, b1, a1;
  85.     double **chol;
  86.  
  87.     chol =(double**)malloc(sizeof(double*)*taille);
  88.  
  89.     for(i=0;i<taille;i++){
  90.  
  91.         chol[i]=(double*)calloc(taille,sizeof(double));
  92.  
  93.  
  94. }
  95.     int k;
  96.  
  97.     double *matest;
  98.     double *Test;
  99.     double Tf;
  100.     int cont=1;
  101.     int cont1=1;
  102.     matest =(double*)malloc(sizeof(double)*taille);
  103.     Test =(double*)malloc(sizeof(double)*taille);
  104.  
  105. //Initialisation des tableaux
  106.     int z=0;
  107.     for(z=0;z<taille;z++){
  108.         matest[z]=1;
  109. }
  110.  
  111.  
  112. //On teste si la matrice entrée est symétrique définie positive
  113.  
  114.     for (i=0;i<taille;i++){
  115.         for(j=0;j<taille;j++)
  116.     {
  117.         if (abs(matrice[i][j]-matrice[j][i])>0.01 )
  118.         cont=0;
  119.  
  120.     }
  121.  
  122. }
  123.     if (!cont)
  124.          printf ("\n La matrice n'est pas symetrique!");
  125.  
  126.     for(int i=0;i<taille;i++){
  127.         Test[i]=0;
  128.         for(int j=0;j<taille;j++){
  129.  
  130.             Test[i]=Test[i]+(matest[i]*matrice[i][j]);
  131.             Tf=Tf+(Test[i]*matest[i]);
  132.             if(Tf<0)
  133.                 cont1=0;
  134.         }
  135.  
  136.     }
  137.  
  138.  
  139.     if (!cont1)
  140.      printf ("\n La matrice n'est pas definie positive!\n");
  141.  
  142.   if(cont && cont1){
  143.  
  144.     //On calcule les valeurs des cases de la matrice decomposée L
  145.     for(i=0;i<taille;i++){
  146.         b=0;
  147.         for(j=0;j<=i-1;j++){
  148.             b1=chol[i][j]*chol[i][j];
  149.             b=b+b1;
  150.     }
  151.         chol[i][i]=sqrt(matrice[i][i]-b);
  152.  
  153.  
  154.     for(k=i+1;k<taille;k++){
  155.             a=0;
  156.         for(m=0;m<=k-1;m++){
  157.                 a1=chol[i][m]*chol[k][m];
  158.                 a=a+a1;
  159.             }
  160.             chol[k][i]=(matrice[k][i]-a)/chol[i][i];
  161.         }
  162.     }
  163. }
  164.     return chol;
  165. }
  166.  
  167. //ALGORITMO DE CALCULO DE LA TRANSPUESTA
  168. double** transposee(double **chol,int col, int fil){
  169.  
  170.     double **cholin;
  171.     cholin = (double**)malloc(fil * sizeof(*cholin));
  172.     for(int m = 0; m < fil; m++)
  173.     {
  174.         cholin[m] = (double*)malloc(col * sizeof(**cholin));
  175.     }
  176.  
  177.     /* Initialisation */
  178.  
  179.     for (int i=0; i < col; i++){
  180.         for (int j=i; j < fil; j++){
  181.  
  182.  
  183.                    cholin[i][j] = chol[j][i];
  184.  
  185.                 }
  186.         }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.     return cholin;
  193.  
  194. }
  195.  
  196.  
  197. void affichage_matrice(double**matrice, int n)
  198. {
  199.     for (int i=0;i<n;i++)
  200.     {
  201.         for (int j=0;j<n;j++)
  202.         {
  203.             cout<<matrice[i][j]<<"   ";
  204.         }
  205.         cout<<endl;
  206.     }
  207.     cout<<endl;
  208. }
  209.  
  210.  
  211.  
  212.  
  213. double* algo_remontee (double**U, double*b, int n)
  214. {
  215.     double*x;
  216.     x=new double[n];
  217.     double somme=0;
  218.  
  219.     x[n-1]=b[n-1]/U[n-1][n-1];
  220.  
  221.     for(int i=n-2;i>=0;i--)
  222.     {
  223.         somme=0;
  224.         for(int j=i+1;j<n;j++)
  225.         {
  226.             somme+=U[i][j]*x[j];
  227.         }
  228.  
  229.         x[i]=(b[i]-somme)/U[i][i];
  230.     }
  231.     return x;
  232. }
  233.  
  234.  
  235.  
  236.  
  237. double* algo_descente (double**L, double*b, int n)
  238. {
  239.     double*x;
  240.     x=new double[n];
  241.     double somme=0;
  242.  
  243.     x[0]=b[0]/L[0][0];
  244.  
  245.     for(int i=1;i<n;i++)
  246.     {
  247.         somme=0;
  248.         for(int j=0;j<i;j++)
  249.         {
  250.             somme+=L[i][j]*x[j];
  251.         }
  252.  
  253.         x[i]=(b[i]-somme)/L[i][i];
  254.     }
  255.     return x;
  256. }
  257.  
  258.  
  259.  
  260.  
  261. double* algo_solution(double **U, double**L, double*b, int n)
  262. {
  263.  
  264.     // on a A, on décompose en LLt, L=L et Lt=U
  265.    double*y=new double[n];
  266.  
  267.     y=algo_descente(L,b,n);
  268.     return algo_remontee(U,y,n);
  269. }
  270.  
  271.  
  272.  
  273. double* fcourbe(double *x,double *y,double *v,int longueur){
  274.     double *Fc=new double[longueur];
  275.     for(int i=0; i<longueur;i++){
  276.  
  277.         Fc[i]=y[i]-(v[0]*exp(-v[1]*x[i])*sin((v[2]*x[i])+v[3]));
  278.  
  279.  
  280.     }
  281.        return Fc;
  282. }
  283.  
  284.  
  285. double** jacobien_courbe(double *x,double *v,int longueur, int longueur1){
  286.  
  287.     double **J;
  288.  
  289.  
  290.         J = (double**)malloc(longueur * sizeof(*J));
  291.         for(int m = 0; m < longueur; m++)
  292.         {
  293.             J[m] = (double*)malloc(longueur1 * sizeof(**J));
  294.         }
  295.  
  296.     for(int i=0; i<longueur; i++){
  297.         J[i][0]= -exp(-v[1]*x[i])*sin((v[2]*x[i])+v[3]);
  298.         J[i][1]= x[i]*v[0]*exp(-v[1]*x[i])*sin((v[2]*x[i])+v[3]);
  299.         J[i][2]= -x[i]*v[0]*exp(-v[1]*x[i])*cos((v[2]*x[i])+v[3]);
  300.         J[i][3]= -v[0]*exp(-v[1]*x[i])*cos((v[2]*x[i])+v[3]);
  301.     }
  302.     return J;
  303.     int e; int d;
  304.     printf("La matrice J est:");
  305.  
  306.     for(e=0;e<longueur;e++){
  307.         for(d=0;d<longueur1;d++){
  308.             printf("%lf " ,J [e][d]);
  309.  
  310.         }
  311.         printf("\n");
  312.     }
  313.  
  314. }
  315.  
  316.  
  317. double* gnewton(double *x,double *y, double *v, int longueur, int longueur1){
  318.     double *F;
  319.     double **J;
  320.     double **A;
  321.  
  322.     A = (double**)malloc(longueur1 * sizeof(*A));
  323.     for(int m = 0; m < longueur1; m++){
  324.         A[m] = (double*)malloc(longueur1 * sizeof(**A));
  325.     }
  326.     double *B=new double[longueur1];
  327.     double *C=new double[longueur1];
  328.     double **L;
  329.     double **Lt;
  330.     double *S;
  331.     double **Jt;
  332.     int p;
  333.  
  334.  
  335.  
  336.  
  337.  
  338.         do{
  339.  
  340.  
  341.         F=fcourbe(x,y,v,longueur);
  342.  
  343.         J=jacobien_courbe(x,v,longueur,longueur1);
  344.          int e; int d;
  345.          printf("La matrice J est:\n");
  346.  
  347.         for(e=0;e<longueur;e++){
  348.             for(d=0;d<longueur1;d++){
  349.                 printf("%lf " ,J [e][d]);
  350.  
  351.             }
  352.             printf("\n");
  353.         }
  354.         Jt=transposee(J,longueur1,longueur);
  355.  
  356.         printf("La matrice Jt est:\n");
  357.  
  358.         for(e=0;e<longueur1;e++){
  359.             for(d=0;d<longueur;d++){
  360.                 printf("%lf " ,Jt [e][d]);
  361.  
  362.             }
  363.             printf("\n");
  364.         }
  365.  
  366.         for(int i=0;i<longueur1;i++){
  367.             for(int j=0;j<longueur1;j++){
  368.                 A[i][j]=0;
  369.                 for(p=0;p<longueur;p++){
  370.  
  371.                     A[i][j]=A[i][j]+(Jt[i][p]*J[p][j]);
  372.  
  373.                 }
  374.             }
  375.  
  376.         }
  377.  
  378.         for(int i=0;i<longueur1;i++){
  379.             B[i]=0;
  380.             for(int j=0;j<longueur;j++){
  381.                 B[i]=B[i]+(Jt[i][j]*(F[j]));
  382.  
  383.             }
  384.             C[i]=-B[i];
  385.         }
  386.  
  387.         printf("La matrice symetrique J*Jt est:");printf("\n");
  388.  
  389.  
  390.         for(e=0;e<longueur1;e++){
  391.             for(d=0;d<longueur1;d++){
  392.                 printf("%lf " ,A [e][d]);
  393.  
  394.             }
  395.             printf("\n");
  396.         }
  397.  
  398.  
  399.             L=cholesky(A,longueur1);
  400.  
  401.             Lt=transposee(L,longueur1,longueur1);
  402.  
  403.  
  404.             S=algo_solution(L,Lt,C,longueur1);
  405.  
  406.  
  407.             printf("S est:");printf("\n");
  408.             for(e=0;e<longueur1;e++){
  409.  
  410.                     printf("%lf " ,S [e]);
  411.             }
  412.             printf("\n");
  413.  
  414.             for(int k=0; k<longueur1;k++){
  415.  
  416.  
  417.                 v[k]=v[k]+S[k];
  418.             }
  419.             printf("v est:");
  420.             printf("\n");
  421.  
  422.  
  423.             for(d=0;d<longueur1;d++){
  424.  
  425.                     printf("%lf " ,v [d]);
  426.             }
  427.             printf("\n");
  428.  
  429.  
  430.     }while(sqrt(B[0]*B[0]+B[1]*B[1]+B[2]*B[2]+B[3]*B[3])>(pow(10,-6)));
  431.  
  432.  
  433.  
  434.  
  435.     return v;
  436. }

Última edición por mikelromero; 05/06/2012 a las 16:24