#include <codi.hpp>
#include <iostream>
template<typename Type>
Type func(const Type& x) {
return x * x;
}
template<typename Type>
using VectorWrapper = typename Factory::RType;
VectorWrapper* vaType = Factory::create(va);
using TypeIdentifier = typename VectorWrapper::Identifier;
using TypeReal = typename VectorWrapper::Real;
TypeReal x_v = data->
getData<TypeReal>();
TypeIdentifier x_i = data->
getData<TypeIdentifier>();
TypeIdentifier w_i = data->
getData<TypeIdentifier>();
TypeReal w_b = vaType->getAdjoint(w_i, 0);
TypeReal t_b = 2.0 * codi::ComputationTraits::transpose(x_v) * w_b;
vaType->updateAdjoint(x_i, 0, t_b);
vaType->resetAdjoint(w_i, 0);
Factory::destroy(vaType);
}
void extFunc_del(Tape* t, void* d) {
delete data;
std::cout << " Reset: data is deleted." << std::endl;
}
template<typename Type>
Type addExternalFunc(Type const& x) {
tape.setPassive();
Type w = func(x);
tape.setActive();
return w;
}
int main(int nargs, char** args) {
tape.setActive();
tape.registerInput(x);
Real t1 = addExternalFunc(x);
std::complex<Real> c(t1, -t1);
std::complex<Real> t2 = addExternalFunc(c);
tape.registerOutput(y);
tape.setPassive();
tape.evaluate();
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
std::cout <<
"dy/dx = " << x.
getGradient() << std::endl;
tape.reset();
return 0;
}
DataExtraction< Type >::Identifier registerExternalFunctionOutput(Type &v)
Register all active types of a aggregated type as external function outputs.
Definition: realTraits.hpp:240
DataExtraction< Type >::Identifier getIdentifier(Type const &v)
Extract the identifiers from a type of aggregated active types.
Definition: realTraits.hpp:216
DataExtraction< Type >::Real getValue(Type const &v)
Extract the primal values from a type of aggregated active types.
Definition: realTraits.hpp:210
RealReverseGen< double > RealReverse
Definition: codi.hpp:120
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
typename Tape::Identifier Identifier
See LhsExpressionInterface.
Definition: activeTypeBase.hpp:78
static Tape & getTape()
Get a reference to the tape which manages this expression.
Definition: activeType.hpp:99
T_Tape Tape
See ActiveType.
Definition: activeType.hpp:55
typename Tape::Real Real
See LhsExpressionInterface.
Definition: activeTypeBase.hpp:76
Factory for the creation of AggregatedTypeVectorAccessWrapper instances.
Definition: aggregatedTypeVectorAccessWrapper.hpp:217
Ease of access structure for user-provided data on the tape for external functions....
Definition: externalFunctionUserData.hpp:59
void getData(Type &value)
Get a copy of the next data item.
Definition: externalFunctionUserData.hpp:171
size_t addData(Type const &value)
Definition: externalFunctionUserData.hpp:151
User-defined evaluation functions for the taping process.
Definition: externalFunction.hpp:102
void setGradient(Gradient const &g)
Set the gradient of this lvalue in the tape.
Definition: lhsExpressionInterface.hpp:120
Gradient getGradient() const
Get the gradient of this lvalue from the tape.
Definition: lhsExpressionInterface.hpp:115
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition: vectorAccessInterface.hpp:91