| [658] | 1 | #include "machdefs.h" | 
|---|
|  | 2 |  | 
|---|
|  | 3 | #include <stdlib.h> | 
|---|
|  | 4 | #include <stdio.h> | 
|---|
|  | 5 | #include <math.h> | 
|---|
|  | 6 | #ifdef __MWERKS__ | 
|---|
|  | 7 | #include <iostream> | 
|---|
|  | 8 | #endif | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include "nbtri.h" | 
|---|
|  | 11 | #include "cspline.h" | 
|---|
|  | 12 |  | 
|---|
|  | 13 | //++ | 
|---|
|  | 14 | // Class        CSpline | 
|---|
|  | 15 | // Lib  Outils++ | 
|---|
|  | 16 | // include      cspline.h | 
|---|
|  | 17 | // | 
|---|
|  | 18 | //      Classe de spline 1D | 
|---|
|  | 19 | //-- | 
|---|
|  | 20 |  | 
|---|
|  | 21 | //++ | 
|---|
|  | 22 | // Titre        Constructeurs | 
|---|
|  | 23 | //-- | 
|---|
|  | 24 |  | 
|---|
|  | 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 26 | //++ | 
|---|
|  | 27 | CSpline::CSpline(int n,double* x,double* y,double yp1,double ypn | 
|---|
|  | 28 | ,int natural,bool order) | 
|---|
|  | 29 | // | 
|---|
|  | 30 | //      Createur pour spline 3 sur "x[0->n],y[0->n]" avec "yp1,ypn" derivees | 
|---|
|  | 31 | //      au premier et dernier points et "natural" indiquant les types de | 
|---|
|  | 32 | //      contraintes sur les derivees 2sd au premier et dernier point. | 
|---|
|  | 33 | //      "order" doit etre mis a "true" si le tableau de "x[]" n'est pas ordonne | 
|---|
|  | 34 | //      dans l'ordre des "x" croissants ("x[i]<x[i+1]"): cette option | 
|---|
|  | 35 | //      realloue la place pour les tableaux "x,y" autrement seule une | 
|---|
|  | 36 | //      connection aux tableaux "x,y" externes est realisee. | 
|---|
|  | 37 | //-- | 
|---|
|  | 38 | : Nel(0), corrupt_Y2(true), XY_Created(false), Natural(natural) | 
|---|
|  | 39 | , YP1(yp1), YPn(ypn), X(NULL), Y(NULL), Y2(NULL), tmp(NULL) | 
|---|
|  | 40 | { | 
|---|
|  | 41 | SetNewTab(n,x,y,order,true); | 
|---|
|  | 42 | if( x != NULL && y != NULL) ComputeCSpline(); | 
|---|
|  | 43 |  | 
|---|
|  | 44 | END_CONSTRUCTOR | 
|---|
|  | 45 | } | 
|---|
|  | 46 |  | 
|---|
|  | 47 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 48 | //++ | 
|---|
|  | 49 | CSpline::CSpline(double yp1,double ypn,int natural) | 
|---|
|  | 50 | // | 
|---|
|  | 51 | //      Createur par defaut. | 
|---|
|  | 52 | //-- | 
|---|
|  | 53 | : Nel(0), corrupt_Y2(true), XY_Created(false), Natural(natural) | 
|---|
|  | 54 | , YP1(yp1), YPn(ypn), X(NULL), Y(NULL), Y2(NULL), tmp(NULL) | 
|---|
|  | 55 | { | 
|---|
|  | 56 | END_CONSTRUCTOR | 
|---|
|  | 57 | } | 
|---|
|  | 58 |  | 
|---|
|  | 59 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 60 | CSpline::~CSpline() | 
|---|
|  | 61 | // destructeur | 
|---|
|  | 62 | { | 
|---|
|  | 63 | DelTab(); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | //++ | 
|---|
|  | 67 | // Titre        Methodes | 
|---|
|  | 68 | //-- | 
|---|
|  | 69 |  | 
|---|
|  | 70 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 71 | //++ | 
|---|
|  | 72 | void CSpline::SetNewTab(int n,double* x,double* y,bool order,bool force) | 
|---|
|  | 73 | // | 
|---|
|  | 74 | //      Pour changer les tableaux sans recreer la classe, | 
|---|
|  | 75 | //      memes arguments que dans le createur. | 
|---|
|  | 76 | //      Pour connecter les tableaux "x[n],y[n]" aux pointeurs internes "X,Y" | 
|---|
|  | 77 | //      Si "order=true", on considere que x n'est pas range par ordre | 
|---|
|  | 78 | //      des "x" croissants. La methode alloue de la place pour des tableaux | 
|---|
|  | 79 | //      internes "X,Y" qu'elle re-ordonne par "x" croissant. | 
|---|
|  | 80 | //      "force=true" impose la reallocation des divers buffers, sinon | 
|---|
|  | 81 | //      la reallocation n'a lieu que si le nombre de points augmente. | 
|---|
|  | 82 | //-- | 
|---|
|  | 83 | { | 
|---|
|  | 84 | ASSERT( n>3 ); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | // allocation des buffers Y2 et tmp | 
|---|
|  | 87 | if( n>Nel || force ) { | 
|---|
|  | 88 | if( Y2  != NULL ) delete [] Y2; | 
|---|
|  | 89 | if( tmp != NULL ) delete [] tmp; | 
|---|
|  | 90 | Y2   = new double[n]; | 
|---|
|  | 91 | tmp  = new double[n]; | 
|---|
|  | 92 | } | 
|---|
|  | 93 | // des-allocation eventuelle de X,Y | 
|---|
|  | 94 | if( XY_Created ) { | 
|---|
|  | 95 | if( !order || n>Nel || force ) { | 
|---|
|  | 96 | if( X != NULL ) delete [] X;  X = NULL; | 
|---|
|  | 97 | if( Y != NULL ) delete [] Y;  Y = NULL; | 
|---|
|  | 98 | XY_Created = false; | 
|---|
|  | 99 | } | 
|---|
|  | 100 | } | 
|---|
|  | 101 | // allocation eventuelle de X,Y | 
|---|
|  | 102 | if( order ) { | 
|---|
|  | 103 | if( !XY_Created || n>Nel || force ) { | 
|---|
|  | 104 | X = new double[n]; | 
|---|
|  | 105 | Y = new double[n]; | 
|---|
|  | 106 | XY_Created = true; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | } | 
|---|
|  | 109 | Nel = n; | 
|---|
|  | 110 | corrupt_Y2 = true; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | if( x==NULL || y==NULL ) return; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | // Classement eventuel par ordre des x croissants | 
|---|
|  | 115 | if( order ) { | 
|---|
|  | 116 | if( tmp == NULL ) tmp = new double[n]; | 
|---|
|  | 117 | ind = (int_4 *) tmp; | 
|---|
|  | 118 | tri_double(x,ind,(int_4) Nel); | 
|---|
|  | 119 | for(int i=0;i<Nel;i++) { | 
|---|
|  | 120 | X[i] = x[ind[i]]; | 
|---|
|  | 121 | Y[i] = y[ind[i]]; | 
|---|
|  | 122 | if( i>0 ) if( X[i-1]>= X[i] ) { | 
|---|
|  | 123 | printf("CSpline::SetNewTab_Erreur: X[%d]>=X[%d] (%g>=%g)\n" | 
|---|
|  | 124 | ,i-1,i,X[i-1],X[i]); | 
|---|
|  | 125 | THROW(inconsistentErr); | 
|---|
|  | 126 | } | 
|---|
|  | 127 | } | 
|---|
|  | 128 | } else { X = x; Y = y; } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 133 | void CSpline::DelTab() | 
|---|
|  | 134 | // destruction des divers tableaux en tenant compte des allocations/connections | 
|---|
|  | 135 | { | 
|---|
|  | 136 | if( X   != NULL && XY_Created ) delete [] X;    X   = NULL; | 
|---|
|  | 137 | if( Y   != NULL && XY_Created ) delete [] Y;    Y   = NULL; | 
|---|
|  | 138 | if( Y2  != NULL ) delete [] Y2;   Y2  = NULL; | 
|---|
|  | 139 | if( tmp != NULL ) delete [] tmp;  tmp = NULL; | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
|  | 142 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 143 | //++ | 
|---|
|  | 144 | void CSpline::SetBound1er(double yp1,double ypn) | 
|---|
|  | 145 | // | 
|---|
|  | 146 | //      Pour changer les valeurs des derivees 1ere au 1er et dernier points | 
|---|
|  | 147 | //      Valeurs imposees des derivees 1ere au points "X[0]" et "X[Nel-1]". | 
|---|
|  | 148 | //-- | 
|---|
|  | 149 | { | 
|---|
|  | 150 | if( yp1 == YP1 && ypn == YPn ) return; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | YP1 = yp1; | 
|---|
|  | 153 | YPn = ypn; | 
|---|
|  | 154 |  | 
|---|
|  | 155 | corrupt_Y2 = true; | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 159 | //++ | 
|---|
|  | 160 | void CSpline::ComputeCSpline() | 
|---|
|  | 161 | // | 
|---|
|  | 162 | //      Pour calculer les tableaux de coeff permettant le calcul | 
|---|
|  | 163 | //      des interpolations spline. | 
|---|
|  | 164 | //-- | 
|---|
|  | 165 | { | 
|---|
|  | 166 | // on ne fait rien si les tableaux ne sont pas connectes | 
|---|
|  | 167 | if( X == NULL || Y == NULL ) { | 
|---|
|  | 168 | printf("CSpline::ComputeCSpline()_Erreur: tableaux non connectes X=%p Y=%p\n" | 
|---|
|  | 169 | ,X,Y); | 
|---|
|  | 170 | return; | 
|---|
|  | 171 | } | 
|---|
|  | 172 | // On ne fait rien si rien n'a change! | 
|---|
|  | 173 | if( ! corrupt_Y2 ) return; | 
|---|
|  | 174 | // protection si tmp a ete desalloue pour gain de place (ex: CSpline2) | 
|---|
|  | 175 | if( tmp == NULL ) tmp = new double[Nel]; | 
|---|
|  | 176 |  | 
|---|
|  | 177 | double p,qn,sig,un; | 
|---|
|  | 178 |  | 
|---|
|  | 179 | if (Natural & Natural1) | 
|---|
|  | 180 | Y2[0] = tmp[0] = 0.0; | 
|---|
|  | 181 | else { | 
|---|
|  | 182 | Y2[0] = -0.5; | 
|---|
|  | 183 | tmp[0] = (3.0/(X[1]-X[0]))*((Y[1]-Y[0])/(X[1]-X[0])-YP1); | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | for (int i=1;i<Nel-1;i++) { | 
|---|
|  | 187 | sig = (X[i]-X[i-1])/(X[i+1]-X[i-1]); | 
|---|
|  | 188 | p = sig * Y2[i-1] + 2.0; | 
|---|
|  | 189 | Y2[i] = (sig-1.0)/p; | 
|---|
|  | 190 | tmp[i]= (Y[i+1]-Y[i])/(X[i+1]-X[i]) - (Y[i]-Y[i-1])/(X[i]-X[i-1]); | 
|---|
|  | 191 | tmp[i]= (6.0*tmp[i]/(X[i+1]-X[i-1])-sig*tmp[i-1])/p; | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | if (Natural & NaturalN) | 
|---|
|  | 195 | qn = un = 0.0; | 
|---|
|  | 196 | else { | 
|---|
|  | 197 | qn = 0.5; | 
|---|
|  | 198 | un = (3.0/(X[Nel-1]-X[Nel-2])) | 
|---|
|  | 199 | *(YPn-(Y[Nel-1]-Y[Nel-2])/(X[Nel-1]-X[Nel-2])); | 
|---|
|  | 200 | } | 
|---|
|  | 201 | Y2[Nel-1] = (un-qn*tmp[Nel-2])/(qn*Y2[Nel-2]+1.0); | 
|---|
|  | 202 | for (int k=Nel-2;k>=0;k--) Y2[k] = Y2[k]*Y2[k+1] + tmp[k]; | 
|---|
|  | 203 |  | 
|---|
|  | 204 | corrupt_Y2 = false; | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 208 | //++ | 
|---|
|  | 209 | double CSpline::CSplineInt(double x) | 
|---|
|  | 210 | // | 
|---|
|  | 211 | //      Interpolation spline en "x" | 
|---|
|  | 212 | //-- | 
|---|
|  | 213 | { | 
|---|
|  | 214 | int klo,khi,k; | 
|---|
|  | 215 | double h,b,a,y = 0.; | 
|---|
|  | 216 |  | 
|---|
|  | 217 | if( corrupt_Y2 ) { | 
|---|
|  | 218 | cout<<"CSpline::CSplineInt: calcul des coef du spline corrupted"<<endl; | 
|---|
|  | 219 | THROW(inconsistentErr); | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
|  | 222 | klo = 0; | 
|---|
|  | 223 | khi = Nel-1; | 
|---|
|  | 224 | while (khi-klo > 1) { | 
|---|
|  | 225 | k = (khi+klo) >> 1; | 
|---|
|  | 226 | if (X[k] > x) khi=k; | 
|---|
|  | 227 | else klo=k; | 
|---|
|  | 228 | } | 
|---|
|  | 229 | h=X[khi]-X[klo]; | 
|---|
|  | 230 |  | 
|---|
|  | 231 | if (h == 0.0) { | 
|---|
|  | 232 | cout<<"CSpline::CSplineInt: pout khi="<<khi<<" klo="<<klo | 
|---|
|  | 233 | <<" memes valeurs de X[]: "<<X[khi]<<endl; | 
|---|
|  | 234 | THROW(inconsistentErr); | 
|---|
|  | 235 | } | 
|---|
|  | 236 |  | 
|---|
|  | 237 | a = (X[khi]-x)/h; | 
|---|
|  | 238 | b = (x-X[klo])/h; | 
|---|
|  | 239 | y = a*Y[klo]+b*Y[khi]+((a*a*a-a)*Y2[klo]+(b*b*b-b)*Y2[khi])*(h*h)/6.0; | 
|---|
|  | 240 |  | 
|---|
|  | 241 | return y; | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
|  | 244 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 245 | ///////// rappel des inlines pour commentaires //////////////// | 
|---|
|  | 246 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 247 |  | 
|---|
|  | 248 | //++ | 
|---|
|  | 249 | // inline void SetNaturalCSpline(int type = NaturalAll) | 
|---|
|  | 250 | //      Pour changer le type de contraintes sur les derivees 2sd | 
|---|
|  | 251 | //-- | 
|---|
|  | 252 | //++ | 
|---|
|  | 253 | // inline void Free_Tmp() | 
|---|
|  | 254 | //      Pour liberer la place tampon qui ne sert que | 
|---|
|  | 255 | //      dans ComputeCSpline() et pas dans CSplineInt | 
|---|
|  | 256 | //-- | 
|---|
|  | 257 |  | 
|---|
|  | 258 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 259 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 260 |  | 
|---|
|  | 261 | //++ | 
|---|
|  | 262 | // Class        CSpline2 | 
|---|
|  | 263 | // Lib  Outils++ | 
|---|
|  | 264 | // include      cspline.h | 
|---|
|  | 265 | // | 
|---|
|  | 266 | //      Classe de spline 2D | 
|---|
|  | 267 | //-- | 
|---|
|  | 268 |  | 
|---|
|  | 269 | //++ | 
|---|
|  | 270 | // Titre        Constructeurs | 
|---|
|  | 271 | //-- | 
|---|
|  | 272 |  | 
|---|
|  | 273 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 274 | //++ | 
|---|
|  | 275 | CSpline2::CSpline2(int n1,double* x1,int n2,double* x2,double* y | 
|---|
|  | 276 | ,int natural,bool order) | 
|---|
|  | 277 | // | 
|---|
|  | 278 | //      Meme commentaire que pour CSpline avec: | 
|---|
|  | 279 | //| x1[n1]: liste des coordonnees selon l axe 1 | 
|---|
|  | 280 | //| x2[n2]: liste des coordonnees selon l axe 2 | 
|---|
|  | 281 | //| y[n1*n2]: liste des valeurs avec le rangement suivant | 
|---|
|  | 282 | //| x1[0]......x1[n1-1]  x1[0]......x1[n1-1]  ... x1[0]......x1[n1-1] | 
|---|
|  | 283 | //| |    0<=i<n1      |  |    0<=i<n1      |  ... |    0<=i<n1      | | 
|---|
|  | 284 | //|      j=0 X2[0]            j=1 X2[1]              j=n2-1 X2[n2-1] | 
|---|
|  | 285 | //-- | 
|---|
|  | 286 | : Nel1(0), Nel2(0), corrupt_Y2(true), XY_Created(false), Natural(natural) | 
|---|
|  | 287 | , X1(NULL), X2(NULL), Y(NULL), Y2(NULL) | 
|---|
|  | 288 | , Nel_S(0), S(NULL), Sint(NULL), tmp(NULL) | 
|---|
|  | 289 | { | 
|---|
|  | 290 | SetNewTab(n1,x1,n2,x2,y,order,true); | 
|---|
|  | 291 | if( x1 != NULL && x2 != NULL && y != NULL) ComputeCSpline(); | 
|---|
|  | 292 |  | 
|---|
|  | 293 | END_CONSTRUCTOR | 
|---|
|  | 294 | } | 
|---|
|  | 295 |  | 
|---|
|  | 296 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 297 | //++ | 
|---|
|  | 298 | CSpline2::CSpline2(int natural) | 
|---|
|  | 299 | // | 
|---|
|  | 300 | //      Createur par defaut. | 
|---|
|  | 301 | //-- | 
|---|
|  | 302 | : Nel1(0), Nel2(0), corrupt_Y2(true), XY_Created(false), Natural(natural) | 
|---|
|  | 303 | , X1(NULL), X2(NULL), Y(NULL), Y2(NULL) | 
|---|
|  | 304 | , Nel_S(0), S(NULL), Sint(NULL), tmp(NULL) | 
|---|
|  | 305 | { | 
|---|
|  | 306 | END_CONSTRUCTOR | 
|---|
|  | 307 | } | 
|---|
|  | 308 |  | 
|---|
|  | 309 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 310 | CSpline2::~CSpline2() | 
|---|
|  | 311 | { | 
|---|
|  | 312 | DelTab(); | 
|---|
|  | 313 | } | 
|---|
|  | 314 |  | 
|---|
|  | 315 | //++ | 
|---|
|  | 316 | // Titre        Methodes | 
|---|
|  | 317 | //-- | 
|---|
|  | 318 |  | 
|---|
|  | 319 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 320 | //++ | 
|---|
|  | 321 | void CSpline2::SetNewTab(int n1,double* x1,int n2,double* x2,double* y | 
|---|
|  | 322 | ,bool order,bool force) | 
|---|
|  | 323 | // | 
|---|
|  | 324 | //      Voir commentaire meme methode de CSpline | 
|---|
|  | 325 | //-- | 
|---|
|  | 326 | { | 
|---|
|  | 327 | ASSERT( n1>3 && n2>3 ); | 
|---|
|  | 328 |  | 
|---|
|  | 329 | int n = ( n1 < n2 ) ? n2 : n1; | 
|---|
|  | 330 |  | 
|---|
|  | 331 | // allocation des buffers Y2 et tmp et des CSpline 1D | 
|---|
|  | 332 | if( n1>Nel1 || n2>Nel2 || force ) { | 
|---|
|  | 333 | if( Y2  != NULL ) delete [] Y2; | 
|---|
|  | 334 | if( tmp != NULL ) delete [] tmp; | 
|---|
|  | 335 | Y2   = new double[n1*n2]; | 
|---|
|  | 336 | tmp  = new double[n]; | 
|---|
|  | 337 |  | 
|---|
|  | 338 | // et les CSpline[n1] pour memoriser les interpolations sur x1(0->n1) | 
|---|
|  | 339 | if( S != NULL ) { | 
|---|
|  | 340 | for(int i=0;i<Nel_S;i++) if(S[i] != NULL) { delete S[i]; S[i]=NULL;} | 
|---|
|  | 341 | delete S; S = NULL; | 
|---|
|  | 342 | } | 
|---|
|  | 343 | S = new CSpline * [n2]; | 
|---|
|  | 344 | for(int j=0;j<n2;j++) { | 
|---|
|  | 345 | S[j] = new CSpline(n1,NULL,NULL,0.,0.,Natural); | 
|---|
|  | 346 | S[j]->Free_Tmp(); | 
|---|
|  | 347 | } | 
|---|
|  | 348 | Nel_S = n2; | 
|---|
|  | 349 |  | 
|---|
|  | 350 | if( S != NULL ) { delete Sint; Sint = NULL;} | 
|---|
|  | 351 | Sint = new CSpline(n2,NULL,NULL,0.,0.,Natural); | 
|---|
|  | 352 |  | 
|---|
|  | 353 | } | 
|---|
|  | 354 | // des-allocation eventuelle de X1,X2,Y | 
|---|
|  | 355 | if( XY_Created ) { | 
|---|
|  | 356 | if( !order || n1>Nel1 || n2>Nel2 || force ) { | 
|---|
|  | 357 | if( X1 != NULL ) delete [] X1; X1 = NULL; | 
|---|
|  | 358 | if( X2 != NULL ) delete [] X2; X2 = NULL; | 
|---|
|  | 359 | if( Y != NULL )  delete [] Y;  Y  = NULL; | 
|---|
|  | 360 | XY_Created = false; | 
|---|
|  | 361 | } | 
|---|
|  | 362 | } | 
|---|
|  | 363 | // allocation eventuelle de X1,X2,Y | 
|---|
|  | 364 | if( order ) { | 
|---|
|  | 365 | if( !XY_Created || n1>Nel1 || n2>Nel1 || force ) { | 
|---|
|  | 366 | X1 = new double[n1]; | 
|---|
|  | 367 | X2 = new double[n2]; | 
|---|
|  | 368 | Y  = new double[n1*n2]; | 
|---|
|  | 369 | XY_Created = true; | 
|---|
|  | 370 | } | 
|---|
|  | 371 | } | 
|---|
|  | 372 | Nel1 = n1;  Nel2 = n2; | 
|---|
|  | 373 | corrupt_Y2 = true; | 
|---|
|  | 374 |  | 
|---|
|  | 375 | if( x1==NULL || x2==NULL || y==NULL ) return; | 
|---|
|  | 376 |  | 
|---|
|  | 377 | // Classement eventuel par ordre des x1 et x2 croissants | 
|---|
|  | 378 | if( order ) { | 
|---|
|  | 379 |  | 
|---|
|  | 380 | if( tmp == NULL ) tmp = new double[n]; | 
|---|
|  | 381 | ind = (int_4 *) tmp; | 
|---|
|  | 382 | double* Ytmp = new double[n1*n2]; | 
|---|
|  | 383 |  | 
|---|
|  | 384 | // tri par valeur croissantes de x1 | 
|---|
|  | 385 | tri_double(x1,ind,(int_4) Nel1); | 
|---|
|  | 386 | for(int i=0;i<Nel1;i++) { | 
|---|
|  | 387 | X1[i] = x1[ind[i]]; | 
|---|
|  | 388 | if( i>0 ) if( X1[i-1] >= X1[i] ) | 
|---|
|  | 389 | { printf("CSpline::SetNewTab_Erreur: X1[%d]>=X1[%d] (%g>=%g)\n" | 
|---|
|  | 390 | ,i-1,i,X1[i-1],X1[i]); | 
|---|
|  | 391 | THROW(inconsistentErr); } | 
|---|
|  | 392 | for(int j=0;j<Nel2;j++) Ytmp[j*Nel1+i]  = y[j*Nel1+ind[i]]; | 
|---|
|  | 393 | } | 
|---|
|  | 394 |  | 
|---|
|  | 395 | // tri par valeur croissantes de x2 | 
|---|
|  | 396 | tri_double(x2,ind,(int_4) Nel2); | 
|---|
|  | 397 | for(int j=0;j<Nel2;j++) { | 
|---|
|  | 398 | X2[j] = x2[ind[j]]; | 
|---|
|  | 399 | if( j>0 ) if( X2[j-1] >= X2[j] ) | 
|---|
|  | 400 | { printf("CSpline::SetNewTab_Erreur: X2[%d]>=X2[%d] (%g>=%g)\n" | 
|---|
|  | 401 | ,j-1,j,X2[j-1],X2[j]); | 
|---|
|  | 402 | THROW(inconsistentErr); } | 
|---|
|  | 403 | for(int i=0;i<Nel1;i++) Y[j*Nel1+i] = Ytmp[j*Nel1+ind[i]]; | 
|---|
|  | 404 | } | 
|---|
|  | 405 | delete [] Ytmp; | 
|---|
|  | 406 |  | 
|---|
|  | 407 | } else { | 
|---|
|  | 408 |  | 
|---|
|  | 409 | X1 = x1; | 
|---|
|  | 410 | X2 = x2; | 
|---|
|  | 411 | Y  = y; | 
|---|
|  | 412 |  | 
|---|
|  | 413 | } | 
|---|
|  | 414 |  | 
|---|
|  | 415 | } | 
|---|
|  | 416 |  | 
|---|
|  | 417 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 418 | void CSpline2::DelTab() | 
|---|
|  | 419 | { | 
|---|
|  | 420 | if( X1  != NULL && XY_Created ) delete [] X1;  X1   = NULL; | 
|---|
|  | 421 | if( X2  != NULL && XY_Created ) delete [] X2;  X2   = NULL; | 
|---|
|  | 422 | if( Y   != NULL && XY_Created ) delete [] Y;   Y   = NULL; | 
|---|
|  | 423 | if( Y2  != NULL ) delete [] Y2;   Y2  = NULL; | 
|---|
|  | 424 | if( tmp != NULL ) delete [] tmp;  tmp = NULL; | 
|---|
|  | 425 | if( S != NULL ) { | 
|---|
|  | 426 | for(int i=0;i<Nel_S;i++) if(S[i] != NULL) { delete S[i]; S[i]=NULL;} | 
|---|
|  | 427 | delete S; S = NULL; | 
|---|
|  | 428 | } | 
|---|
|  | 429 | if( Sint != NULL ) { delete Sint; Sint=NULL;} | 
|---|
|  | 430 | } | 
|---|
|  | 431 |  | 
|---|
|  | 432 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 433 | //++ | 
|---|
|  | 434 | void CSpline2::ComputeCSpline() | 
|---|
|  | 435 | // | 
|---|
|  | 436 | //      Voir commentaire meme methode de CSpline | 
|---|
|  | 437 | //-- | 
|---|
|  | 438 | { | 
|---|
|  | 439 | // on ne fait rien si X1 ou X2 ou Y non connectes | 
|---|
|  | 440 | if( X1 == NULL || X2 == NULL || Y == NULL ) return; | 
|---|
|  | 441 | // On ne fait rien si rien n'a change | 
|---|
|  | 442 | if( ! corrupt_Y2 ) return; | 
|---|
|  | 443 |  | 
|---|
|  | 444 | for(int j=0; j<Nel2; j++) { | 
|---|
|  | 445 | // on n'alloue pas de place nouvelle, on utilise CSpline2::tmp | 
|---|
|  | 446 | S[j]->tmp = tmp; | 
|---|
|  | 447 | // connection de X1,Y au spline 1D sans ordre demande | 
|---|
|  | 448 | S[j]->SetNewTab(Nel1,X1,&Y[j*Nel1],false,false); | 
|---|
|  | 449 | // calcul des coeff splien pour l'interpolation future | 
|---|
|  | 450 | S[j]->ComputeCSpline(); | 
|---|
|  | 451 | } | 
|---|
|  | 452 |  | 
|---|
|  | 453 | corrupt_Y2 = false; | 
|---|
|  | 454 | } | 
|---|
|  | 455 |  | 
|---|
|  | 456 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 457 | //++ | 
|---|
|  | 458 | double CSpline2::CSplineInt(double x1,double x2) | 
|---|
|  | 459 | // | 
|---|
|  | 460 | //      Voir commentaire meme methode de CSpline | 
|---|
|  | 461 | //-- | 
|---|
|  | 462 | { | 
|---|
|  | 463 | // calcul de la valeur Y pour x=x1 et remplissage du tampon tmp | 
|---|
|  | 464 | for(int j=0;j<Nel2;j++) tmp[j] = S[j]->CSplineInt(x1); | 
|---|
|  | 465 |  | 
|---|
|  | 466 | // connection X2,tmp pour interpolation selon x=x2 | 
|---|
|  | 467 | Sint->SetNewTab(Nel2,X2,tmp,false,false); | 
|---|
|  | 468 | // calcul des coeff pour interpolation selon X2 | 
|---|
|  | 469 | Sint->ComputeCSpline(); | 
|---|
|  | 470 | // Interpolation finale | 
|---|
|  | 471 | return Sint->CSplineInt(x2); | 
|---|
|  | 472 | } | 
|---|
|  | 473 |  | 
|---|
|  | 474 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 475 | ///////// rappel des inlines pour commenatires //////////////// | 
|---|
|  | 476 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 477 |  | 
|---|
|  | 478 | //++ | 
|---|
|  | 479 | // inline void SetNaturalCSpline(int type = NaturalAll) | 
|---|
|  | 480 | //      Voir commentaire meme methode de CSpline | 
|---|
|  | 481 | //-- | 
|---|