source: Sophya/trunk/SophyaLib/NTools/cspline.cc@ 520

Last change on this file since 520 was 520, checked in by ansari, 26 years ago

Portage Mac D. Y.

File size: 12.5 KB
Line 
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//++
27CSpline::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{
41SetNewTab(n,x,y,order,true);
42if( x != NULL && y != NULL) ComputeCSpline();
43
44END_CONSTRUCTOR
45}
46
47//////////////////////////////////////////////////////////////////////////////
48//++
49CSpline::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{
56END_CONSTRUCTOR
57}
58
59//////////////////////////////////////////////////////////////////////////////
60CSpline::~CSpline()
61// destructeur
62{
63DelTab();
64}
65
66//++
67// Titre Methodes
68//--
69
70//////////////////////////////////////////////////////////////////////////////
71//++
72void 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{
84ASSERT( n>3 );
85
86// allocation des buffers Y2 et tmp
87if( 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
94if( 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
102if( order ) {
103 if( !XY_Created || n>Nel || force ) {
104 X = new double[n];
105 Y = new double[n];
106 XY_Created = true;
107 }
108}
109Nel = n;
110corrupt_Y2 = true;
111
112if( x==NULL || y==NULL ) return;
113
114// Classement eventuel par ordre des x croissants
115if( 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//////////////////////////////////////////////////////////////////////////////
133void CSpline::DelTab()
134// destruction des divers tableaux en tenant compte des allocations/connections
135{
136if( X != NULL && XY_Created ) delete [] X; X = NULL;
137if( Y != NULL && XY_Created ) delete [] Y; Y = NULL;
138if( Y2 != NULL ) delete [] Y2; Y2 = NULL;
139if( tmp != NULL ) delete [] tmp; tmp = NULL;
140}
141
142//////////////////////////////////////////////////////////////////////////////
143//++
144void 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{
150if( yp1 == YP1 && ypn == YPn ) return;
151
152YP1 = yp1;
153YPn = ypn;
154
155corrupt_Y2 = true;
156}
157
158//////////////////////////////////////////////////////////////////////////////
159//++
160void 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
167if( 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!
173if( ! corrupt_Y2 ) return;
174// protection si tmp a ete desalloue pour gain de place (ex: CSpline2)
175if( tmp == NULL ) tmp = new double[Nel];
176
177double p,qn,sig,un;
178
179if (Natural & Natural1)
180 Y2[0] = tmp[0] = 0.0;
181else {
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
186for (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
194if (Natural & NaturalN)
195 qn = un = 0.0;
196else {
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}
201Y2[Nel-1] = (un-qn*tmp[Nel-2])/(qn*Y2[Nel-2]+1.0);
202for (int k=Nel-2;k>=0;k--) Y2[k] = Y2[k]*Y2[k+1] + tmp[k];
203
204corrupt_Y2 = false;
205}
206
207//////////////////////////////////////////////////////////////////////////////
208//++
209double CSpline::CSplineInt(double x)
210//
211// Interpolation spline en "x"
212//--
213{
214int klo,khi,k;
215double h,b,a,y = 0.;
216
217if( corrupt_Y2 ) {
218 cout<<"CSpline::CSplineInt: calcul des coef du spline corrupted"<<endl;
219 THROW(inconsistentErr);
220}
221
222klo = 0;
223khi = Nel-1;
224while (khi-klo > 1) {
225 k = (khi+klo) >> 1;
226 if (X[k] > x) khi=k;
227 else klo=k;
228}
229h=X[khi]-X[klo];
230
231if (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
237a = (X[khi]-x)/h;
238b = (x-X[klo])/h;
239y = a*Y[klo]+b*Y[khi]+((a*a*a-a)*Y2[klo]+(b*b*b-b)*Y2[khi])*(h*h)/6.0;
240
241return 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//++
275CSpline2::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{
290SetNewTab(n1,x1,n2,x2,y,order,true);
291if( x1 != NULL && x2 != NULL && y != NULL) ComputeCSpline();
292
293END_CONSTRUCTOR
294}
295
296//////////////////////////////////////////////////////////////////////////////
297//++
298CSpline2::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{
306END_CONSTRUCTOR
307}
308
309//////////////////////////////////////////////////////////////////////////////
310CSpline2::~CSpline2()
311{
312DelTab();
313}
314
315//++
316// Titre Methodes
317//--
318
319//////////////////////////////////////////////////////////////////////////////
320//++
321void 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{
327ASSERT( n1>3 && n2>3 );
328
329int n = ( n1 < n2 ) ? n2 : n1;
330
331// allocation des buffers Y2 et tmp et des CSpline 1D
332if( 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
355if( 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
364if( 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}
372Nel1 = n1; Nel2 = n2;
373corrupt_Y2 = true;
374
375if( x1==NULL || x2==NULL || y==NULL ) return;
376
377// Classement eventuel par ordre des x1 et x2 croissants
378if( 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//////////////////////////////////////////////////////////////////////////////
418void CSpline2::DelTab()
419{
420if( X1 != NULL && XY_Created ) delete [] X1; X1 = NULL;
421if( X2 != NULL && XY_Created ) delete [] X2; X2 = NULL;
422if( Y != NULL && XY_Created ) delete [] Y; Y = NULL;
423if( Y2 != NULL ) delete [] Y2; Y2 = NULL;
424if( tmp != NULL ) delete [] tmp; tmp = NULL;
425if( 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}
429if( Sint != NULL ) { delete Sint; Sint=NULL;}
430}
431
432//////////////////////////////////////////////////////////////////////////////
433//++
434void 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
440if( X1 == NULL || X2 == NULL || Y == NULL ) return;
441// On ne fait rien si rien n'a change
442if( ! corrupt_Y2 ) return;
443
444for(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
453corrupt_Y2 = false;
454}
455
456//////////////////////////////////////////////////////////////////////////////
457//++
458double 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
464for(int j=0;j<Nel2;j++) tmp[j] = S[j]->CSplineInt(x1);
465
466// connection X2,tmp pour interpolation selon x=x2
467Sint->SetNewTab(Nel2,X2,tmp,false,false);
468// calcul des coeff pour interpolation selon X2
469Sint->ComputeCSpline();
470// Interpolation finale
471return 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//--
Note: See TracBrowser for help on using the repository browser.