本文整理汇总了C++中AD::make_variable方法的典型用法代码示例。如果您正苦于以下问题:C++ AD::make_variable方法的具体用法?C++ AD::make_variable怎么用?C++ AD::make_variable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AD
的用法示例。
在下文中一共展示了AD::make_variable方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
AD<Base> operator / (const AD<Base> &left , const AD<Base> &right)
{
// compute the Base part
AD<Base> result;
result.value_ = left.value_ / right.value_;
CPPAD_ASSERT_UNKNOWN( Parameter(result) );
// check if there is a recording in progress
ADTape<Base>* tape = AD<Base>::tape_ptr();
if( tape == CPPAD_NULL )
return result;
tape_id_t tape_id = tape->id_;
// tape_id cannot match the default value for tape_id_; i.e., 0
CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
bool var_left = left.tape_id_ == tape_id;
bool var_right = right.tape_id_ == tape_id;
if( var_left )
{ if( var_right )
{ // result = variable / variable
CPPAD_ASSERT_KNOWN(
left.tape_id_ == right.tape_id_,
"Dividing AD objects that are"
" variables on different tapes."
);
CPPAD_ASSERT_UNKNOWN( NumRes(DivvvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(DivvvOp) == 2 );
// put operand addresses in tape
tape->Rec_.PutArg(left.taddr_, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(DivvvOp);
// make result a variable
result.tape_id_ = tape_id;
}
else if( IdenticalOne(right.value_) )
{ // result = variable / 1
result.make_variable(left.tape_id_, left.taddr_);
}
else
{ // result = variable / parameter
CPPAD_ASSERT_UNKNOWN( NumRes(DivvpOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(DivvpOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(right.value_);
tape->Rec_.PutArg(left.taddr_, p);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(DivvpOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
else if( var_right )
{ if( IdenticalZero(left.value_) )
{ // result = 0 / variable
}
else
{ // result = parameter / variable
CPPAD_ASSERT_UNKNOWN( NumRes(DivpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(DivpvOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(left.value_);
tape->Rec_.PutArg(p, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(DivpvOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
return result;
}
示例2: if
AD<Base> operator - (const AD<Base> &left , const AD<Base> &right)
{ ADTape<Base> *tape = AD<Base>::tape_ptr();
bool var_left, var_right;
# ifdef NDEBUG
if( tape == CPPAD_NULL )
{ var_left = false;
var_right = false;
}
else
{
var_left = left.id_ == tape->id_;
var_right = right.id_ == tape->id_;
}
# else
var_left = Variable(left);
var_right = Variable(right);
CPPAD_ASSERT_KNOWN(
(! var_left) || left.id_ == tape->id_ ,
"- left operand is a variable for a different thread"
);
CPPAD_ASSERT_KNOWN(
(! var_right) || right.id_ == tape->id_ ,
"- right operand is a variable for a different thread"
);
# endif
AD<Base> result;
result.value_ = left.value_ - right.value_;
CPPAD_ASSERT_UNKNOWN( Parameter(result) );
if( var_left )
{ if( var_right )
{ // result = variable - variable
CPPAD_ASSERT_UNKNOWN( NumVar(SubvvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumInd(SubvvOp) == 2 );
// put operand addresses in tape
tape->Rec_.PutInd(left.taddr_, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(SubvvOp);
// make result a variable
result.id_ = tape->id_;
}
else if( IdenticalZero(right.value_) )
{ // result = variable - 0
result.make_variable(left.id_, left.taddr_);
}
else
{ // result = variable - parameter
CPPAD_ASSERT_UNKNOWN( NumVar(SubvpOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumInd(SubvpOp) == 2 );
// put operand addresses in tape
size_t p = tape->Rec_.PutPar(right.value_);
tape->Rec_.PutInd(left.taddr_, p);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(SubvpOp);
// make result a variable
result.id_ = tape->id_;
}
}
else if( var_right )
{ // result = parameter - variable
CPPAD_ASSERT_UNKNOWN( NumVar(SubpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumInd(SubpvOp) == 2 );
// put operand addresses in tape
size_t p = tape->Rec_.PutPar(left.value_);
tape->Rec_.PutInd(p, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(SubpvOp);
// make result a variable
result.id_ = tape->id_;
}
return result;
}
示例3: if
// case where x and y are AD<Base> -------------------------------------------
template <class Base> AD<Base>
azmul(const AD<Base>& x, const AD<Base>& y)
{
// compute the Base part
AD<Base> result;
result.value_ = azmul(x.value_, y.value_);
// check if there is a recording in progress
ADTape<Base>* tape = AD<Base>::tape_ptr();
if( tape == CPPAD_NULL )
return result;
tape_id_t tape_id = tape->id_;
// tape_id cannot match the default value for tape_id_; i.e., 0
CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
bool var_x = x.tape_id_ == tape_id;
bool var_y = y.tape_id_ == tape_id;
if( var_x )
{ if( var_y )
{ // result = azmul(variable, variable)
CPPAD_ASSERT_UNKNOWN( NumRes(ZmulvvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(ZmulvvOp) == 2 );
// put operand addresses in tape
tape->Rec_.PutArg(x.taddr_, y.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(ZmulvvOp);
// make result a variable
result.tape_id_ = tape_id;
}
else if( IdenticalZero( y.value_ ) )
{ // result = variable * 0
}
else if( IdenticalOne( y.value_ ) )
{ // result = variable * 1
result.make_variable(x.tape_id_, x.taddr_);
}
else
{ // result = zmul(variable, parameter)
CPPAD_ASSERT_UNKNOWN( NumRes(ZmulvpOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(ZmulvpOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(y.value_);
tape->Rec_.PutArg(x.taddr_, p);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(ZmulvpOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
else if( var_y )
{ if( IdenticalZero(x.value_) )
{ // result = 0 * variable
}
else if( IdenticalOne( x.value_ ) )
{ // result = 1 * variable
result.make_variable(y.tape_id_, y.taddr_);
}
else
{ // result = zmul(parameter, variable)
CPPAD_ASSERT_UNKNOWN( NumRes(ZmulpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(ZmulpvOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(x.value_);
tape->Rec_.PutArg(p, y.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(ZmulpvOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
return result;
}
示例4: NumRes
void local::ADTape<Base>::RecordCondExp(
enum CompareOp cop ,
AD<Base> &returnValue ,
const AD<Base> &left ,
const AD<Base> &right ,
const AD<Base> &if_true ,
const AD<Base> &if_false )
{ size_t ind0, ind1, ind2, ind3, ind4, ind5;
size_t returnValue_taddr;
// taddr_ of this variable
CPPAD_ASSERT_UNKNOWN( NumRes(CExpOp) == 1 );
returnValue_taddr = Rec_.PutOp(CExpOp);
// ind[0] = cop
ind0 = addr_t( cop );
// ind[1] = base 2 represenation of the value
// [Var(left), Var(right), Var(if_true), Var(if_false)]
ind1 = 0;
// Make sure returnValue is in the list of variables and set its taddr
if( Parameter(returnValue) )
returnValue.make_variable(id_, returnValue_taddr );
else returnValue.taddr_ = returnValue_taddr;
// ind[2] = left address
if( Parameter(left) )
ind2 = Rec_.PutPar(left.value_);
else
{ ind1 += 1;
ind2 = left.taddr_;
}
// ind[3] = right address
if( Parameter(right) )
ind3 = Rec_.PutPar(right.value_);
else
{ ind1 += 2;
ind3 = right.taddr_;
}
// ind[4] = if_true address
if( Parameter(if_true) )
ind4 = Rec_.PutPar(if_true.value_);
else
{ ind1 += 4;
ind4 = if_true.taddr_;
}
// ind[5] = if_false address
if( Parameter(if_false) )
ind5 = Rec_.PutPar(if_false.value_);
else
{ ind1 += 8;
ind5 = if_false.taddr_;
}
CPPAD_ASSERT_UNKNOWN( NumArg(CExpOp) == 6 );
CPPAD_ASSERT_UNKNOWN( ind1 > 0 );
Rec_.PutArg(ind0, ind1, ind2, ind3, ind4, ind5);
// check that returnValue is a dependent variable
CPPAD_ASSERT_UNKNOWN( Variable(returnValue) );
}
示例5: if
AD<Base> operator / (const AD<Base> &left , const AD<Base> &right)
{
// compute the Base part
AD<Base> result;
result.value_ = left.value_ / right.value_;
CPPAD_ASSERT_UNKNOWN( Parameter(result) );
// check if there is a recording in progress
local::ADTape<Base>* tape = AD<Base>::tape_ptr();
if( tape == CPPAD_NULL )
return result;
tape_id_t tape_id = tape->id_;
// tape_id cannot match the default value for tape_id_; i.e., 0
CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
// check if left and right tapes match
bool match_left = left.tape_id_ == tape_id;
bool match_right = right.tape_id_ == tape_id;
// check if left and right are dynamic parameters
bool dyn_left = match_left & (left.ad_type_ == dynamic_enum);
bool dyn_right = match_right & (right.ad_type_ == dynamic_enum);
// check if left and right are variables
bool var_left = match_left & (left.ad_type_ != dynamic_enum);
bool var_right = match_right & (right.ad_type_ != dynamic_enum);
CPPAD_ASSERT_KNOWN(
left.tape_id_ == right.tape_id_ || ! match_left || ! match_right ,
"Divide: AD variables or dynamic parameters on different threads."
);
if( var_left )
{ if( var_right )
{ // result = variable / variable
CPPAD_ASSERT_UNKNOWN( local::NumRes(local::DivvvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( local::NumArg(local::DivvvOp) == 2 );
// put operand addresses in tape
tape->Rec_.PutArg(left.taddr_, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(local::DivvvOp);
// make result a variable
result.tape_id_ = tape_id;
result.ad_type_ = variable_enum;
}
else if( (! dyn_right) & IdenticalOne(right.value_) )
{ // result = variable / 1
result.make_variable(left.tape_id_, left.taddr_);
}
else
{ // result = variable / parameter
CPPAD_ASSERT_UNKNOWN( local::NumRes(local::DivvpOp) == 1 );
CPPAD_ASSERT_UNKNOWN( local::NumArg(local::DivvpOp) == 2 );
// put operand addresses in tape
addr_t p = right.taddr_;
if( ! dyn_right )
p = tape->Rec_.put_con_par(right.value_);
tape->Rec_.PutArg(left.taddr_, p);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(local::DivvpOp);
// make result a variable
result.tape_id_ = tape_id;
result.ad_type_ = variable_enum;
}
}
else if( var_right )
{ if( (! dyn_left) & IdenticalZero(left.value_) )
{ // result = 0 / variable
}
else
{ // result = parameter / variable
CPPAD_ASSERT_UNKNOWN( local::NumRes(local::DivpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( local::NumArg(local::DivpvOp) == 2 );
// put operand addresses in tape
addr_t p = left.taddr_;
if( ! dyn_left )
p = tape->Rec_.put_con_par(left.value_);
tape->Rec_.PutArg(p, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(local::DivpvOp);
// make result a variable
result.tape_id_ = tape_id;
result.ad_type_ = variable_enum;
}
}
else if( dyn_left | dyn_right )
{ addr_t arg0 = left.taddr_;
addr_t arg1 = right.taddr_;
if( ! dyn_left )
arg0 = tape->Rec_.put_con_par(left.value_);
if( ! dyn_right )
arg1 = tape->Rec_.put_con_par(right.value_);
//
// parameters with a dynamic parameter result
result.taddr_ = tape->Rec_.put_dyn_par(
result.value_, local::div_dyn, arg0, arg1
);
result.tape_id_ = tape_id;
//.........这里部分代码省略.........
示例6: if
AD<Base> operator + (const AD<Base> &left , const AD<Base> &right)
{
// compute the Base part of this AD object
AD<Base> result;
result.value_ = left.value_ + right.value_;
CPPAD_ASSERT_UNKNOWN( Parameter(result) );
// check if there is a recording in progress
ADTape<Base>* tape = AD<Base>::tape_ptr();
if( tape == CPPAD_NULL )
return result;
tape_id_t tape_id = tape->id_;
// tape_id cannot match the default value for tape_id_; i.e., 0
CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
bool var_left = left.tape_id_ == tape_id;
bool var_right = right.tape_id_ == tape_id;
if( var_left )
{ if( var_right )
{ // result = variable + variable
CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
// put operand addresses in tape
tape->Rec_.PutArg(left.taddr_, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(AddvvOp);
// make result a variable
result.tape_id_ = tape_id;
}
else if( IdenticalZero(right.value_) )
{ // result = variable + 0
result.make_variable(left.tape_id_, left.taddr_);
}
else
{ // result = variable + parameter
// = parameter + variable
CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(right.value_);
tape->Rec_.PutArg(p, left.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(AddpvOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
else if( var_right )
{ if( IdenticalZero(left.value_) )
{ // result = 0 + variable
result.make_variable(right.tape_id_, right.taddr_);
}
else
{ // result = parameter + variable
CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 );
CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 );
// put operand addresses in tape
addr_t p = tape->Rec_.PutPar(left.value_);
tape->Rec_.PutArg(p, right.taddr_);
// put operator in the tape
result.taddr_ = tape->Rec_.PutOp(AddpvOp);
// make result a variable
result.tape_id_ = tape_id;
}
}
return result;
}