source: Sophya/trunk/SophyaLib/NTools/functab.cc@ 2751

Last change on this file since 2751 was 2618, checked in by cmv, 21 years ago

Tabulation de fonctions avec acces rapide cmv 13/09/04

File size: 3.4 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include <iostream>
6#include <math.h>
7#include <string.h>
8#include <string>
9
10#include "pexceptions.h"
11#include "functab.h"
12
13/*!
14 \class SOPHYA::FuncTab
15 \ingroup NTools
16 Function tabulation
17*/
18
19/* --Methode-- */
20//! Constructor
21/*!
22 \param func : function to be tabulated
23 \param ntab : number of tabulated points
24 \param xmin xmax : tabulate from xmin to max
25 \verbatim
26 - Tabul[1] contient func(xmin), Tabul[NTab] contient func(xmax)
27 - ATTENTION: Tabul[i] contient func(xmin + (i-1)*Dx)
28 Tabul[i+1] contient func(xmin + i*Dx)
29 - Tabul[0] et Tabul[NTab+1] servent pour interpoler rapidement
30 Par defaut Tabul[0] contient Tabul[1] (=func(xmin))
31 Tabul[NTab+1] contient Tabul[NTab] (=func(xmax))
32 Ces valeurs peuvent etre changees avec les methodes SetLimVal
33 Si la fonction existe pour x<xmin et x>xmax alors le mieux est
34 d'utiliser la methode SetLimVal(void):
35 Tabul[0] = func(xmin-dx)
36 Tabul[NTab+1] = func(xmax+dx)
37 Si la fonction n'existe pas pour x<xmin et x>xmax utiliser
38 la methode SetLimVal(val0,valn):
39 Tabul[0] = val0
40 Tabul[NTab+1] = valn
41 \endverbatim
42
43 */
44FuncTab::FuncTab(double (*func)(double const),uint_4 ntab,r_8 xmin,r_8 xmax)
45{
46 cout<<"func="<<func<<endl;
47 if(ntab<=1) ntab = 10;
48 if(xmin>=xmax) {xmin=0.; xmax=1.;}
49 NTab = ntab;
50 XMin = xmin;
51 XMax = xmax;
52 Func = func;
53 Dx = (r_8)(XMax-XMin)/(r_8)(NTab-1);
54 Tabul = new r_8[NTab+2];
55 for(int_4 i=0;i<NTab+2;i++) Tabul[i] = 0.;
56 PInterpL = PInterpP[0] = PInterpP[1] = NULL;
57 if(!Tabul) return;
58 for(int_4 i=0;i<NTab;i++) Tabul[i+1] = func(XMin + i*Dx);
59 Tabul[0] = Tabul[1];
60 Tabul[NTab+1] = Tabul[NTab];
61}
62
63/* --Methode-- */
64//! Destructor
65FuncTab::~FuncTab(void)
66{
67 if(Tabul) {delete [] Tabul; Tabul=NULL;}
68 if(PInterpL) {delete [] PInterpL; PInterpL=NULL;}
69}
70
71/* --Methode-- */
72//! Set automatically values for the index [0] and [NTab+1]
73/*! Set automatically values for the index [0] and [NTab+1]
74 That is needed for fast interpolation
75 Value are set for xmin-dx and xmax+dx
76 */
77void FuncTab::SetLimVal(void)
78{
79 Tabul[0] = Func(XMin-Dx);
80 Tabul[NTab+1] = Func(XMax+Dx);
81}
82
83
84/* --Methode-- */
85//! Give values for the index [0] and [NTab+1]
86/*! Set automatically values for the index [0] and [NTab+1]
87 That is needed for fast interpolation
88 */
89void FuncTab::SetLimVal(r_8 val0,r_8 valn)
90{
91 Tabul[0] = val0;
92 Tabul[NTab+1] = valn;
93}
94
95/* --Methode-- */
96//! Construc table for linear and parabolic interpolation
97/*!
98 \param typ : 0=nearest tabulated value, 1=linear, 2=parabolic
99 \warning For speed consideration, no protection is done
100 in the corresponding Value?() inline routine.
101 */
102void FuncTab::SetInterp(uint_2 typ)
103{
104 if(typ==1) { // Linear Interpolation between two consecutive values
105
106 if(PInterpL) delete [] PInterpL;
107 PInterpL = new r_8[NTab+2];
108 for(int_4 i=0;i<NTab+1;i++)
109 PInterpL[i] = (Tabul[i+1]-Tabul[i])/Dx;
110 PInterpL[NTab+1] = 0.;
111
112 } else if(typ==2) {
113
114 if(PInterpP[0]) delete [] PInterpP[0];
115 if(PInterpP[1]) delete [] PInterpP[1];
116 PInterpP[0] = new r_8[NTab+2];
117 PInterpP[1] = new r_8[NTab+2];
118 for(int_4 i=1;i<=NTab;i++) {
119 PInterpP[0][i] = (Tabul[i+1]+Tabul[i-1]-2.*Tabul[i])/(2.*Dx*Dx);
120 PInterpP[1][i] = (Tabul[i+1]-Tabul[i-1])/(2.*Dx);
121 }
122 PInterpP[0][0]=PInterpP[0][NTab+1]=PInterpP[1][0]=PInterpP[1][NTab+1]=0.;
123
124 }
125}
Note: See TracBrowser for help on using the repository browser.