#ifndef ROOT_TSonataMath #define ROOT_TSonataMath // Copyright (C) 1994 CodeCERN. All rights reserved. ////////////////////////////////////////////////////////////////////////// // // // TSonataMath // // // // Encapsulate math routines. For the time being avoid templates. // // // ////////////////////////////////////////////////////////////////////////// typedef unsigned char Bool_t; class TSonataMath { public: static const double Pi, E; // Trigo static double Sin(double); static double Cos(double); static double Tan(double); static double ASin(double); static double ACos(double); static double ATan(double); static double ATan2(double, double); static double Hypot(double x, double y); static void Sincos(double a, double *sin, double *cos); // Misc static double Sqrt(double x); static double Ceil(double x); static double Floor(double x); static double Exp(double); static double Power(double x, double y); static double Log(double x); static double Log2(double x); static double Log10(double x); static int Nint(float x); static int Nint(double x); static int Int(float x); static int Int(double x); // Some integer math static long NextPrime(long x); // least prime number greater than x static long Sqrt(long x); static double Atan2(long x, long y); static long Hypot(long x, long y); // sqrt(px*px + py*py) // Abs static short Abs(short d); static int Abs(int d); static long Abs(long d); static float Abs(float d); static double Abs(double d); // Even/Odd static Bool_t Even(long a); static Bool_t Odd(long a); // Signum static short Sign(short a); static int Sign(int a); static long Sign(long a); static double Sign(double a); // Min static short Min(short a, short b); static unsigned short Min(unsigned short a, unsigned short b); static int Min(int a, int b); static unsigned int Min(unsigned int a, unsigned int b); static long Min(long a, long b); static unsigned long Min(unsigned long a, unsigned long b); static float Min(float a, float b); static double Min(double a, double b); // Max static short Max(short a, short b); static unsigned short Max(unsigned short a, unsigned short b); static int Max(int a, int b); static unsigned int Max(unsigned int a, unsigned int b); static long Max(long a, long b); static unsigned long Max(unsigned long a, unsigned long b); static float Max(float a, float b); static double Max(double a, double b); // Range static short Range(short lb, short ub, short x); static int Range(int lb, int ub, int x); static long Range(long lb, long ub, long x); static unsigned long Range(unsigned long lb, unsigned long ub, unsigned long x); static double Range(double lb, double ub, double x); //Added by Kris Hagel for Sonata static float Erf(float x); static double Erf(double x); static float FGauss(); static int MOD(int dividend,int divisor); static void vzero(int *arr,int len); //void vzero(TBrDigitizedTPC* *arr,Int_t len); //can the other one be used with casting static void DEQINV(int n,double **a,int idim,float *r,int *ifail,int k,double **b); static void DFACT(int n,double **a,int idim,float *r,int *ifail, double *det,int *jfail); static void DFEQN(int n,double **a,int idim,float *r,int k,double **b); static void DFINV(int n,double **a,int idim,float *r); }; //---- Misc -------------------------------------------------------------------- inline int TSonataMath::Nint(float x) { int i = int(x + 0.5); if (x + 0.5 == float(i) && i & 1) i--; return i; } inline int TSonataMath::Nint(double x) { int i = int(x + 0.5); if (x + 0.5 == double(i) && i & 1) i--; return i; } inline int TSonataMath::Int(float x) { int i; if( x > 0 ) { i = int(x + 0.5); if (x + 0.5 == float(i) && i & 1) i--; } else { i = int(x - 0.5); if (x - 0.5 == float(i) && i & 1) i++; } return i; } inline int TSonataMath::Int(double x) { int i; if( x > 0 ) { i = int(x + 0.5); if (x + 0.5 == double(i) && i & 1) i--; } else { i = int(x - 0.5); if (x - 0.5 == double(i) && i & 1) i++; } return i; } //---- Sign -------------------------------------------------------------------- inline short TSonataMath::Sign(short a) { return (a == 0) ? 0 : ( (a > 0) ? 1 : -1 ); } inline int TSonataMath::Sign(int a) { return (a == 0) ? 0 : ( (a > 0) ? 1 : -1 ); } inline long TSonataMath::Sign(long a) { return (a == 0) ? 0 : ( (a > 0) ? 1 : -1 ); } inline double TSonataMath::Sign(double a) { return (a == 0.0) ? 0 : ( (a > 0.0) ? 1 : -1 ); } //---- Even/odd ---------------------------------------------------------------- inline Bool_t TSonataMath::Even(long a) { return ! (a & 1); } inline Bool_t TSonataMath::Odd(long a) { return (a & 1); } //---- Abs --------------------------------------------------------------------- inline short TSonataMath::Abs(short d) { return (d > 0) ? d : -d; } inline int TSonataMath::Abs(int d) { return (d > 0) ? d : -d; } inline long TSonataMath::Abs(long d) { return (d > 0) ? d : -d; } inline float TSonataMath::Abs(float d) { return (d > 0) ? d : -d; } inline double TSonataMath::Abs(double d) { return (d > 0) ? d : -d; } //---- Min --------------------------------------------------------------------- inline short TSonataMath::Min(short a, short b) { return a <= b ? a : b; } inline unsigned short TSonataMath::Min(unsigned short a, unsigned short b) { return a <= b ? a : b; } inline int TSonataMath::Min(int a, int b) { return a <= b ? a : b; } inline unsigned int TSonataMath::Min(unsigned int a, unsigned int b) { return a <= b ? a : b; } inline long TSonataMath::Min(long a, long b) { return a <= b ? a : b; } inline unsigned long TSonataMath::Min(unsigned long a, unsigned long b) { return a <= b ? a : b; } inline float TSonataMath::Min(float a, float b) { return a <= b ? a : b; } inline double TSonataMath::Min(double a, double b) { return a <= b ? a : b; } //---- Max --------------------------------------------------------------------- inline short TSonataMath::Max(short a, short b) { return a >= b ? a : b; } inline unsigned short TSonataMath::Max(unsigned short a, unsigned short b) { return a >= b ? a : b; } inline int TSonataMath::Max(int a, int b) { return a >= b ? a : b; } inline unsigned int TSonataMath::Max(unsigned int a, unsigned int b) { return a >= b ? a : b; } inline long TSonataMath::Max(long a, long b) { return a >= b ? a : b; } inline unsigned long TSonataMath::Max(unsigned long a, unsigned long b) { return a >= b ? a : b; } inline float TSonataMath::Max(float a, float b) { return a >= b ? a : b; } inline double TSonataMath::Max(double a, double b) { return a >= b ? a : b; } //---- Range ------------------------------------------------------------------- inline short TSonataMath::Range(short lb, short ub, short x) { return x < lb ? lb : (x > ub ? ub : x); } inline int TSonataMath::Range(int lb, int ub, int x) { return x < lb ? lb : (x > ub ? ub : x); } inline long TSonataMath::Range(long lb, long ub, long x) { return x < lb ? lb : (x > ub ? ub : x); } inline unsigned long TSonataMath::Range(unsigned long lb, unsigned long ub, unsigned long x) { return x < lb ? lb : (x > ub ? ub : x); } inline double TSonataMath::Range(double lb, double ub, double x) { return x < lb ? lb : (x > ub ? ub : x); } //---- Trig and other functions ------------------------------------------------ #if defined(AIX) || defined(MAC) // in AIX and MAC math functions are defined inline so we have to include them here # include #else // don't want to include complete extern "C" { extern double sin(double); extern double cos(double); extern double tan(double); extern double sqrt(double); extern double exp(double); extern double pow(double, double); extern double log(double); extern double log10(double); } #endif inline double TSonataMath::Sin(double x) { return sin(x); } inline double TSonataMath::Cos(double x) { return cos(x); } inline double TSonataMath::Tan(double x) { return tan(x); } inline double TSonataMath::Sqrt(double x) { return sqrt(x); } inline double TSonataMath::Exp(double x) { return exp(x); } inline double TSonataMath::Power(double x, double y) { return pow(x, y); } inline double TSonataMath::Log(double x) { return log(x); } inline double TSonataMath::Log10(double x) { return log10(x); } #endif