CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mul.hpp
Go to the documentation of this file.
1 // $Id$
2 # ifndef CPPAD_CORE_MUL_HPP
3 # define CPPAD_CORE_MUL_HPP
4 
5 /* --------------------------------------------------------------------------
6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
7 
8 CppAD is distributed under multiple licenses. This distribution is under
9 the terms of the
10  Eclipse Public License Version 1.0.
11 
12 A copy of this license is included in the COPYING file of this distribution.
13 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14 -------------------------------------------------------------------------- */
15 
16 // BEGIN CppAD namespace
17 namespace CppAD {
18 
19 template <class Base>
20 AD<Base> operator * (const AD<Base> &left , const AD<Base> &right)
21 {
22  // compute the Base part
23  AD<Base> result;
24  result.value_ = left.value_ * right.value_;
26 
27  // check if there is a recording in progress
29  if( tape == CPPAD_NULL )
30  return result;
31  tape_id_t tape_id = tape->id_;
32 
33  // tape_id cannot match the default value for tape_id_; i.e., 0
34  CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
35  bool var_left = left.tape_id_ == tape_id;
36  bool var_right = right.tape_id_ == tape_id;
37 
38  if( var_left )
39  { if( var_right )
40  { // result = variable * variable
43 
44  // put operand addresses in tape
45  tape->Rec_.PutArg(left.taddr_, right.taddr_);
46  // put operator in the tape
47  result.taddr_ = tape->Rec_.PutOp(local::MulvvOp);
48  // make result a variable
49  result.tape_id_ = tape_id;
50  }
51  else if( IdenticalZero(right.value_) )
52  { // result = variable * 0
53  }
54  else if( IdenticalOne(right.value_) )
55  { // result = variable * 1
56  result.make_variable(left.tape_id_, left.taddr_);
57  }
58  else
59  { // result = variable * parameter
62 
63  // put operand addresses in tape
64  addr_t p = tape->Rec_.PutPar(right.value_);
65  tape->Rec_.PutArg(p, left.taddr_);
66  // put operator in the tape
67  result.taddr_ = tape->Rec_.PutOp(local::MulpvOp);
68  // make result a variable
69  result.tape_id_ = tape_id;
70  }
71  }
72  else if( var_right )
73  { if( IdenticalZero(left.value_) )
74  { // result = 0 * variable
75  }
76  else if( IdenticalOne(left.value_) )
77  { // result = 1 * variable
78  result.make_variable(right.tape_id_, right.taddr_);
79  }
80  else
81  { // result = parameter * variable
84 
85  // put operand addresses in tape
86  addr_t p = tape->Rec_.PutPar(left.value_);
87  tape->Rec_.PutArg(p, right.taddr_);
88  // put operator in the tape
89  result.taddr_ = tape->Rec_.PutOp(local::MulpvOp);
90  // make result a variable
91  result.tape_id_ = tape_id;
92  }
93  }
94  return result;
95 }
96 
97 // convert other cases into the case above
99 
100 } // END CppAD namespace
101 
102 # endif
Base value_
Definition: ad.hpp:38
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
Definition: ad.hpp:34
#define CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(Op)
Declares automatic coercion for certain binary operations with AD result.
Definition: define.hpp:124
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
bool IdenticalZero(const std::complex< double > &x)
AD< Base > operator*(const AD< Base > &left, const AD< Base > &right)
Definition: mul.hpp:20
static local::ADTape< Base > * tape_ptr(void)
Pointer for the tape for this AD&lt;Base&gt; class and the current thread.
Definition: tape_link.hpp:130
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
bool IdenticalOne(const std::complex< double > &x)
local::recorder< Base > Rec_
This is where the information is recorded.
Definition: ad_tape.hpp:106
tape_id_t tape_id_
Definition: ad.hpp:41
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION bool Parameter(const AD< Base > &x)
Definition: par_var.hpp:80
addr_t taddr_
Definition: ad.hpp:44
void make_variable(tape_id_t id, addr_t taddr)
Definition: ad.hpp:271
tape_id_t id_
Unique identifier for this tape.
Definition: ad_tape.hpp:101
CPPAD_TAPE_ID_TYPE tape_id_t
Definition: declare_ad.hpp:45