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
|
/*
fuzzylite (R), a fuzzy logic control library in C++.
Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>
This file is part of fuzzylite.
fuzzylite is free software: you can redistribute it and/or modify it under
the terms of the FuzzyLite License included with the software.
You should have received a copy of the FuzzyLite License along with
fuzzylite. If not, see <http://www.fuzzylite.com/license/>.
fuzzylite is a registered trademark of FuzzyLite Limited.
*/
#include "fl/factory/FunctionFactory.h"
#include "fl/rule/Rule.h"
namespace fl {
FunctionFactory::FunctionFactory() : CloningFactory<Function::Element*>("Function::Element") {
registerOperators();
registerFunctions();
}
FunctionFactory::~FunctionFactory() { }
void FunctionFactory::registerOperators() {
//OPERATORS:
int p = 100;
//First order: not, negate:
registerObject("!", new Function::Element("!", "Logical NOT",
Function::Element::Operator, &(Op::logicalNot), p, 1)); //logical not
registerObject("~", new Function::Element("~", "Negation",
Function::Element::Operator, &(Op::negate), p, 1)); // ~ negates a number
p -= 10;
//Second order: power
registerObject("^", new Function::Element("^", "Power",
Function::Element::Operator, &(std::pow), p, 1));
p -= 10;
//Third order: multiplication, division, modulo
registerObject("*", new Function::Element("*", "Multiplication",
Function::Element::Operator, &(Op::multiply), p));
registerObject("/", new Function::Element("/", "Division",
Function::Element::Operator, &(Op::divide), p));
registerObject("%", new Function::Element("%", "Modulo",
Function::Element::Operator, &(Op::modulo), p));
p -= 10;
//Fourth order: addition, subtraction
registerObject("+", new Function::Element("+", "Addition",
Function::Element::Operator, &(Op::add), p));
registerObject("-", new Function::Element("-", "Subtraction",
Function::Element::Operator, &(Op::subtract), p));
//Fifth order: logical and, logical or
p -= 10; //Logical AND
registerObject(Rule::andKeyword(), new Function::Element(Rule::andKeyword(), "Logical AND",
Function::Element::Operator, &(Op::logicalAnd), p));
p -= 10; //Logical OR
registerObject(Rule::orKeyword(), new Function::Element(Rule::orKeyword(), "Logical OR",
Function::Element::Operator, &(Op::logicalOr), p));
}
void FunctionFactory::registerFunctions() {
//FUNCTIONS
registerObject("gt", new Function::Element("gt", "Greater than (>)",
Function::Element::Function, &(Op::gt)));
registerObject("ge", new Function::Element("ge", "Greater than or equal to (>=)",
Function::Element::Function, &(Op::ge)));
registerObject("eq", new Function::Element("eq", "Equal to (==)",
Function::Element::Function, &(Op::eq)));
registerObject("neq", new Function::Element("neq", "Not equal to (!=)",
Function::Element::Function, &(Op::neq)));
registerObject("le", new Function::Element("le", "Less than or equal to (<=)",
Function::Element::Function, &(Op::le)));
registerObject("lt", new Function::Element("lt", "Less than (<)",
Function::Element::Function, &(Op::lt)));
registerObject("min", new Function::Element("min", "Minimum",
Function::Element::Function, &(Op::min)));
registerObject("max", new Function::Element("max", "Maximum",
Function::Element::Function, &(Op::max)));
registerObject("acos", new Function::Element("acos", "Inverse cosine",
Function::Element::Function, &(std::acos)));
registerObject("asin", new Function::Element("asin", "Inverse sine",
Function::Element::Function, &(std::asin)));
registerObject("atan", new Function::Element("atan", "Inverse tangent",
Function::Element::Function, &(std::atan)));
registerObject("ceil", new Function::Element("ceil", "Ceiling",
Function::Element::Function, &(std::ceil)));
registerObject("cos", new Function::Element("cos", "Cosine",
Function::Element::Function, &(std::cos)));
registerObject("cosh", new Function::Element("cosh", "Hyperbolic cosine",
Function::Element::Function, &(std::cosh)));
registerObject("exp", new Function::Element("exp", "Exponential",
Function::Element::Function, &(std::exp)));
registerObject("abs", new Function::Element("abs", "Absolute",
Function::Element::Function, &(std::abs)));
registerObject("fabs", new Function::Element("fabs", "Absolute",
Function::Element::Function, &(std::fabs)));
registerObject("floor", new Function::Element("floor", "Floor",
Function::Element::Function, &(std::floor)));
registerObject("log", new Function::Element("log", "Natural logarithm",
Function::Element::Function, &(std::log)));
registerObject("log10", new Function::Element("log10", "Common logarithm",
Function::Element::Function, &(std::log10)));
registerObject("round", new Function::Element("round", "Round",
Function::Element::Function, &(Op::round)));
registerObject("sin", new Function::Element("sin", "Sine",
Function::Element::Function, &(std::sin)));
registerObject("sinh", new Function::Element("sinh", "Hyperbolic sine",
Function::Element::Function, &(std::sinh)));
registerObject("sqrt", new Function::Element("sqrt", "Square root",
Function::Element::Function, &(std::sqrt)));
registerObject("tan", new Function::Element("tan", "Tangent",
Function::Element::Function, &(std::tan)));
registerObject("tanh", new Function::Element("tanh", "Hyperbolic tangent",
Function::Element::Function, &(std::tanh)));
#if defined(FL_UNIX) && !defined(FL_USE_FLOAT)
//found in Unix when using double precision. not found in Windows.
registerObject("log1p", new Function::Element("log1p", "Natural logarithm plus one",
Function::Element::Function, &(log1p)));
registerObject("acosh", new Function::Element("acosh", "Inverse hyperbolic cosine",
Function::Element::Function, &(acosh)));
registerObject("asinh", new Function::Element("asinh", "Inverse hyperbolic sine",
Function::Element::Function, &(asinh)));
registerObject("atanh", new Function::Element("atanh", "Inverse hyperbolic tangent",
Function::Element::Function, &(atanh)));
#endif
registerObject("pow", new Function::Element("pow", "Power",
Function::Element::Function, &(std::pow)));
registerObject("atan2", new Function::Element("atan2", "Inverse tangent (y,x)",
Function::Element::Function, &(std::atan2)));
registerObject("fmod", new Function::Element("fmod", "Floating-point remainder",
Function::Element::Function, &(std::fmod)));
}
std::vector<std::string> FunctionFactory::availableOperators() const {
std::vector<std::string> result;
std::map<std::string, Function::Element*>::const_iterator it = this->objects().begin();
while (it != this->objects().end()) {
if (it->second and it->second->type == Function::Element::Operator)
result.push_back(it->first);
++it;
}
return result;
}
std::vector<std::string> FunctionFactory::availableFunctions() const {
std::vector<std::string> result;
std::map<std::string, Function::Element*>::const_iterator it = this->objects().begin();
while (it != this->objects().end()) {
if (it->second and it->second->type == Function::Element::Function)
result.push_back(it->first);
++it;
}
return result;
}
}
|