reg_trsm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include "nr.h"
  20. #define SIZE 100000
  21. #define ma 3
  22. typedef struct Coord
  23. {
  24. float x1;
  25. float x2;
  26. } coord, *pcoord;
  27. coord tabcoord[SIZE];
  28. float tabx[SIZE];
  29. float taby[SIZE];
  30. float sig[SIZE];
  31. float afunc[ma+1];
  32. int ia[ma+1];
  33. float a[ma+1];
  34. void funcs( float i, float afunc[ma+1], int ma2)
  35. {
  36. afunc[1]=1;
  37. afunc[2]=tabcoord[(int)i].x1;
  38. afunc[3]=tabcoord[(int)i].x1*tabcoord[(int)i].x1*tabcoord[(int)i].x2;
  39. //printf("%f %f %f \n",afunc[0],afunc[1],afunc[2]);
  40. }
  41. int main(int argc, char * argv[])
  42. {
  43. float total=0.0;
  44. float ecart=0.0;
  45. /* long double total=0.0; */
  46. /* long double ecart=0.0; */
  47. char *filename = argv[1];
  48. int k,i;
  49. FILE * res;
  50. int ndat=atoi(argv[2]);
  51. float ** covar;
  52. float *chisq = (float *)malloc(sizeof(float));
  53. res = fopen(filename,"r");
  54. covar = (float**) malloc((ma+1) *sizeof(float*));
  55. for (i=0;i<ma+1;i++)
  56. covar[i]=(float*)malloc((ma+1) *sizeof(float));
  57. for (k=1;k<ndat+1;k++)
  58. {
  59. int i, j;
  60. float tmpfloat;
  61. fscanf(res,"%f\t%d\t%d\n", &tmpfloat, &i, &j);
  62. tabcoord[k].x1=i-j;
  63. tabcoord[k].x2=j;
  64. taby[k]=tmpfloat;
  65. // printf("%d -> %f %d %d\n", k, tmpfloat, i-j, j);
  66. sig[k]=1;
  67. tabx[k]=k;
  68. }
  69. for (k=1;k<ma+1;k++)
  70. ia[k]=1;
  71. lfit(tabx, taby, sig, ndat, a, ia, ma, covar, chisq, &funcs);
  72. for (k=1;k<ma+1;k++)
  73. {
  74. // printf("%.12lf\n", a[k]);
  75. //total+=a[k];
  76. }
  77. //calcul de l'ecart type
  78. for (k=1;k<ndat+1;k++)
  79. {
  80. double abs=0.0;
  81. abs += a[1];
  82. abs += tabcoord[k].x1*a[2];
  83. abs += tabcoord[k].x1*tabcoord[k].x1*tabcoord[k].x2*a[3];
  84. // fprintf(stderr,"k=%i ; calcul : %lf ; reel : %lf ; ", k, abs, taby[k]);
  85. abs = abs - taby[k];
  86. if (abs < 0)
  87. abs = - abs;
  88. // fprintf(stderr,"ecart : %lf\n ", abs);
  89. total += abs;
  90. //printf("%f %f %f\n",tabcoord[k].x1 ,tabcoord[k].x2 ,tabcoord[k].x3);
  91. }
  92. fprintf(stdout,"#define TRSM_A %e\n#define TRSM_B %e\n#define TRSM_C %e\n", a[3], a[2], a[1]);
  93. fprintf(stderr,"#define PERF_TRSM(i,j) (TRSM_A*(double)(i)*(double)(i)*(double)(j)+TRSM_B*(double)(i)+TRSM_C)\n");
  94. fprintf(stderr, "total %lf\n", total);
  95. ecart = total / ndat;
  96. fprintf(stderr, "ecart moyen %lf\n", ecart);
  97. return 0;
  98. }