Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-09 10:28:39

0001 #ifndef WS_MATH_UT
0002 #define WS_MATH_UT
0003 //#include <stdlib.h>

0004 
0005 typedef double real_type;
0006 
0007 //***********************************************************************

0008 template <class T> int outrange(const T& x, const T& xmin, const T& xmax)
0009 {return (x < xmin) ? -1 : ((x >= xmax)? 1 : 0);}
0010 
0011 //-----------------------------------------------------------------------

0012 class TLimits {
0013 public:
0014   real_type lo, hi;
0015 protected:
0016   int started;
0017 
0018 public:
0019   TLimits(){ started = 0;}
0020   void Reset(){started = 0;}
0021   void Reset(real_type xl, real_type xh){
0022     lo = xl;
0023     hi = xh;
0024     started = 1;
0025   }
0026 
0027   real_type Lo(){
0028     if(!started) {
0029       cerr << "TLimits::Lo ERROR: not started." << endl;
0030       exit(101);
0031     }
0032     return lo;
0033   }
0034 
0035   real_type Hi(){
0036     if(!started) {
0037       cerr << "TLimits::Hi ERROR: not started." << endl;
0038       exit(101);
0039     }
0040     return hi;
0041   }
0042 
0043   void IncrLo(real_type x){
0044     if(x > lo) lo = x;
0045   }
0046   void DecrHi(real_type x){
0047     if(x < hi) hi = x;
0048   }
0049 
0050   void Intersect(real_type xl, real_type xh){
0051     if(!started) return;
0052     IncrLo(xl);
0053     DecrHi(xh);
0054   }
0055 
0056   void Intersect(const TLimits& lims){
0057     if(!started) return;
0058     IncrLo(lims.lo);
0059     DecrHi(lims.hi);
0060   }
0061 
0062   TLimits Scale(real_type s){
0063     if(!started) return *this;
0064     if(s >= 0.0) {lo = s*lo; hi = s*hi;}
0065     else {real_type lo1 = s*hi; hi = s*lo; lo = lo1;}
0066     return *this;
0067   }
0068 
0069   void Expand(real_type x){
0070     if(started){
0071       if(x < lo) lo = x;
0072       else if(x > hi) hi = x;
0073     }
0074     else {lo = hi = x; started = 1;}
0075   }
0076 
0077   int InRange(real_type x){
0078     return started && (lo < x) && (x < hi);
0079   }
0080 
0081   int InRangeR(real_type x){
0082     return started && (lo < x) && (x <= hi);
0083   }
0084 
0085   int InRangeL(real_type x){
0086     return started && (lo <= x) && (x < hi);
0087   }
0088 
0089   int InRangeLR(real_type x){
0090     return started && (lo <= x) && (x <= hi);
0091   }
0092 
0093   friend ostream& operator << (ostream& ops, TLimits rr){
0094     return ops << "[" << rr.Lo() << ", "
0095         << rr.Hi() << "] ";
0096   }
0097 
0098 };
0099 
0100 //-----------------------------------------------------------------------

0101 struct TTableVar {
0102   real_type step, start, end, v;
0103   int logstep;
0104 
0105   TTableVar(real_type s, real_type e, int np, int logst=0){
0106     start = s;
0107     end = e;
0108     logstep = logst? 1 : 0;
0109     if(np > 1) step = logstep? log(end/start)/(np-1) : (end-start)/(np-1);
0110     else step = logstep;
0111   }
0112 
0113   TTableVar(const TLimits& lims, int np, int logst=0){
0114     *this = TTableVar(lims.lo, lims.hi, np, logst);
0115   }
0116 
0117   real_type Val(int ind){
0118     return v = logstep? start*exp(step*ind) : (start + ind*step);
0119   }
0120 };
0121 
0122 #endif