-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.cxx
73 lines (62 loc) · 1.82 KB
/
operators.cxx
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
#include <cassert>
#include "operators.hxx"
function & operator-(fnbase & rhs)
{
function::fptr_t neg_fptr = [](std::vector<double> & args) {
return -args.front();
};
return *(new function(neg_fptr, 1, &rhs));
}
function & operator+=(fnbase & lhs, /* const */ fnbase & rhs)
{
fnbase_ptr_vec sum_args = {fnbase_ptr(&lhs), fnbase_ptr(&rhs)};
function::fptr_t sum_fptr = [](std::vector<double> & args) {
assert(2 == args.size());
return args[0] + args[1];
};
return *(new function(sum_fptr, sum_args));
}
function & operator+(fnbase & lhs, /* const */ fnbase & rhs)
{
return lhs += rhs;
}
function & operator-=(fnbase & lhs, /* const */ fnbase & rhs)
{
fnbase_ptr_vec diff_args = {fnbase_ptr(&lhs), fnbase_ptr(&rhs)};
function::fptr_t diff_fptr = [](std::vector<double> & args) {
assert(2 == args.size());
return args[0] - args[1];
};
return *(new function(diff_fptr, diff_args));
}
function & operator-(fnbase & lhs, /* const */ fnbase & rhs)
{
return lhs -= rhs;
}
function & operator*=(fnbase & lhs, fnbase & rhs)
{
fnbase_ptr_vec prod_args = {fnbase_ptr(&lhs), fnbase_ptr(&rhs)};
function::fptr_t prod_fptr = [](std::vector<double> & args) {
assert(2 == args.size());
return args[0] * args[1];
};
return *(new function(prod_fptr, prod_args));
}
function & operator*(fnbase & lhs, /* const */ fnbase & rhs)
{
return lhs *= rhs;
}
function & operator/=(fnbase & lhs, /* const */ fnbase & rhs)
{
fnbase_ptr_vec div_args = {fnbase_ptr(&lhs), fnbase_ptr(&rhs)};
function::fptr_t div_fptr = [](std::vector<double> & args) {
assert(2 == args.size());
// assert(args[1] != 0);
return args[0] / args[1]; // pass on inf, do not want to crash
};
return *(new function(div_fptr, div_args));
}
function & operator/(fnbase & lhs, /* const */ fnbase & rhs)
{
return lhs /= rhs;
}