Skip to content

Commit

Permalink
master: valvector: ad_join: add reverse mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradbell committed Mar 1, 2024
1 parent c065622 commit 76b7723
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions example/valvector/ad_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ bool ad_join(void)
for(size_t j = 0; j < n; ++j)
ok &= z[0][j] == x[j][0] * x[j][0];
//
// dw
CPPAD_TESTVECTOR( valvector ) w(m), dw(n);
w[0][0] = 1.0;
dw = f.Reverse(1, w);
//
// ok
for(size_t j = 0; j < n; ++j)
ok &= dw[0][j] == scalar_type(2) * x[0][j];
return ok;
}
// END C++
35 changes: 35 additions & 0 deletions include/cppad/example/valvector/ad_join.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ class valvector_ad_join_atom : public CppAD::atomic_four<valvector> {
//
return ok;
}
// ------------------------------------------------------------------------
// reverse
bool reverse(
size_t call_id ,
const CppAD::vector<bool>& select_x ,
size_t order_up ,
const CppAD::vector<valvector>& taylor_x ,
const CppAD::vector<valvector>& taylor_y ,
CppAD::vector<valvector>& partial_x ,
const CppAD::vector<valvector>& partial_y ) override
{ //
// ok
bool ok = true;
//
// q, m
size_t q = order_up + 1;
size_t n = taylor_x.size() / q;
//
# ifndef NDEBUG
size_t m = taylor_y.size() / q;
assert( call_id == 0 );
assert( m == 1 );
assert( n == select_x.size() );
# endif
//
// partial_x
for(size_t k = 0; k < q; ++k)
{ assert( taylor_y[k].size() == n );
for(size_t j = 0; j < n; ++j)
{ partial_x[j * q + k].resize(1);
partial_x[j * q + k][0] = partial_y[k][j];
}
}
return ok;
}
};

class valvector_ad_join {
Expand Down

0 comments on commit 76b7723

Please sign in to comment.