ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
advector.h
Go to the documentation of this file.
1/* ---------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3
4 Revision: $Id$
5 Contents: advector.h contains a vector<adouble> implementation
6 that is able to trace subscripting operations.
7
8 Copyright (c) Kshitij Kulshreshtha
9
10 This file is part of ADOL-C. This software is provided as open source.
11 Any use, reproduction, or distribution of the software constitutes
12 recipient's acceptance of the terms of the accompanying license file.
13
14---------------------------------------------------------------------------*/
15
16#ifndef ADOLC_ADVECTOR_H
17#define ADOLC_ADVECTOR_H
18
19#include <adolc/adolcexport.h>
20#include <adolc/adtb_types.h>
22#include <memory>
23#include <vector>
24
25/****************************************************************************/
26/* THIS IS ONLY FOR TAPED VERSION
27 */
28#if !defined(TAPELESS)
29
30class advector;
31class adubref;
32
40 /* This class is supposed to be used only when an advector subscript
41 * occurs as an lvalue somewhere. What we need to do is read the location
42 * of the referenced adouble out of store[location] and perform
43 * operations with this refloc. This means that the tape needs new
44 * opcodes (ref_assign_* /ref_eq_* / ref_{incr,decr}_a) for each of
45 * these operations, most of the code will simply be copied from
46 * adouble class, since the operation is really the same except for
47 * the part where the refloc is read from store[location].
48 * Reverse mode is also straightforward the same way.
49 *
50 * Convert to a new adub as soon as used as rvalue, this is why adubref
51 * is not a child of badouble, since it should never occur as rvalue.
52 */
53
54 size_t loc_;
55 size_t refloc_;
56
57public:
58 explicit adubref(size_t lo, size_t ref);
59 adubref(void) = delete;
60 adubref(double) = delete;
61 adubref(const adubref &) = delete;
62 adubref(adubref &&) = delete;
63 adubref(const adouble &) = delete;
64 adubref(adouble &&) = delete;
66
67 adubref &operator=(const double coval);
69 adubref &operator=(const pdouble &p) { return *this = adouble(p); }
70
71 size_t loc() const { return loc_; }
72 size_t refloc() const { return refloc_; }
73 double refloc_value() const { return currentTape().get_ad_value(refloc_); }
74 double loc_value() const { return currentTape().get_ad_value(loc_); }
75 void refloc_value(const double coval) {
76 currentTape().set_ad_value(refloc_, coval);
77 }
78 operator adouble() const;
79
82
85
86 adubref &operator+=(const double coval);
88 inline adubref &operator+=(const pdouble &p) { return *this += adouble(p); }
89
90 adubref &operator-=(const double coval);
92 inline adubref &operator-=(const pdouble &p) { return *this -= adouble(p); }
93
94 adubref &operator*=(const double coval);
96 inline adubref &operator*=(const pdouble &p) { return *this *= adouble(p); }
97
98 inline adubref &operator/=(const double coval) {
99 return *this *= (1.0 / coval);
100 }
101 inline adubref &operator/=(const adouble &a) { return *this *= (1.0 / a); }
102 inline adubref &operator/=(const pdouble &p) { return *this /= adouble(p); }
103
104 adubref &operator<<=(const double coval);
106 adubref &operator>>=(double &coval);
108};
109
110void ADOLC_API condassign(adubref &res, const adouble &cond,
111 const adouble &arg1, const adouble &arg2);
112void ADOLC_API condassign(adubref &res, const adouble &cond,
113 const adouble &arg);
114
115void ADOLC_API condeqassign(adubref &res, const adouble &cond,
116 const adouble &arg1, const adouble &arg2);
117void ADOLC_API condeqassign(adubref &res, const adouble &cond,
118 const adouble &arg);
119
127 std::vector<adouble> data_;
128
129public:
130 advector() = default;
131 explicit advector(size_t n)
132 : data_(currentTape().ensureContiguousLocations_(n)) {}
133
134 advector(const advector &ad_vec)
135 : data_(currentTape().ensureContiguousLocations_(ad_vec.size())) {
136 adolc_vec_copy(data_.data(), ad_vec.data_.data(), ad_vec.size());
137 }
138
139 // given adouble vector might not have contiguous locations
140 advector(const std::vector<adouble> &v) : data_(v) {};
141
143
144 operator const std::vector<adouble> &() const { return data_; }
145 operator std::vector<adouble> &() { return data_; }
146 operator adouble *() { return data_.data(); }
147 adouble operator[](const adouble &index) const;
149 adouble &operator[](size_t i) { return data_[i]; }
150 const adouble &operator[](size_t i) const { return data_[i]; }
151 adouble lookupindex(const adouble &a, const adouble &b) const;
152
153 size_t size() const { return data_.size(); }
154 bool nondecreasing() const;
155};
156
157#endif /* TAPELESS */
158#endif // ADOLC_ADVECTOR_H
#define ADOLC_API
Definition adolcexport.h:16
void ADOLC_API adolc_vec_copy(adouble *const dest, const adouble *const src, size_t size)
void ADOLC_API condassign(adubref &res, const adouble &cond, const adouble &arg1, const adouble &arg2)
void ADOLC_API condeqassign(adubref &res, const adouble &cond, const adouble &arg1, const adouble &arg2)
double get_ad_value(size_t loc) const
Definition valuetape.h:534
void set_ad_value(size_t loc, double coval)
Definition valuetape.h:535
Taped active scalar for reusable forward and reverse differentiation.
Definition adtb_types.h:211
Proxy for an active vector element selected by an active index.
Definition advector.h:39
void refloc_value(const double coval)
Definition advector.h:75
adubref & operator/=(const pdouble &p)
Definition advector.h:102
size_t loc() const
Definition advector.h:71
size_t refloc() const
Definition advector.h:72
adubref(void)=delete
adubref & operator-=(const pdouble &p)
Definition advector.h:92
adouble operator--(int)
void declareIndependent()
adubref(double)=delete
adubref & operator=(const pdouble &p)
Definition advector.h:69
void declareDependent()
adubref(const adubref &)=delete
adubref(size_t lo, size_t ref)
adubref & operator=(const adouble &a)
adubref & operator<<=(const double coval)
adubref & operator/=(const double coval)
Definition advector.h:98
adubref & operator+=(const double coval)
adubref & operator*=(const pdouble &p)
Definition advector.h:96
adubref(adubref &&)=delete
adubref & operator+=(const adouble &a)
double loc_value() const
Definition advector.h:74
adubref & operator--()
adubref(const adouble &)=delete
adubref & operator*=(const double coval)
adubref(adouble &&)=delete
adubref & operator++()
adouble operator++(int)
adubref & operator-=(const double coval)
adubref & operator*=(const adouble &a)
double refloc_value() const
Definition advector.h:73
adubref & operator>>=(double &coval)
adubref & operator+=(const pdouble &p)
Definition advector.h:88
adubref & operator-=(const adouble &a)
adubref & operator=(const double coval)
adubref & operator/=(const adouble &a)
Definition advector.h:101
Contiguous vector of taped active values with active indexing.
Definition advector.h:126
const adouble & operator[](size_t i) const
Definition advector.h:150
advector(const advector &ad_vec)
Definition advector.h:134
advector(const std::vector< adouble > &v)
Definition advector.h:140
size_t size() const
Definition advector.h:153
advector()=default
adubref operator[](const adouble &index)
adouble operator[](const adouble &index) const
bool nondecreasing() const
adouble & operator[](size_t i)
Definition advector.h:149
~advector()
Definition advector.h:142
advector(size_t n)
Definition advector.h:131
adouble lookupindex(const adouble &a, const adouble &b) const
Mutable passive parameter stored on a recorded tape.
Definition adtb_types.h:431
ValueTape & currentTape()
Returns a reference to the current ValueTape.
Definition taperegistry.h:113