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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* function.cpp - part of abakus
* Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "numerictypes.h"
#include <kdebug.h>
#include <qvaluevector.h>
#include <qstring.h>
#include <qregexp.h>
#include <math.h>
#include "function.h"
#include "node.h"
#include "valuemanager.h"
#include "hmath.h"
// Used to try and avoid recursive function definitions
class DupFinder : public NodeFunctor
{
public:
DupFinder(const QString &nameToFind) :
m_name(nameToFind), m_valid(true)
{
}
virtual ~DupFinder() { }
bool isValid() const { return m_valid; }
virtual void operator()(const Node *node)
{
if(!m_valid)
return;
const BaseFunction *fn = dynamic_cast<const BaseFunction *>(node);
if(fn && fn->name() == m_name)
m_valid = false; // Duplicate detected
}
private:
QString m_name;
bool m_valid;
};
// Define static member for FunctionManager
FunctionManager *FunctionManager::m_manager = 0;
FunctionManager *FunctionManager::instance()
{
if(!m_manager)
m_manager = new FunctionManager;
return m_manager;
}
FunctionManager::FunctionManager(QObject *parent, const char *name) :
QObject(parent, name)
{
m_dict.setAutoDelete(true);
}
// Dummy return value to enable static initialization in the DECL_*()
// macros.
bool FunctionManager::addFunction(const QString &name, function_t fn, const QString &desc)
{
Function *newFn = new Function;
QRegExp returnTrigRE("^a(cos|sin|tan)");
QRegExp needsTrigRE("^(cos|sin|tan)");
QString fnName(name);
newFn->name = name;
newFn->description = desc;
newFn->fn = fn;
newFn->userDefined = false;
newFn->returnsTrig = fnName.contains(returnTrigRE);
newFn->needsTrig = fnName.contains(needsTrigRE);
m_dict.insert(name, newFn);
return false;
}
#define DECLARE_FUNC(name, fn, desc) bool dummy##name = FunctionManager::instance()->addFunction(#name, fn, desc)
// Declares a function name that is implemented by the function of a different
// name. e.g. atan -> Abakus::number_t::arctan()
#define DECLARE_FUNC2(name, fnName, desc) DECLARE_FUNC(name, &Abakus::number_t::fnName, desc)
// Declares a function name that is implemented by the function of the
// same base name.
#define DECLARE_FUNC1(name, desc) DECLARE_FUNC2(name, name, desc)
DECLARE_FUNC1(sin, "Trigonometric sine");
DECLARE_FUNC1(cos, "Trigonometric cosine");
DECLARE_FUNC1(tan, "Trigonometric tangent");
DECLARE_FUNC1(sinh, "Hyperbolic sine");
DECLARE_FUNC1(cosh, "Hyperbolic cosine");
DECLARE_FUNC1(tanh, "Hyperbolic tangent");
DECLARE_FUNC1(atan, "Inverse tangent");
DECLARE_FUNC1(acos, "Inverse cosine");
DECLARE_FUNC1(asin, "Inverse sine");
DECLARE_FUNC1(asinh, "Inverse hyperbolic sine");
DECLARE_FUNC1(acosh, "Inverse hyperbolic cosine");
DECLARE_FUNC1(atanh, "Inverse hyperbolic tangent");
DECLARE_FUNC1(abs, "Absolute value of number");
DECLARE_FUNC1(sqrt, "Square root");
DECLARE_FUNC1(ln, "Natural logarithm (base e)");
DECLARE_FUNC1(log, "Logarithm (base 10)");
DECLARE_FUNC1(exp, "Natural exponential function");
DECLARE_FUNC1(round, "Round to nearest number");
DECLARE_FUNC1(ceil, "Nearest greatest integer");
DECLARE_FUNC1(floor, "Nearest lesser integer");
DECLARE_FUNC2(int, integer, "Integral part of number");
DECLARE_FUNC1(frac, "Fractional part of number");
Function *FunctionManager::function(const QString &name)
{
return m_dict[name];
}
// Returns true if the named identifier is a function, false otherwise.
bool FunctionManager::isFunction(const QString &name)
{
return function(name) != 0;
}
bool FunctionManager::isFunctionUserDefined(const QString &name)
{
const Function *fn = function(name);
return (fn != 0) && (fn->userDefined);
}
bool FunctionManager::addFunction(BaseFunction *fn, const QString &dependantVar)
{
// First see if this function is recursive
DupFinder dupFinder(fn->name());
UnaryFunction *unFunction = dynamic_cast<UnaryFunction *>(fn);
if(unFunction && unFunction->operand()) {
unFunction->operand()->applyMap(dupFinder);
if(!dupFinder.isValid())
return false;
}
// Structure holds extra data needed to call the user defined
// function.
UserFunction *newFn = new UserFunction;
newFn->sequenceNumber = m_dict.count();
newFn->fn = fn;
newFn->varName = QString(dependantVar);
// Now setup the Function data structure that holds the information
// we need to access and call the function later.
Function *fnTabEntry = new Function;
fnTabEntry->name = fn->name();
fnTabEntry->userFn = newFn;
fnTabEntry->returnsTrig = false;
fnTabEntry->needsTrig = false;
fnTabEntry->userDefined = true;
if(m_dict.find(fn->name()))
emit signalFunctionRemoved(fn->name());
m_dict.replace(fn->name(), fnTabEntry);
emit signalFunctionAdded(fn->name());
return true;
}
void FunctionManager::removeFunction(const QString &name)
{
Function *fn = function(name);
// If we remove a function, we need to decrement the sequenceNumber of
// functions after this one.
if(fn && fn->userDefined) {
int savedSeqNum = fn->userFn->sequenceNumber;
// Emit before we actually remove it so that the info on the function
// can still be looked up.
emit signalFunctionRemoved(name);
delete fn->userFn;
fn->userFn = 0;
m_dict.remove(name);
QDictIterator<Function> it(m_dict);
for (; it.current(); ++it) {
UserFunction *userFn = it.current()->userDefined ? it.current()->userFn : 0;
if(userFn && userFn->sequenceNumber > savedSeqNum)
--it.current()->userFn->sequenceNumber;
}
}
}
QStringList FunctionManager::functionList(FunctionManager::FunctionType type)
{
QDictIterator<Function> it(m_dict);
QStringList functions;
switch(type) {
case Builtin:
for(; it.current(); ++it)
if(!it.current()->userDefined)
functions += it.current()->name;
break;
case UserDefined:
// We want to return the function names in the order they were
// added.
{
QValueVector<Function *> fnTable(m_dict.count(), 0);
QValueVector<int> sequenceNumberTable(m_dict.count(), -1);
// First find out what sequence numbers we have.
for(; it.current(); ++it)
if(it.current()->userDefined) {
int id = it.current()->userFn->sequenceNumber;
fnTable[id] = it.current();
sequenceNumberTable.append(id);
}
// Now sort the sequence numbers and return the ordered list
qHeapSort(sequenceNumberTable.begin(), sequenceNumberTable.end());
for(unsigned i = 0; i < sequenceNumberTable.count(); ++i)
if(sequenceNumberTable[i] >= 0)
functions += fnTable[sequenceNumberTable[i]]->name;
}
break;
case All:
functions += functionList(Builtin);
functions += functionList(UserDefined);
break;
}
return functions;
}
// Applies the function identified by func, using value as a parameter.
Abakus::number_t evaluateFunction(const Function *func, const Abakus::number_t value)
{
if(func->userDefined) {
// Pull real entry from userFunctionTable
UserFunction *realFunction = func->userFn;
bool wasSet = ValueManager::instance()->isValueSet(realFunction->varName);
Abakus::number_t oldValue;
if(wasSet)
oldValue = ValueManager::instance()->value(realFunction->varName);
ValueManager::instance()->setValue(realFunction->varName, value);
Abakus::number_t result = realFunction->fn->value();
if(wasSet)
ValueManager::instance()->setValue(realFunction->varName, oldValue);
else
ValueManager::instance()->removeValue(realFunction->varName);
return result;
}
return (value.*(func->fn))();
}
void setTrigMode(Abakus::TrigMode mode)
{
Abakus::m_trigMode = mode;
}
Abakus::TrigMode trigMode()
{
return Abakus::m_trigMode;
}
#include "function.moc"
|