CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
math_other.hpp
1 #ifndef CPPAD_CG_MATH_OTHER_INCLUDED
2 #define CPPAD_CG_MATH_OTHER_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 
20 template <class Base>
22  const CppAD::cg::CG<Base>& y) {
23  using namespace CppAD::cg;
24 
25  if (x.isParameter() && y.isParameter()) {
26  return CG<Base> (pow(x.getValue(), y.getValue()));
27  }
28 
29  CodeHandler<Base>* handler;
30  if (y.isParameter()) {
31  if (y.isIdenticalZero()) {
32  return CG<Base> (Base(1.0)); // does not consider that x could be infinity
33  } else if (y.isIdenticalOne()) {
34  return CG<Base> (x);
35  }
36  handler = x.getCodeHandler();
37  } else {
38  handler = y.getCodeHandler();
39  }
40 
41  CG<Base> result(*handler->makeNode(CGOpCode::Pow,{x.argument(), y.argument()}));
42  if (x.isValueDefined() && y.isValueDefined()) {
43  result.setValue(pow(x.getValue(), y.getValue()));
44  }
45  return result;
46 }
47 
48 /*******************************************************************************
49  * pow() with other types
50  ******************************************************************************/
51 
52 template <class Base>
53 inline CppAD::cg::CG<Base> pow(const Base& x,
54  const CppAD::cg::CG<Base>& y) {
55  return CppAD::pow<Base>(CppAD::cg::CG<Base>(x), y);
56 }
57 
58 template <class Base>
60  const Base& y) {
61  return CppAD::pow<Base>(x, CppAD::cg::CG<Base>(y));
62 }
63 
64 template <class Base>
65 CppAD::cg::CG<Base> pow(const int& x,
66  const CppAD::cg::CG<Base>& y) {
67  return pow(CppAD::cg::CG<Base>(x), y);
68 }
69 
70 /*******************************************************************************
71  *
72  ******************************************************************************/
73 template <class Base>
75  using namespace CppAD::cg;
76 
77  if (x.isParameter()) {
78  if (x.getValue() > Base(0.0)) {
79  return CG<Base> (Base(1.0));
80  } else if (x.getValue() == Base(0.0)) {
81  return CG<Base> (Base(0.0));
82  } else {
83  return CG<Base> (Base(-1.0));
84  }
85  }
86 
87  CodeHandler<Base>& h = *x.getOperationNode()->getCodeHandler();
88  CG<Base> result(*h.makeNode(CGOpCode::Sign, x.argument()));
89  if (x.isValueDefined()) {
90  if (x.getValue() > Base(0.0)) {
91  result.setValue(Base(1.0));
92  } else if (x.getValue() == Base(0.0)) {
93  result.setValue(Base(0.0));
94  } else {
95  result.setValue(Base(-1.0));
96  }
97  }
98  return result;
99 }
100 
101 } // END CppAD namespace
102 
103 #endif
const Base & getValue() const
Definition: variable.hpp:45
void setValue(const Base &val)
Definition: variable.hpp:54
cg::CG< Base > pow(const cg::CG< Base > &x, const cg::CG< Base > &y)
Definition: math_other.hpp:21
bool isValueDefined() const
Definition: variable.hpp:40
CodeHandler< Base > * getCodeHandler() const
Definition: variable.hpp:22
bool isParameter() const
Definition: variable.hpp:35
cg::CG< Base > sign(const cg::CG< Base > &x)
Definition: math_other.hpp:74