ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
testSpecFuncErf.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 
5 #include <cmath>
6 
7 #include <TMath.h>
8 #include <Math/SpecFunc.h>
9 
10 // #include "SpecFuncCephes.h"
11 
12 #include <TApplication.h>
13 
14 #include <TCanvas.h>
15 #include <TH2F.h>
16 #include <TGraph.h>
17 #include <TLegend.h>
18 
19 const double ERRORLIMIT = 1E-8;
20 const double MIN = -2.5;
21 const double MAX = +2.5;
22 const double INCREMENT = 0.01;
23 const int ARRAYSIZE = (int) (( MAX - MIN ) / INCREMENT) + 1;
24 
25 bool showGraphics = false;
26 bool verbose = false;
27 
28 using namespace std;
29 
30 TGraph* drawPoints(Double_t x[], Double_t y[], int color, int style = 1)
31 {
32  TGraph* g = new TGraph(ARRAYSIZE, x, y);
33  g->SetLineColor(color);
34  g->SetLineStyle(style);
35  g->SetLineWidth(3);
36  g->Draw("SAME");
37 
38  return g;
39 }
40 
42 {
43  vector<Double_t> x( ARRAYSIZE );
44  vector<Double_t> yerf( ARRAYSIZE );
45  vector<Double_t> ymerf( ARRAYSIZE );
46  vector<Double_t> yerfc( ARRAYSIZE );
47  vector<Double_t> ymerfc( ARRAYSIZE );
48  vector<Double_t> yierf( ARRAYSIZE );
49  vector<Double_t> yierfc( ARRAYSIZE );
50 // vector<Double_t> yndtri( ARRAYSIZE );
51 
52  int status = 0;
53 
54 // ofstream outputFile ("values.txt");
55 
56  unsigned int index = 0;
57  for ( double i = MIN; i < MAX; i += INCREMENT )
58  {
59 // outputFile << "i:"; outputFile.width(5); outputFile << i
60 // << " index: "; outputFile.width(5); outputFile << index
61 // << " TMath::Erf(x): "; outputFile.width(10); outputFile << TMath::Erf(i)
62 // << " ROOT::Math::erf(x): "; outputFile.width(10); outputFile << ROOT::Math::erf(i)
63 // << " TMath::Erfc(x): "; outputFile.width(10); outputFile << TMath::Erfc(i)
64 // << " ROOT::Math::erfc(x): "; outputFile.width(10); outputFile << ROOT::Math::erfc(i)
65 // << " TMath::ErfInverse(x): "; outputFile.width(10); outputFile << TMath::ErfInverse(i)
66 // << " TMath::ErfcInverse(x): "; outputFile.width(10); outputFile << TMath::ErfcInverse(i)
67 // // << " ROOT::Math::Cephes::ndtri(x): "; outputFile.width(10); outputFile << ROOT::Math::Cephes::ndtri(i)
68 // << endl;
69 
70  x[index] = i;
71 
72  yerf[index] = TMath::Erf(i);
73  ymerf[index] = ROOT::Math::erf(i);
74  if ( std::fabs( yerf[index] - ymerf[index] ) > ERRORLIMIT )
75  {
76  cout << "i " << i
77  << " yerf[index] " << yerf[index]
78  << " ymerf[index] " << ymerf[index]
79  << " " << std::fabs( yerf[index] - ymerf[index] )
80  << endl;
81  status += 1;
82  }
83 
84  yerfc[index] = TMath::Erfc(i);
85  ymerfc[index] = ROOT::Math::erfc(i);
86  if ( std::fabs( yerfc[index] - ymerfc[index] ) > ERRORLIMIT )
87  {
88  cout << "i " << i
89  << " yerfc[index] " << yerfc[index]
90  << " ymerfc[index] " << ymerfc[index]
91  << " " << std::fabs( yerfc[index] - ymerfc[index] )
92  << endl;
93  status += 1;
94  }
95 
96  yierf[index] = TMath::ErfInverse(yerf[index]);
97  if ( std::fabs( yierf[index] - i ) > ERRORLIMIT )
98  {
99  cout << "i " << i
100  << " yierf[index] " << yierf[index]
101  << " " << std::fabs( yierf[index] - i )
102  << endl;
103  status += 1;
104  }
105 
106  yierfc[index] = TMath::ErfcInverse(yerfc[index]);
107  if ( std::fabs( yierfc[index] - i ) > ERRORLIMIT )
108  {
109  cout << "i " << i
110  << " yierfc[index] " << yierfc[index]
111  << " " << std::fabs( yierfc[index] - i )
112  << endl;
113  status += 1;
114  }
115 
116 // yndtri[index] = ROOT::Math::Cephes::ndtri(i);
117 
118  index += 1;
119  }
120 
121  if ( showGraphics )
122  {
123 
124  TCanvas* c1 = new TCanvas("c1", "Two Graphs", 600, 400);
125  TH2F* hpx = new TH2F("hpx", "Two Graphs(hpx)", ARRAYSIZE, MIN, MAX, ARRAYSIZE, -1,2);
126  hpx->SetStats(kFALSE);
127  hpx->Draw();
128 
129  TGraph* gerf = drawPoints(&x[0], &yerf[0], 14);
130  TGraph* gmerf = drawPoints(&x[0], &ymerf[0], 5, 7);
131  TGraph* gerfc = drawPoints(&x[0], &yerfc[0], 2);
132  TGraph* gmerfc = drawPoints(&x[0], &ymerfc[0], 3, 7);
133 // drawPoints(&x[0], &yierf[0], 21);
134 // drawPoints(&x[0], &yierfc[0], 28);
135 // drawPoints(&x[0], &yndtri[0], 9);
136 
137  TLegend* legend = new TLegend(0.61,0.62,0.86,0.86);
138  legend->AddEntry(gerf, "TMath::Erf()");
139  legend->AddEntry(gmerf, "ROOT:Math::erf()");
140  legend->AddEntry(gerfc, "TMath::Erfc()");
141  legend->AddEntry(gmerfc, "ROOT::Math::erfInverse()");
142  legend->Draw();
143 
144  c1->Show();
145  }
146 
147  cout << "Test Done!" << endl;
148 
149  return status;
150 }
151 
152 int main(int argc, char **argv)
153 {
154 
155  // Parse command line arguments
156  for (Int_t i=1 ; i<argc ; i++) {
157  std::string arg = argv[i] ;
158  if (arg == "-g") {
159  showGraphics = true;
160  }
161  if (arg == "-v") {
162  showGraphics = true;
163  //verbose = true;
164  }
165  if (arg == "-h") {
166  cerr << "Usage: " << argv[0] << " [-g] [-v]\n";
167  cerr << " where:\n";
168  cerr << " -g : graphics mode\n";
169  cerr << " -v : verbose mode";
170  cerr << endl;
171  return -1;
172  }
173  }
174 
175  TApplication* theApp = 0;
176  if ( showGraphics )
177  theApp = new TApplication("App",&argc,argv);
178 
179  int status = testSpecFuncErf();
180 
181  if ( showGraphics )
182  {
183  theApp->Run();
184  delete theApp;
185  theApp = 0;
186  }
187 
188  return status;
189 }
double erf(double x)
Error function encountered in integrating the normal distribution.
Double_t ErfInverse(Double_t x)
returns the inverse error function x must be <-1<x<1
Definition: TMath.cxx:206
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
TCanvas * c1
Definition: legend1.C:2
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
int Int_t
Definition: RtypesCore.h:41
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
const Bool_t kFALSE
Definition: Rtypes.h:92
const double INCREMENT
TLegend * legend
Definition: pirndm.C:35
double erfc(double x)
Complementary error function.
Double_t x[n]
Definition: legend1.C:17
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
const double ERRORLIMIT
Double_t Erfc(Double_t x)
Compute the complementary error function erfc(x).
Definition: TMath.cxx:197
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Definition: TMath.cxx:187
Double_t E()
Definition: TMath.h:54
Double_t ErfcInverse(Double_t x)
Definition: TMath.cxx:233
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
TCanvas * style()
Definition: style.C:1
Double_t y[n]
Definition: legend1.C:17
int main(int argc, char **argv)
TGraph * drawPoints(Double_t x[], Double_t y[], int color, int style=1)
const int ARRAYSIZE
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
const double MIN
TH1F * hpx
Definition: hcons.C:32
const double MAX
int testSpecFuncErf()