-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.cpp
112 lines (88 loc) · 2.85 KB
/
function.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
#include "function.hpp"
#include <math.h>
/**
* Add here functions to extend functionality
*/
Function::Registry Function::registry = {
{"sin", [](const Values& arg){ return std::make_shared<SinusFunction>(arg); }},
{"cos", [](const Values& arg){ return std::make_shared<CosinusFunction>(arg); }},
{"tan", [](const Values& arg){ return std::make_shared<TanFunction>(arg); }},
{"atan", [](const Values& arg){ return std::make_shared<ATanFunction>(arg); }},
{"asin", [](const Values& arg){ return std::make_shared<ASinFunction>(arg); }},
{"acos", [](const Values& arg){ return std::make_shared<ACosFunction>(arg); }},
{"hypot", [](const Values& arg){ return std::make_shared<HypotFunction>(arg); }},
};
Function::Function(const Values& arg, size_t numArgs)
: m_Arg(arg)
{
if (m_Arg.empty()) {
throw std::runtime_error("Function can not be instantiated w/o argument");
}
if (m_Arg.size() != numArgs) {
throw std::runtime_error("Function has incorrect number of arguments");
}
for (auto& arg : m_Arg) {
if (!arg) {
throw std::runtime_error("Function can not be instantiated with empty argument");
}
}
}
const Values& Function::arg() const {
return m_Arg;
}
////////////////////////////////////////////////////////////////////////////////
SinusFunction::SinusFunction(const Values& value)
: Function(value, 1)
{
}
float SinusFunction::evaluate() {
return sinf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
CosinusFunction::CosinusFunction(const Values& value)
: Function(value, 1)
{
}
float CosinusFunction::evaluate() {
return cosf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
TanFunction::TanFunction(const Values& value)
: Function(value, 1)
{
}
float TanFunction::evaluate() {
return tanf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
ATanFunction::ATanFunction(const Values& value)
: Function(value, 1)
{
}
float ATanFunction::evaluate() {
return atanf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
ASinFunction::ASinFunction(const Values& value)
: Function(value, 1)
{
}
float ASinFunction::evaluate() {
return asinf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
ACosFunction::ACosFunction(const Values& value)
: Function(value, 1)
{
}
float ACosFunction::evaluate() {
return acosf(arg()[0]->evaluate());
}
////////////////////////////////////////////////////////////////////////////////
HypotFunction::HypotFunction(const Values& value)
: Function(value, 2)
{
}
float HypotFunction::evaluate() {
return hypot(arg()[0]->evaluate(), arg()[1]->evaluate());
}