-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTN1.hpp
421 lines (345 loc) · 11.2 KB
/
HTN1.hpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#pragma once
#include "HTN0.hpp"
namespace HTN {
/*
Automatic Calculation of "Gradient"
template
- s_type : "float" or "double"
- dim : The size of the variable you want to use (0 ... dim-1)
*/
template<typename s_type, unsigned int dim = 1,
typename std::enable_if<std::is_floating_point<s_type>::value>::type* = nullptr>
class htn_r1 : public htn_r0<s_type, dim>, htn_tag {
public:
using this_type_v = htn_r1<s_type, dim>;
using inner_type_s = viennacl::scalar<s_type>;
using inner_type_v = viennacl::vector<s_type>;
using inner_type_v_with_bool = std::pair<bool, inner_type_v>;
friend class htn_r1<s_type, dim>;
template<unsigned int id = 0>
static this_type_v x(s_type scalar) {
assert(id < dim);
return this_type_v(inner_type_s(scalar), viennacl::unit_vector<s_type>(dim, id));
}
static this_type_v x(s_type scalar, unsigned int id = 0) {
assert(id < dim);
return this_type_v(inner_type_s(scalar), viennacl::unit_vector<s_type>(dim, id));
}
htn_r1() = default;
using htn_r0<s_type, dim>::htn_r0;
htn_r1(const inner_type_s& init0, const inner_type_v& init1) {
this->x0 = init0;
this->x1 = init1;
this->is_zero_vector = false;
}
this_type_v& operator=(const this_type_v& src) {
this->x0 = src.x0;
this->x1 = src.x1;
this->is_zero_vector = src.is_zero_vector;
return *this;
}
this_type_v& operator=(s_type src) {
this->x0 = src;
if (!is_zero_vector) {
this->x1.empty();
is_zero_vector = true;
}
return *this;
}
this_type_v operator+() const {
return *this;
}
this_type_v operator-() const {
return create_this_type_v(this->minus0r(), this->minus1r());
}
this_type_v operator+(const this_type_v& rhs) const {
return create_this_type_v(this->add0r(rhs), this->add1r(rhs));
}
this_type_v operator-(const this_type_v& rhs) const {
return create_this_type_v(this->sub0r(rhs), this->sub1r(rhs));
}
this_type_v operator*(const this_type_v& rhs) const {
return create_this_type_v(this->mul0r(rhs), this->mul1r(rhs));
}
this_type_v operator/(const this_type_v& rhs) const {
auto rhsinv = rhs.inverse();
return create_this_type_v(this->mul0r(rhsinv), this->mul1r(rhsinv));
}
this_type_v& operator+=(const this_type_v& rhs) {
return (*this = *this + rhs);
}
this_type_v& operator-=(const this_type_v& rhs) {
return (*this = *this - rhs);
}
this_type_v& operator*=(const this_type_v& rhs) {
return (*this = *this * rhs);
}
this_type_v& operator/=(const this_type_v& rhs) {
return (*this = *this / rhs);
}
this_type_v& operator+=(s_type rhs) {
return (*this = *this + this_type_v(rhs));
}
this_type_v& operator-=(s_type rhs) {
return (*this = *this - this_type_v(rhs));
}
this_type_v& operator*=(s_type rhs) {
return (*this = *this * this_type_v(rhs));
}
this_type_v& operator/=(s_type rhs) {
return (*this = *this / this_type_v(rhs));
}
this_type_v inverse() const {
return create_this_type_v(this->inv0r(), this->inv1r());
}
this_type_v pow(s_type index) const {
return create_this_type_v(this->pow0r(index), this->pow1r(index));
}
this_type_v pow(const this_type_v& index) const {
if (index.is_zero_vector) {
return create_this_type_v(this->pow0r((s_type)index.x0), this->pow1r((s_type)index.x0));
}
else {
return create_this_type_v(this->pow0r((s_type)index.x0), this->pow1r2(index));
}
}
this_type_v sqrt() const {
return (*this).pow(static_cast<s_type>(0.5));
}
this_type_v exp() const {
return create_this_type_v(this->exp0r(), this->exp1r());
}
this_type_v log() const {
return create_this_type_v(this->log0r(), this->log1r());
}
this_type_v sin() const {
return create_this_type_v(this->sin0r(), this->sin1r());
}
this_type_v cos() const {
return create_this_type_v(this->cos0r(), this->cos1r());
}
this_type_v tan() const {
return create_this_type_v(this->tan0r(), this->tan1r());
}
this_type_v asin() const {
return create_this_type_v(this->asin0r(), this->asin1r());
}
this_type_v acos() const {
return create_this_type_v(this->acos0r(), this->acos1r());
}
this_type_v atan() const {
return create_this_type_v(this->atan0r(), this->atan1r());
}
this_type_v sinh() const {
return ((*this).exp() - (-(*this)).exp()) * this_type_v(static_cast<s_type>(0.5));
}
this_type_v cosh() const {
return ((*this).exp() + (-(*this)).exp()) * this_type_v(static_cast<s_type>(0.5));
}
this_type_v tanh() const {
const auto exp_p = (*this).exp();
const auto exp_m = (-(*this)).exp();
return (exp_p - exp_m) / (exp_p + exp_m);
}
this_type_v asinh() const {
const auto _1 = this_type_v(static_cast<s_type>(1));
return ((*this) + ( this->pow(2) + _1 ).sqrt()).log();
}
this_type_v acosh(s_type pm = 1) const {
const auto _1 = this_type_v(static_cast<s_type>(1));
const auto _sgn = this_type_v(static_cast<s_type>((pm >= 0) - (pm < 0)));
return ((*this) + _sgn * ( this->pow(2) - _1 ).sqrt() ).log();
}
this_type_v atanh() const {
const auto _05 = this_type_v(static_cast<s_type>(0.5));
const auto _1 = this_type_v(static_cast<s_type>(1));
return ( (_1 + *this) / (_1 - *this) ).log() * _05;
}
inner_type_v& X1() {
return x1;
}
inner_type_v X1() const {
return x1;
}
Eigen::Matrix<s_type, -1, 1> X1_to_eig() const {
Eigen::Matrix<s_type, -1, 1> res(dim);
if (is_zero_vector) {
res.setZero();
}
else {
viennacl::copy(x1, res);
}
return res;
}
protected:
inner_type_v x1;
bool is_zero_vector = true;
this_type_v create_this_type_v(const inner_type_s& src0, const inner_type_v_with_bool& src1) const {
if (!src1.first) {
return this_type_v(src0, src1.second);
}
else {
return this_type_v(src0);
}
}
inner_type_v_with_bool minus1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, -x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool add1r(const this_type_v& rhs) const {
if (is_zero_vector == false && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, x1 + rhs.x1);
}
else if (is_zero_vector == false && rhs.is_zero_vector == true) {
return inner_type_v_with_bool(false, x1);
}
else if (is_zero_vector == true && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, rhs.x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool sub1r(const this_type_v& rhs) const {
if (is_zero_vector == false && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, x1 - rhs.x1);
}
else if (is_zero_vector == false && rhs.is_zero_vector == true) {
return inner_type_v_with_bool(false, x1);
}
else if (is_zero_vector == true && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, -rhs.x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool mul1r(const this_type_v& rhs) const {
if (is_zero_vector == false && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, rhs.x0 * x1 + this->x0 * rhs.x1);
}
else if (is_zero_vector == false && rhs.is_zero_vector == true) {
return inner_type_v_with_bool(false, rhs.x0 * x1);
}
else if (is_zero_vector == true && rhs.is_zero_vector == false) {
return inner_type_v_with_bool(false, this->x0 * rhs.x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool inv1r() const {
assert(this->x0 != .0);
if (!is_zero_vector) {
return inner_type_v_with_bool(false, -x1 / (this->x0 * this->x0));
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool pow1r(s_type index) const {
/*infinity error*/
assert(!(this->x0 == .0 && index < 1));
/*imaginary number error*/
assert(!(this->x0 < .0 && std::floor(index) != index));
if (index == 0) return inner_type_v_with_bool(true, inner_type_v());
if (!is_zero_vector) {
return inner_type_v_with_bool(false, x1 * ( index * std::pow((s_type)this->x0, index - static_cast<s_type>(1)) ) );
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool pow1r2(const this_type_v& index) const {
/*infinity error*/
assert(!(this->x0 == .0 && index.x0 < 1));
/*imaginary number error*/
assert(!(this->x0 < .0 && std::floor(index.x0) != index.x0));
if (is_zero_vector == false && index.is_zero_vector == false) {
const s_type c2 = (s_type)index.x0 * std::pow((s_type)this->x0, (s_type)index.x0 - static_cast<s_type>(1));
const s_type c1 = std::pow((s_type)this->x0, (s_type)index.x0) * std::log((s_type)this->x0);
return inner_type_v_with_bool(false, c2 * x1 + c1 * index.x1);
}
else if (is_zero_vector == false && index.is_zero_vector == true) {
const s_type c2 = (s_type)index.x0 * std::pow((s_type)this->x0, (s_type)index.x0 - static_cast<s_type>(1));
return inner_type_v_with_bool(false, c2 * x1);
}
else if (is_zero_vector == true && index.is_zero_vector == false) {
const s_type c1 = std::pow((s_type)this->x0, (s_type)index.x0) * std::log((s_type)this->x0);
return inner_type_v_with_bool(false, c1 * index.x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool exp1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, std::exp((s_type)this->x0) * x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool log1r() const {
assert(this->x0 > .0);
if (!is_zero_vector) {
return inner_type_v_with_bool(false, x1 / (s_type)this->x0);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool sin1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, std::cos((s_type)this->x0) * x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool cos1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, -std::sin((s_type)this->x0) * x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool tan1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, (static_cast<s_type>(1) + std::pow(std::tan((s_type)this->x0), 2)) * x1);
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool asin1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, x1 / std::sqrt(static_cast<s_type>(1) - std::pow((s_type)this->x0, 2)) );
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool acos1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, -x1 / std::sqrt(static_cast<s_type>(1) - std::pow((s_type)this->x0, 2)));
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
inner_type_v_with_bool atan1r() const {
if (!is_zero_vector) {
return inner_type_v_with_bool(false, x1 / (static_cast<s_type>(1) + std::pow((s_type)this->x0, 2)) );
}
else {
return inner_type_v_with_bool(true, inner_type_v());
}
}
};
}