-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.hpp
More file actions
141 lines (108 loc) · 3.18 KB
/
func.hpp
File metadata and controls
141 lines (108 loc) · 3.18 KB
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
#ifndef FUNC_H
#define FUNC_H
#include <functional>
template <typename out, typename in>
out e(in value = in{}) {
return out{};
}
template <typename out = double, typename in = double>
class func {
public:
func() : func(e<out, in>){}
func (const std::function<out(in)>& f_) : f(f_) {}
func(out (*p)(in)) : func(std::function<out(in)>(p)) {}
func(const func<in, out>& f_) : func(f_.f ) {}
func(const in& value) {
*this = constant(value);
}
out operator()(in value = in{}) const {
return f(value);
}
// Stores given constant in f
func& constant(const in& value) {
this->f = [value](in vd) -> out { return value; };
return *this;
}
// Returns given value (may convert from in_type to out_type)
func& chi() {
this-> f = [](in value) -> out { return value; };
return *this;
}
func& operator=(const func& f_) {
this->f = f_.f;
return *this;
}
func& operator+=(const func& f_) {
this->f = [*this, f_](in value) -> out { return f(value) + f_(value); };
return *this;
}
func& operator-=(const func& f_) {
this->f = [*this, f_](in value) -> out { return f(value) - f_(value); };
return *this;
}
func operator-() const {
return func() - *this;
}
func& operator*=(const func& f_) {
this->f = [*this, f_](in value) -> out { return f(value) * f_(value); };
return *this;
}
func& operator/=(const func& f_) {
this->f = [*this, f_](in value) -> out { return f(value) / f_(value); };
return *this;
}
func& pow_to(const func& f_) {
this->f = [*this, f_](in value) -> out { return std::pow(f(value), f_(value)); };
return *this;
}
func pow(const func& f_) const {
func<out, in> f__ = *this;
return pow_to(f__, f_);
}
func& compose_to(const func& f_) {
this->f = [*this, f_](in value) -> out { return f(f_(value)); };
return *this;
}
func compose(const func& f_) {
func<out, in> f__ = *this;
return compose_to(f__, f_);
}
bool operator==(const func& f_) {
return false;
}
private:
std::function<out(in)> f;
};
template <class out, class in>
std::ostream& operator<<(std::ostream& ost, const func<out, in>& f) {
ost << f(in{});
return ost;
}
template <class out, class in>
std::istream& operator>>(std::istream& ist, func<out, in>& f) {
in value;
ist >> value;
f.constant(value);
return ist;
}
template<class out, class in>
inline func<out, in> operator+(const func<out, in>& f1, const func<out, in>& f2) {
func<out, in> f3 = f1;
return f3 += f2;
}
template<class out, class in>
inline func<out, in> operator-(const func<out, in>& f1, const func<out, in>& f2) {
func<out, in> f3 = f1;
return f3 -= f2;
}
template<class out, class in>
inline func<out, in> operator*(const func<out, in>& f1, const func<out, in>& f2) {
func<out, in> f3 = f1;
return f3 *= f2;
}
template<class out, class in>
inline func<out, in> operator/(const func<out, in>& f1, const func<out, in>& f2) {
func<out, in> f3 = f1;
return f3 /= f2;
}
#endif