source: trunk/ADOL-C/include/adolc/advector.h @ 676

Last change on this file since 676 was 676, checked in by kulshres, 3 years ago

add a condeqassign to test for a >= 0

since condassign only tests for a > 0, there was no equality test

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* ---------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3
4 Revision: $Id: advector.h 676 2016-03-17 14:29:13Z kulshres $
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#if !defined(ADOLC_ADVECTOR_H)
17#define ADOLC_ADVECTOR_H 1
18
19/****************************************************************************/
20/*                                                         THIS FILE IS C++ */
21#ifdef __cplusplus
22#include <vector>
23
24/****************************************************************************/
25/*                                           THIS IS ONLY FOR TAPED VERSION */
26#if !defined(TAPELESS)
27
28class advector;
29class adubref;
30
31class ADOLC_DLL_EXPORT adubref {
32    /* This class is supposed to be used only when an advector subscript
33     * occurs as an lvalue somewhere. What we need to do is read the location
34     * of the referenced adouble out of store[location] and perform
35     * operations with this refloc. This means that the tape needs new
36     * opcodes (ref_assign_* /ref_eq_* / ref_{incr,decr}_a) for each of
37     * these operations, most of the code  will simply be copied from
38     * adouble class, since the operation is really the same except for
39     * the part where the refloc is read from store[location].
40     * Reverse mode is also straightforward the same way.
41     *
42     * Convert to a new adub as soon as used as rvalue, this is why adubref
43     * is not a child of badouble, since it should never occur as rvalue.
44     */
45    friend ADOLC_DLL_EXPORT class adub;
46    friend ADOLC_DLL_EXPORT class advector;
47    friend ADOLC_DLL_EXPORT class pdouble;
48protected:
49    locint location;
50    locint refloc;
51    explicit adubref( locint lo, locint ref );
52    explicit adubref( void ) {
53        fprintf(DIAG_OUT,"ADOL-C error: illegal default construction of adubref"
54                " variable\n");
55        exit(-2);
56    }
57    explicit adubref( double ) {
58        fprintf(DIAG_OUT,"ADOL-C error: illegal  construction of adubref"
59                " variable from double\n");
60        exit(-2);
61    }
62    explicit adubref( const badouble& ) {
63        fprintf(DIAG_OUT,"ADOL-C error: illegal  construction of adubref"
64                " variable from badouble\n");
65        exit(-2);
66    }
67    explicit adubref( const adub& ) {
68        fprintf(DIAG_OUT,"ADOL-C error: illegal  construction of adubref"
69                " variable from adub\n");
70        exit(-2);
71    }
72    adubref( const adubref& ) {
73        fprintf(DIAG_OUT,"ADOL-C error: illegal copy construction of adubref"
74               " variable\n");
75        exit(-2);
76    }
77    bool isInit;  // marker if the badouble is properly initialized
78public:
79    /* adub prevents postfix operators to occur on the left
80       side of an assignment which would not work  */
81    adub operator++( int );
82    adub operator--( int );
83    adubref& operator++( void );
84    adubref& operator--( void );
85    adubref& operator = ( double );
86    adubref& operator = ( const badouble& );
87    adubref& operator = ( const adubref& );
88    adubref& operator = ( const pdouble& );
89    adubref& operator +=  ( double );
90    adubref& operator +=  ( const badouble& );
91    adubref& operator +=  ( const pdouble& );
92    adubref& operator -=  ( double x );
93    adubref& operator -=  ( const badouble& );
94    adubref& operator -=  ( const pdouble& );
95    adubref& operator *=  ( double x );
96    adubref& operator *=  ( const badouble& );
97    adubref& operator *=  ( const pdouble& );
98    inline adubref& operator /=  ( double x );
99    inline adubref& operator /=  ( const badouble& );
100    inline adubref& operator /=  ( const pdouble&);
101
102    adubref& operator <<= ( double );
103    void declareIndependent();
104    adubref& operator >>= ( double& );
105    void declareDependent();
106    operator adub() const;
107    friend ADOLC_DLL_EXPORT void condassign(adubref&, const badouble&, const badouble&, const badouble&);
108    friend ADOLC_DLL_EXPORT void condassign(adubref&, const badouble&, const badouble&);
109    friend ADOLC_DLL_EXPORT void condeqassign(adubref&, const badouble&, const badouble&, const badouble&);
110    friend ADOLC_DLL_EXPORT void condeqassign(adubref&, const badouble&, const badouble&);
111    ~adubref();
112};
113
114/* adolc_vec_copy(dest,src,size); */
115void ADOLC_DLL_EXPORT adolc_vec_copy(adouble *const, const adouble*const, locint);
116/* adolc_vec_axpy(res,a,x,y,size); <=> res = a*x + y  */
117void ADOLC_DLL_EXPORT adolc_vec_axpy(adouble *const, const badouble&, const adouble*const, const adouble*const, locint);
118
119class advector {
120private:
121    struct ADOLC_DLL_EXPORT blocker {
122        blocker() {}
123        blocker(size_t n);
124        ~blocker() {}
125    } blk;
126    std::vector<adouble> data;
127    ADOLC_DLL_EXPORT bool nondecreasing() const;
128public:
129    ADOLC_DLL_EXPORT advector() : blk(), data() {}
130    ADOLC_DLL_EXPORT explicit advector(size_t n) : blk(n), data(n) {}
131    ADOLC_DLL_EXPORT ~advector() {}
132    ADOLC_DLL_EXPORT advector(const advector& x) : blk(x.size()), data(x.size()) {  adolc_vec_copy(data.data(),x.data.data(),x.size()); }
133    // in the above copy we are sure of contiguous locations
134    // but not so in the one below
135    ADOLC_DLL_EXPORT advector(const std::vector<adouble>& v) : blk(v.size()), data(v) {}
136    ADOLC_DLL_EXPORT size_t size() const { return data.size(); }
137    ADOLC_DLL_EXPORT operator const std::vector<adouble>&() const { return data; }
138    ADOLC_DLL_EXPORT operator std::vector<adouble>&() { return data; }
139    ADOLC_DLL_EXPORT operator adouble*() { return data.data(); }
140    ADOLC_DLL_EXPORT adub operator[](const badouble& index) const;
141    ADOLC_DLL_EXPORT adubref operator[](const badouble& index);
142    ADOLC_DLL_EXPORT adouble& operator[](size_t i) { return data[i]; }
143    ADOLC_DLL_EXPORT const adouble& operator[](size_t i) const { return data[i]; }
144    ADOLC_DLL_EXPORT adouble lookupindex(const badouble& x, const badouble& y) const;
145};
146
147inline adubref& adubref::operator /= (double y) {
148    *this *=  (1.0/y);
149    return *this;
150}
151
152
153inline adubref& adubref::operator /= (const badouble& y) {
154    *this *=  (1.0/y);
155    return *this;
156}
157
158inline adubref& adubref::operator /= (const pdouble& p) {
159    *this *= recipr(p); 
160    return *this;
161}
162#endif /* TAPELESS */
163#endif /* __cplusplus */
164#endif /* ADOLC_ADVECTOR_H */
Note: See TracBrowser for help on using the repository browser.