testSpecFuncErf.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <vector>
00004 
00005 #include <cmath>
00006 
00007 #include <TMath.h>
00008 #include <Math/SpecFunc.h>
00009 
00010 // #include "SpecFuncCephes.h"
00011 
00012 #include <TApplication.h>
00013 
00014 #include <TCanvas.h>
00015 #include <TH2F.h>
00016 #include <TGraph.h>
00017 #include <TLegend.h>
00018 
00019 const double ERRORLIMIT = 1E-8;
00020 const double MIN = -2.5;
00021 const double MAX = +2.5;
00022 const double INCREMENT = 0.01;
00023 const int ARRAYSIZE = (int) (( MAX - MIN ) / INCREMENT) + 1;
00024 
00025 bool showGraphics = true;
00026 
00027 using namespace std;
00028 
00029 TGraph* drawPoints(Double_t x[], Double_t y[], int color, int style = 1)
00030 {
00031    TGraph* g = new TGraph(ARRAYSIZE, x, y);
00032    g->SetLineColor(color);
00033    g->SetLineStyle(style);
00034    g->SetLineWidth(3);
00035    g->Draw("SAME");
00036 
00037    return g;
00038 }
00039 
00040 int testSpecFuncErf() 
00041 {
00042    vector<Double_t> x( ARRAYSIZE );
00043    vector<Double_t> yerf( ARRAYSIZE );
00044    vector<Double_t> ymerf( ARRAYSIZE );
00045    vector<Double_t> yerfc( ARRAYSIZE );
00046    vector<Double_t> ymerfc( ARRAYSIZE );
00047    vector<Double_t> yierf( ARRAYSIZE );
00048    vector<Double_t> yierfc( ARRAYSIZE );
00049 //    vector<Double_t> yndtri( ARRAYSIZE );
00050 
00051    int status = 0;
00052 
00053 //    ofstream outputFile ("values.txt");
00054 
00055    unsigned int index = 0;
00056    for ( double i = MIN; i < MAX; i += INCREMENT )
00057    {
00058 //       outputFile << "i:"; outputFile.width(5); outputFile << i 
00059 //            << " index: "; outputFile.width(5); outputFile << index 
00060 //            << " TMath::Erf(x): "; outputFile.width(10); outputFile << TMath::Erf(i)
00061 //            << " ROOT::Math::erf(x): "; outputFile.width(10); outputFile << ROOT::Math::erf(i)
00062 //            << " TMath::Erfc(x): "; outputFile.width(10); outputFile << TMath::Erfc(i)
00063 //            << " ROOT::Math::erfc(x): "; outputFile.width(10); outputFile << ROOT::Math::erfc(i)
00064 //            << " TMath::ErfInverse(x): "; outputFile.width(10); outputFile << TMath::ErfInverse(i)
00065 //            << " TMath::ErfcInverse(x): "; outputFile.width(10); outputFile << TMath::ErfcInverse(i)
00066 // //            << " ROOT::Math::Cephes::ndtri(x): "; outputFile.width(10); outputFile << ROOT::Math::Cephes::ndtri(i)
00067 //            << endl;
00068 
00069       x[index] = i;
00070 
00071       yerf[index] = TMath::Erf(i);
00072       ymerf[index] = ROOT::Math::erf(i);
00073       if ( std::fabs( yerf[index] - ymerf[index] ) > ERRORLIMIT )
00074       {
00075          cout << "i " << i   
00076               << " yerf[index] " << yerf[index]
00077               << " ymerf[index] " << ymerf[index]
00078               << " " << std::fabs( yerf[index] - ymerf[index] )
00079               << endl;
00080          status += 1;
00081       }
00082 
00083       yerfc[index] = TMath::Erfc(i);
00084       ymerfc[index] = ROOT::Math::erfc(i);
00085       if ( std::fabs( yerfc[index] - ymerfc[index] ) > ERRORLIMIT )
00086       {
00087          cout << "i " << i 
00088               << " yerfc[index] " << yerfc[index]
00089               << " ymerfc[index] " << ymerfc[index]
00090               << " " << std::fabs( yerfc[index] - ymerfc[index] )
00091               << endl;
00092          status += 1;
00093       }
00094 
00095       yierf[index] = TMath::ErfInverse(yerf[index]);
00096       if ( std::fabs( yierf[index] - i ) > ERRORLIMIT )
00097       {
00098          cout << "i " << i 
00099               << " yierf[index] " << yierf[index]
00100               << " " << std::fabs( yierf[index] - i )
00101               << endl;
00102          status += 1;
00103       }
00104 
00105       yierfc[index] = TMath::ErfcInverse(yerfc[index]);
00106       if ( std::fabs( yierfc[index] - i ) > ERRORLIMIT )
00107       {
00108          cout << "i " << i 
00109               << " yierfc[index] " << yierfc[index]
00110               << " " << std::fabs( yierfc[index] - i )
00111               << endl;
00112          status += 1;
00113       }
00114 
00115 //       yndtri[index] = ROOT::Math::Cephes::ndtri(i);
00116 
00117       index += 1;
00118    }
00119 
00120    if ( showGraphics )
00121    {
00122 
00123       TCanvas* c1 = new TCanvas("c1", "Two Graphs", 600, 400); 
00124       TH2F* hpx = new TH2F("hpx", "Two Graphs(hpx)", ARRAYSIZE, MIN, MAX, ARRAYSIZE, -1,2);
00125       hpx->SetStats(kFALSE);
00126       hpx->Draw();
00127       
00128       TGraph* gerf   = drawPoints(&x[0], &yerf[0], 14);
00129       TGraph* gmerf  = drawPoints(&x[0], &ymerf[0], 5, 7);
00130       TGraph* gerfc  = drawPoints(&x[0], &yerfc[0], 2);
00131       TGraph* gmerfc = drawPoints(&x[0], &ymerfc[0], 3, 7);
00132 //   drawPoints(&x[0], &yierf[0], 21);
00133 //   drawPoints(&x[0], &yierfc[0], 28);
00134 //   drawPoints(&x[0], &yndtri[0], 9);
00135 
00136       TLegend* legend = new TLegend(0.61,0.62,0.86,0.86);
00137       legend->AddEntry(gerf,   "TMath::Erf()");
00138       legend->AddEntry(gmerf,  "ROOT:Math::erf()");
00139       legend->AddEntry(gerfc,  "TMath::Erfc()");
00140       legend->AddEntry(gmerfc, "ROOT::Math::erfInverse()");
00141       legend->Draw();
00142       
00143       c1->Show();
00144    }
00145       
00146    cout << "Test Done!" << endl;
00147 
00148    return status;
00149 }
00150 
00151 int main(int argc, char **argv)
00152 {
00153    if ( argc > 1 && argc != 2 )
00154    {
00155       cerr << "Usage: " << argv[0] << " [-ng]\n";
00156       cerr << "  where:\n";
00157       cerr << "     -ng : no graphics mode";
00158       cerr << endl;
00159       exit(1);
00160    }
00161 
00162    if ( argc == 2 && strcmp( argv[1], "-ng") == 0 ) 
00163    {
00164       showGraphics = false;
00165    }
00166 
00167    TApplication* theApp = 0;
00168    if ( showGraphics )
00169       theApp = new TApplication("App",&argc,argv);
00170 
00171    int status = testSpecFuncErf();
00172 
00173    if ( showGraphics )
00174    {
00175       theApp->Run();
00176       delete theApp;
00177       theApp = 0;
00178    }
00179 
00180    return status;
00181 }

Generated on Tue Jul 5 14:34:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1