CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Power.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: Power.cc,v 1.4 2003/10/10 17:40:39 garren Exp $
4 #include <cmath> // for pow()
5 
6 namespace Genfun {
8 
9 Power::Power(int n):
10  _intPower(n),
11  _asInteger(true)
12 {}
13 
14 Power::Power(unsigned int n):
15  _intPower(n),
16  _asInteger(true)
17 {}
18 
19 Power::Power(double n):
20  _doublePower(n),
21  _asInteger(false)
22 {}
23 
24 Power::Power(const Power & right)
25  : _doublePower(right._doublePower),
26  _intPower(right._intPower),
27  _asInteger(right._asInteger)
28 {}
29 
31 }
32 
33 double Power::operator() (double x) const {
34  if (_asInteger) {
35  if (_intPower==0) {
36  return 1;
37  }
38  else if (_intPower>0) {
39  double f = 1;
40  for (int i=0;i<_intPower;i++) {
41  f *=x;
42  }
43  return f;
44  }
45  else {
46  double f = 1;
47  for (int i=0;i<-_intPower;i++) {
48  f /=x;
49  }
50  return f;
51  }
52  }
53  else {
54  return pow(x,_doublePower);
55  }
56 
57 }
58 
59 
60 
61 Derivative Power::partial(unsigned int index) const {
62  if (_asInteger) {
63  const AbsFunction & fPrime = _intPower*Power(_intPower-1);
64  return Derivative(&fPrime);
65  }
66  else {
67  const AbsFunction & fPrime = _doublePower*Power(_doublePower-1);
68  return Derivative(&fPrime);
69  }
70 
71 }
72 
73 
74 } // namespace Genfun