00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004
00005 #include <cmath>
00006
00007 #include <TMath.h>
00008 #include <Math/SpecFuncMathCore.h>
00009
00010 #include <TApplication.h>
00011
00012 #include <TCanvas.h>
00013 #include <TH2F.h>
00014 #include <TGraph.h>
00015 #include <TLegend.h>
00016
00017 const double ERRORLIMIT = 1E-8;
00018 const double MIN = -2.5;
00019 const double MAX = +2.5;
00020 const double INCREMENT = 0.01;
00021 const int ARRAYSIZE = (int) (( MAX - MIN ) / INCREMENT) + 1;
00022
00023 bool showGraphics = true;
00024
00025 using namespace std;
00026
00027 TGraph* drawPoints(Double_t x[], Double_t y[], int color, int style = 1)
00028 {
00029 TGraph* g = new TGraph(ARRAYSIZE, x, y);
00030 g->SetLineColor(color);
00031 g->SetLineStyle(style);
00032 g->SetLineWidth(3);
00033 g->Draw("SAME");
00034
00035 return g;
00036 }
00037
00038 int testSpecFuncGamma()
00039 {
00040 vector<Double_t> x( ARRAYSIZE );
00041 vector<Double_t> yg( ARRAYSIZE );
00042 vector<Double_t> ymtg( ARRAYSIZE );
00043 vector<Double_t> yga( ARRAYSIZE );
00044 vector<Double_t> ymga( ARRAYSIZE );
00045 vector<Double_t> ylng( ARRAYSIZE );
00046 vector<Double_t> ymlng( ARRAYSIZE );
00047
00048 Double_t a = 0.56;
00049
00050 int status = 0;
00051
00052
00053
00054 unsigned int index = 0;
00055 for ( double i = MIN; i < MAX; i += INCREMENT )
00056 {
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 x[index] = i;
00068 yg[index] = TMath::Gamma(i);
00069 ymtg[index] = ROOT::Math::tgamma(i);
00070
00071 if ( std::fabs(yg[index]) < 1E+12 && std::fabs( yg[index] - ymtg[index] ) > ERRORLIMIT )
00072 {
00073 cout << "i " << i
00074 << " yg[index] " << yg[index]
00075 << " ymtg[index] " << ymtg[index]
00076 << " " << std::fabs( yg[index] - ymtg[index] )
00077 << endl;
00078 status += 1;
00079 }
00080
00081 yga[index] = TMath::Gamma(a, i);
00082 ymga[index] = ROOT::Math::inc_gamma(a, i);
00083 if ( std::fabs( yga[index] - ymga[index] ) > ERRORLIMIT )
00084 {
00085 cout << "i " << i
00086 << " yga[index] " << yga[index]
00087 << " ymga[index] " << ymga[index]
00088 << " " << std::fabs( yga[index] - ymga[index] )
00089 << endl;
00090 status += 1;
00091 }
00092
00093 ylng[index] = TMath::LnGamma(i);
00094 ymlng[index] = ROOT::Math::lgamma(i);
00095 if ( std::fabs( ylng[index] - ymlng[index] ) > ERRORLIMIT )
00096 {
00097 cout << "i " << i
00098 << " ylng[index] " << ylng[index]
00099 << " ymlng[index] " << ymlng[index]
00100 << " " << std::fabs( ylng[index] - ymlng[index] )
00101 << endl;
00102 status += 1;
00103 }
00104
00105 index += 1;
00106 }
00107
00108 if ( showGraphics )
00109 {
00110
00111 TCanvas* c1 = new TCanvas("c1", "Two Graphs", 600, 400);
00112 TH2F* hpx = new TH2F("hpx", "Two Graphs(hpx)", ARRAYSIZE, MIN, MAX, ARRAYSIZE, -1,5);
00113 hpx->SetStats(kFALSE);
00114 hpx->Draw();
00115
00116 TGraph* gg = drawPoints(&x[0], &yg[0], 1);
00117 TGraph* gmtg = drawPoints(&x[0], &ymtg[0], 2, 7);
00118 TGraph* gga = drawPoints(&x[0], &yga[0], 3);
00119 TGraph* gmga = drawPoints(&x[0], &ymga[0], 4, 7);
00120 TGraph* glng = drawPoints(&x[0], &ylng[0], 5);
00121 TGraph* gmlng = drawPoints(&x[0], &ymlng[0], 6, 7);
00122
00123 TLegend* legend = new TLegend(0.61,0.52,0.86,0.86);
00124 legend->AddEntry(gg, "TMath::Gamma()");
00125 legend->AddEntry(gmtg, "ROOT::Math::tgamma()");
00126 legend->AddEntry(gga, "TMath::GammaI()");
00127 legend->AddEntry(gmga, "ROOT::Math::inc_gamma()");
00128 legend->AddEntry(glng, "TMath::LnGamma()");
00129 legend->AddEntry(gmlng, "ROOT::Math::lgamma()");
00130 legend->Draw();
00131
00132 c1->Show();
00133 }
00134
00135 cout << "Test Done!" << endl;
00136
00137 return status;
00138 }
00139
00140
00141 int main(int argc, char **argv)
00142 {
00143 if ( argc > 1 && argc != 2 )
00144 {
00145 cerr << "Usage: " << argv[0] << " [-ng]\n";
00146 cerr << " where:\n";
00147 cerr << " -ng : no graphics mode";
00148 cerr << endl;
00149 exit(1);
00150 }
00151
00152 if ( argc == 2 && strcmp( argv[1], "-ng") == 0 )
00153 {
00154 showGraphics = false;
00155 }
00156
00157 TApplication* theApp = 0;
00158 if ( showGraphics )
00159 theApp = new TApplication("App",&argc,argv);
00160
00161 int status = testSpecFuncGamma();
00162
00163 if ( showGraphics )
00164 {
00165 theApp->Run();
00166 delete theApp;
00167 theApp = 0;
00168 }
00169
00170 return status;
00171 }