summaryrefslogtreecommitdiff
path: root/fuzzylite/src/factory/FunctionFactory.cpp
blob: 3c0718d34b8544813f23d527afca4459935b3fb3 (plain)
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
170
171
172
/*
 Author: Juan Rada-Vilela, Ph.D.
 Copyright (C) 2010-2014 FuzzyLite Limited
 All rights reserved

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation, either version 3 of the License, or (at your option)
 any later version.

 fuzzylite is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with fuzzylite.  If not, see <http://www.gnu.org/licenses/>.

 fuzzylite™ is a 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, &(fl::Op::logicalNot), p, 1)); //logical not
        registerObject("~", new Function::Element("~", "Negation",
                Function::Element::OPERATOR, &(fl::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, &(fl::Op::multiply), p));
        registerObject("/", new Function::Element("/", "Division",
                Function::Element::OPERATOR, &(fl::Op::divide), p));
        registerObject("%", new Function::Element("%", "Modulo",
                Function::Element::OPERATOR, &(fl::Op::modulo), p));

        p -= 10;
        //Fourth order: addition, subtraction
        registerObject("+", new Function::Element("+", "Addition",
                Function::Element::OPERATOR, &(fl::Op::add), p));
        registerObject("-", new Function::Element("-", "Subtraction",
                Function::Element::OPERATOR, &(fl::Op::subtract), p));

        //Fifth order: logical and, logical or
        p -= 10; //Logical AND
        registerObject(fl::Rule::andKeyword(), new Function::Element(fl::Rule::andKeyword(), "Logical AND",
                Function::Element::OPERATOR, &(fl::Op::logicalAnd), p));
        p -= 10; //Logical OR
        registerObject(fl::Rule::orKeyword(), new Function::Element(fl::Rule::orKeyword(), "Logical OR",
                Function::Element::OPERATOR, &(fl::Op::logicalOr), p));
    }

    void FunctionFactory::registerFunctions() {
        //FUNCTIONS
        registerObject("gt", new Function::Element("gt", "Greater than (>)",
                Function::Element::FUNCTION, &(fl::Op::gt)));
        registerObject("ge", new Function::Element("ge", "Greater than or equal to (>=)",
                Function::Element::FUNCTION, &(fl::Op::ge)));
        registerObject("eq", new Function::Element("eq", "Equal to (==)",
                Function::Element::FUNCTION, &(fl::Op::eq)));
        registerObject("neq", new Function::Element("neq", "Not equal to (!=)",
                Function::Element::FUNCTION, &(fl::Op::neq)));
        registerObject("le", new Function::Element("le", "Less than or equal to (<=)",
                Function::Element::FUNCTION, &(fl::Op::le)));
        registerObject("lt", new Function::Element("lt", "Less than (<)",
                Function::Element::FUNCTION, &(fl::Op::lt)));

        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("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, &(fl::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;
    }

}