-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecorder.cpp
303 lines (272 loc) · 9.4 KB
/
recorder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Recorder -- A package for Algorithmic Differentiation with CasADi
*
* Copyright (C) 2019 The Authors
* Author: Joris Gillis
* Contributor: Antoine Falisse
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include "recorder.hpp"
static int counter_asserts = 0;
Recorder::Recorder(const Recorder& r) {
value_ = r.value_;
if (r.is_symbol()) {
id_ = get_id();
stream() << "a" + std::to_string(id_) << " = " << r.repr() << ";% copy constructor" << value_ << std::endl;
} else {
id_ = -1;
}
}
Recorder::~Recorder() {
if(id_ == 0) std::cout << "goodbye" << id_ << ":" << value_ << std::endl;
}
Recorder::Recorder() : id_(-1), value_(3.14) {}
Recorder::Recorder(double value) : id_(-1), value_(value) {}
void Recorder::operator<<=(double value) {
if (is_symbol()) throw std::runtime_error("Needs to be symbolic");
id_ = get_id();
stream() << "if nom" << std::endl;
stream() << " " << repr() << " = " << value << ";" << std::endl;
stream() << "else" << std::endl;
stream() << " " << repr() << " = x(" << counter_input+1 << ");" << std::endl;
stream() << "end" << std::endl;
counter_input++;
value_ = value;
}
Recorder& Recorder::operator = ( const Recorder& r) {
value_ = r.value_;
if (r.is_symbol()) {
id_ = get_id();
stream() << "a" + std::to_string(id_) << " = " << r.repr() << ";% copy assignment" << value_ << std::endl;
} else {
id_ = -1;
}
return *this;
}
void Recorder::operator>>=(double& value) {
if (!is_symbol()) throw std::runtime_error("Needs to be symbolic");
stream() << "if ~nom" << std::endl;
stream() << "y{" << counter_output+1 << "} = " << repr() << ";%" << value_ << std::endl;
stream() << "end" << std::endl;
counter_output++;
value = value_;
}
double Recorder::getValue() const {return value_;}
Recorder operator+(const Recorder& lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, lhs.value_ + rhs.value_, "plus");
}
Recorder operator*(const Recorder& lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, lhs.value_ * rhs.value_, "times");
}
Recorder operator-(const Recorder& lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, lhs.value_ - rhs.value_, "minus");
}
Recorder operator/(const Recorder& lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, lhs.value_ / rhs.value_, "rdivide");
}
bool operator>=(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ >= rhs.value_, "ge"));
}
bool operator<=(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ <= rhs.value_, "le"));
}
bool operator>(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ > rhs.value_, "gt"));
}
bool operator<(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ < rhs.value_, "lt"));
}
bool operator!=(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ != rhs.value_, "ne"));
}
bool operator==(const Recorder& lhs, const Recorder& rhs) {
return static_cast<bool>(Recorder::from_binary(lhs, rhs, lhs.value_ == rhs.value_, "eq"));
}
Recorder operator-(const Recorder& arg) {
return Recorder::from_unary(arg, -arg.value_, "uminus");
}
Recorder pow( const Recorder&lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, pow(lhs.value_,rhs.value_), "power");
}
Recorder fmax ( const Recorder&lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, fmax(lhs.value_,rhs.value_), "max");
}
Recorder fmin ( const Recorder&lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, fmin(lhs.value_,rhs.value_), "min");
}
Recorder atan2 ( const Recorder&lhs, const Recorder& rhs) {
return Recorder::from_binary(lhs, rhs, atan2(lhs.value_,rhs.value_), "atan2");
}
Recorder exp(const Recorder& arg) {
return Recorder::from_unary(arg, exp(arg.value_), "exp");
}
Recorder log(const Recorder& arg) {
return Recorder::from_unary(arg, log(arg.value_), "log");
}
Recorder sqrt(const Recorder& arg) {
return Recorder::from_unary(arg, sqrt(arg.value_), "sqrt");
}
Recorder sin(const Recorder& arg) {
return Recorder::from_unary(arg, sin(arg.value_), "sin");
}
Recorder cos(const Recorder& arg) {
return Recorder::from_unary(arg, cos(arg.value_), "cos");
}
Recorder tan(const Recorder& arg) {
return Recorder::from_unary(arg, tan(arg.value_), "tan");
}
Recorder asin(const Recorder& arg) {
return Recorder::from_unary(arg, asin(arg.value_), "asin");
}
Recorder acos(const Recorder& arg) {
return Recorder::from_unary(arg, acos(arg.value_), "acos");
}
Recorder atan(const Recorder& arg) {
return Recorder::from_unary(arg, atan(arg.value_), "atan");
}
Recorder log10(const Recorder& arg) {
return Recorder::from_unary(arg, log10(arg.value_), "log10");
}
Recorder sinh(const Recorder& arg) {
return Recorder::from_unary(arg, sinh(arg.value_), "sinh");
}
Recorder cosh(const Recorder& arg) {
return Recorder::from_unary(arg, cosh(arg.value_), "cosh");
}
Recorder tanh(const Recorder& arg) {
return Recorder::from_unary(arg, tanh(arg.value_), "tanh");
}
Recorder asinh(const Recorder& arg) {
return Recorder::from_unary(arg, asinh(arg.value_), "asinh");
}
Recorder acosh(const Recorder& arg) {
return Recorder::from_unary(arg, acosh(arg.value_), "acosh");
}
Recorder atanh(const Recorder& arg) {
return Recorder::from_unary(arg, atanh(arg.value_), "atanh");
}
Recorder erf(const Recorder& arg) {
return Recorder::from_unary(arg, erf(arg.value_), "erf");
}
Recorder fabs(const Recorder& arg) {
return Recorder::from_unary(arg, fabs(arg.value_), "abs");
}
Recorder ceil(const Recorder& arg) {
return Recorder::from_unary(arg, ceil(arg.value_), "ceil");
}
Recorder floor(const Recorder& arg) {
return Recorder::from_unary(arg, floor(arg.value_), "floor");
}
Recorder::operator bool() const {
bool ret = value_==1;
if (is_symbol()) {
counter_bool++;
stream() << "a{" << counter_asserts+1 << "} = " << repr() << "-" << value_ << ";%" << value_ << std::endl;
}
return ret;
}
std::ostream& operator<<(std::ostream &stream, const Recorder& obj) {
obj.disp(stream);
return stream;
}
std::istream& operator >> (std::istream& is, const Recorder& a) {
throw std::runtime_error("No way!");
}
void Recorder::stop_recording() {
stream() << "if ~nom, y = vertcat(y{:}); end;" << std::endl;
if (counter_bool > 0) {
stream() << "a = vertcat(a{:});" << std::endl;
}
}
void Recorder::disp(std::ostream &stream) const {
if (is_symbol()) {
stream << "[#" << id_ << "|" << value_ << "]";
} else {
stream << "(" << value_ << ")";
}
}
int Recorder::get_id() {
counter++;
return counter;
}
bool Recorder::is_symbol() const {
return id_>=0;
}
std::string Recorder::repr() const {
if (is_symbol()) {
return "a" + std::to_string(id_);
} else {
std::stringstream ss;
ss << std::scientific << std::setprecision(16);
ss << value_;
return ss.str();
}
}
bool is_suspicious(double v) {
return (v>0 && v <1e-200) || (v<0 && v >-1e-200);
}
Recorder Recorder::from_binary(const Recorder& lhs, const Recorder& rhs, double res, const std::string& op) {
if (lhs.is_symbol() || rhs.is_symbol()) {
int id = get_id();
stream() << "a" << id << " = " << op << "(" << lhs.repr() << "," << rhs.repr() << ");" << std::endl;
stream() << "if nom, assert(" << "a" << id << "==" << res << "); end;" << std::endl;
if (is_suspicious(res)) {
stream() << "% suspicious activity" << std::endl;
}
return Recorder(res, id);
} else {
return Recorder(res);
}
}
Recorder Recorder::from_unary(const Recorder& arg, double res, const std::string& op) {
if (arg.is_symbol()) {
int id = get_id();
stream() << "a" << id << " = " << op << "(" << arg.repr() << ");" << std::endl;
stream() << "if nom, assert(" << "a" << id << "==" << res << "); end;" << std::endl;
if (is_suspicious(res)) {
stream() << "% suspicious activity" << std::endl;
}
return Recorder(res, id);
} else {
return Recorder(res);
}
}
class StreamWrapper {
public:
StreamWrapper() {
stream = new std::ofstream("foo.m");
(*stream) << std::scientific << std::setprecision(16);
(*stream) << "function [y,a]=foo(x)" << std::endl;
(*stream) << "nom = nargin==0;" << std::endl;
}
std::ofstream* stream;
};
static StreamWrapper stream_wrapper{};
std::ofstream& Recorder::stream() {
return *stream_wrapper.stream;
};
Recorder::Recorder(double value, int id) {
id_ = id;
value_ = value;
}
int Recorder::counter = 0;
int Recorder::counter_input = 0;
int Recorder::counter_output = 0;
int Recorder::counter_bool = 0;