-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ops.h
103 lines (76 loc) · 2.64 KB
/
Ops.h
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
#pragma once
#include <type_traits>
#include <memory>
#include "Type_Ops.h"
namespace netn {
template <typename T> class Model;
struct Component;
template <typename L, typename R>
struct AddFct {
typedef typename add_t<L, R>::type result_t;
static result_t eval(const L & lhs, const R & rhs) {
return lhs + rhs;
}
static result_t derivPart(const L & lhs, const R & rhs, const L & dlhs, const R & drhs) {
return dlhs + drhs;
}
};
template <typename L, typename R>
struct SubFct {
typedef typename sub_t<L, R>::type result_t;
static result_t eval(const L & lhs, const R & rhs) {
return lhs - rhs;
}
static result_t derivPart(const L & lhs, const R & rhs, const L & dlhs, const R & drhs) {
return dlhs - drhs;
}
};
template <typename L, typename R>
struct MulFct {
typedef typename mul_t<L, R>::type result_t;
static result_t eval(const L & lhs, const R & rhs) {
return lhs * rhs;
}
static result_t derivPart(const L & lhs, const R & rhs, const L & dlhs, const R & drhs) {
return lhs * drhs + dlhs * rhs;
}
};
template <typename L, typename R>
struct DivFct {
typedef typename div_t<L, R>::type result_t;
static result_t eval(const L & lhs, const R & rhs) {
return lhs / rhs;
}
static result_t derivPart(const L & lhs, const R & rhs, const L & dlhs, const R & drhs) {
return (dlhs * rhs - lhs * drhs) / (rhs * rhs);
}
};
template<typename L, typename R, typename Fct>
class Ops : public Model<typename Fct::result_t> {
public:
Ops(const Model<L> & lhs, const Model<R> & rhs);
Ops(const Ops & other)
: _lhs(other._lhs), _rhs(other._rhs) {}
value_t eval() const override;
value_t derivPart(const Component & component) const override;
std::shared_ptr<Model<value_t>> toModel() const override;
private:
std::shared_ptr<Model<L>> _lhs;
std::shared_ptr<Model<R>> _rhs;
};
template <typename L, typename R> using AddOp = Ops<L, R, AddFct<L, R>>;
template <typename L, typename R> using SubOp = Ops<L, R, SubFct<L, R>>;
template <typename L, typename R> using MulOp = Ops<L, R, MulFct<L, R>>;
template <typename L, typename R> using DivOp = Ops<L, R, DivFct<L, R>>;
template <typename L, typename R>
AddOp<L, R> operator+(const Model<L> & lhs, const Model<R> & rhs);
template <typename L, typename R>
SubOp<L, R> operator-(const Model<L> & lhs, const Model<R> & rhs);
template <typename L, typename R>
MulOp<L, R> operator*(const Model<L> & lhs, const Model<R> & rhs);
template <typename L, typename R>
DivOp<L, R> operator/(const Model<L> & lhs, const Model<R> & rhs);
template <typename T>
MulOp<double, T> operator*(double lhs, const Model<T> & rhs);
}
#include "Ops.inl"