46 template <
typename ScalarT>
47 ScalarT
func(
const ScalarT&
a,
const ScalarT& b,
const ScalarT&
c) {
54 double& drda,
double& drdb,
55 double& d2rda2,
double& d2rdb2,
double& d2rdadb)
66 template <
typename ScalarT>
68 ScalarT& r, ScalarT& drda, ScalarT& drdb) {
70 FadType a_fad(2, 0, a);
71 FadType b_fad(2, 1, b);
74 FadType r_fad =
func(a_fad, b_fad, c_fad);
90 template <
typename ScalarT>
92 const ScalarT& v_a,
const ScalarT& v_b,
93 ScalarT& r, ScalarT& drda, ScalarT& drdb,
94 ScalarT& z_a, ScalarT& z_b) {
101 FadType a_fad(1, a); a_fad.fastAccessDx(0) = v_a;
102 FadType b_fad(1, b); b_fad.fastAccessDx(0) = v_b;
105 FadType r_fad, drda_fad, drdb_fad;
109 drda = drda_fad.val();
110 drdb = drdb_fad.val();
111 z_a = drda_fad.dx(0);
112 z_b = drdb_fad.dx(0);
115 int main(
int argc,
char **argv)
129 double r_ad, drda_ad, drdb_ad, z_a_ad, z_b_ad;
130 func_and_deriv2(a, b, c, v_a, v_b, r_ad, drda_ad, drdb_ad, z_a_ad, z_b_ad);
133 double r =
func(a, b, c);
136 double drda, drdb, d2rda2, d2rdb2, d2rdadb;
138 double z_a = d2rda2*v_a + d2rdadb*v_b;
139 double z_b = d2rdadb*v_a + d2rdb2*v_b;
144 std::cout.setf(std::ios::scientific);
145 std::cout.precision(p);
146 std::cout <<
" r = " << std::setw(w) << r <<
" (original) == "
147 << std::setw(w) << r_ad <<
" (AD) Error = " << std::setw(w)
148 << r - r_ad << std::endl
149 <<
"dr/da = " << std::setw(w) << drda <<
" (analytic) == "
150 << std::setw(w) << drda_ad <<
" (AD) Error = " << std::setw(w)
151 << drda - drda_ad << std::endl
152 <<
"dr/db = " << std::setw(w) << drdb <<
" (analytic) == "
153 << std::setw(w) << drdb_ad <<
" (AD) Error = " << std::setw(w)
154 << drdb - drdb_ad << std::endl
155 <<
"z_a = " << std::setw(w) << z_a <<
" (analytic) == "
156 << std::setw(w) << z_a_ad <<
" (AD) Error = " << std::setw(w)
157 << z_a - z_a_ad << std::endl
158 <<
"z_b = " << std::setw(w) << z_b <<
" (analytic) == "
159 << std::setw(w) << z_b_ad <<
" (AD) Error = " << std::setw(w)
160 << z_b - z_b_ad << std::endl;
162 double tol = 1.0e-14;
168 std::cout <<
"\nExample passed!" << std::endl;
172 std::cout <<
"\nSomething is wrong, example failed!" << std::endl;
Sacado::Fad::DFad< double > FadType
KOKKOS_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
void func_and_deriv2(const ScalarT &a, const ScalarT &b, const ScalarT &c, const ScalarT &v_a, const ScalarT &v_b, ScalarT &r, ScalarT &drda, ScalarT &drdb, ScalarT &z_a, ScalarT &z_b)
void analytic_deriv(double a, double b, double c, double &drda, double &drdb, double &d2rda2, double &d2rdb2, double &d2rdadb)
const T func(int n, T *x)
void func_and_deriv(const ScalarT &a, const ScalarT &b, const ScalarT &c, ScalarT &r, ScalarT &drda, ScalarT &drdb)