GeoModel - GeoModelCore
Power.cpp
1 //==========================================================================//
2 // Imported from the QAT library (qat.pitt.edu) by the copyright holder //
3 // Joe Boudreau. Software licensed under the terms and conditions of the //
4 // GNU Lesser Public License v3. //
5 //==========================================================================//
6 #include "GeoGenericFunctions/Power.h"
7 #include <cmath> // for pow()
8 #include <stdexcept>
9 namespace GeoGenfun {
10 FUNCTION_OBJECT_IMP(Power)
11 
12 Power::Power(int n):
13  _intPower(n),
14  _asInteger(true)
15 {}
16 
17 Power::Power(unsigned int n):
18  _intPower(n),
19  _asInteger(true)
20 {}
21 
22 Power::Power(double n):
23  _doublePower(n),
24  _asInteger(false)
25 {}
26 
27 Power::Power(const Power & right)
28  : AbsFunction(right),
29  _doublePower(right._doublePower),
30  _intPower(right._intPower),
31  _asInteger(right._asInteger)
32 {}
33 
34 Power::~Power() {
35 }
36 
37 double Power::operator() (double x) const {
38  if (_asInteger) {
39  if (_intPower==0) {
40  return 1;
41  }
42  else if (_intPower>0) {
43  double f = 1;
44  for (int i=0;i<_intPower;i++) {
45  f *=x;
46  }
47  return f;
48  }
49  else {
50  double f = 1;
51  for (int i=0;i<-_intPower;i++) {
52  f /=x;
53  }
54  return f;
55  }
56  }
57  else {
58  return std::pow(x,_doublePower);
59  }
60 
61 }
62 
63 
64 
65 Derivative Power::partial(unsigned int index) const {
66  if (index!=0) throw std::range_error("Power: partial derivative index out of range");
67  if (_asInteger) {
68  const AbsFunction & fPrime = _intPower*Power(_intPower-1);
69  return Derivative(&fPrime);
70  }
71  else {
72  const AbsFunction & fPrime = _doublePower*Power(_doublePower-1);
73  return Derivative(&fPrime);
74  }
75 
76 }
77 
78 
79 } // namespace GeoGenfun
Definition: Abs.h:15