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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
<?php
/**
* This file is part of the MathExecutor package
*
* (c) Alexander Kiryukhin
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace NXP;
use NXP\Classes\Calculator;
use NXP\Classes\Lexer;
use NXP\Classes\TokenFactory;
use NXP\Exception\UnknownVariableException;
/**
* Class MathExecutor
* @package NXP
*/
class MathExecutor
{
/**
* Available variables
*
* @var array
*/
private $variables = [];
/**
* @var TokenFactory
*/
private $tokenFactory;
/**
* @var array
*/
private $cache = [];
/**
* Base math operators
*/
public function __construct()
{
$this->addDefaults();
}
public function __clone()
{
$this->addDefaults();
}
/**
* Get all vars
*
* @return array
*/
public function getVars()
{
return $this->variables;
}
/**
* Get a specific var
*
* @param string $variable
* @return integer|float
* @throws UnknownVariableException
*/
public function getVar($variable)
{
if (!isset($this->variables[$variable])) {
throw new UnknownVariableException("Variable ({$variable}) not set");
}
return $this->variables[$variable];
}
/**
* Add variable to executor
*
* @param string $variable
* @param integer|float $value
* @return MathExecutor
* @throws \Exception
*/
public function setVar($variable, $value)
{
if (!is_numeric($value)) {
throw new \Exception("Variable ({$variable}) value must be a number ({$value}) type ({gettype($value)})");
}
$this->variables[$variable] = $value;
return $this;
}
/**
* Add variables to executor
*
* @param array $variables
* @param bool $clear Clear previous variables
* @return MathExecutor
* @throws \Exception
*/
public function setVars(array $variables, $clear = true)
{
if ($clear) {
$this->removeVars();
}
foreach ($variables as $name => $value) {
$this->setVar($name, $value);
}
return $this;
}
/**
* Remove variable from executor
*
* @param string $variable
* @return MathExecutor
*/
public function removeVar($variable)
{
unset ($this->variables[$variable]);
return $this;
}
/**
* Remove all variables
*/
public function removeVars()
{
$this->variables = [];
return $this;
}
/**
* Add operator to executor
*
* @param string $operatorClass Class of operator token
* @return MathExecutor
* @throws Exception\UnknownOperatorException
*/
public function addOperator($operatorClass)
{
$this->tokenFactory->addOperator($operatorClass);
return $this;
}
/**
* Get all registered operators to executor
*
* @return array of operator class names
*/
public function getOperators()
{
return $this->tokenFactory->getOperators();
}
/**
* Add function to executor
*
* @param string $name Name of function
* @param callable $function Function
* @param int $places Count of arguments
* @return MathExecutor
* @throws \ReflectionException
*/
public function addFunction($name, $function = null, $places = null)
{
$this->tokenFactory->addFunction($name, $function, $places);
return $this;
}
/**
* Get all registered functions
*
* @return array containing callback and places indexed by
* function name
*/
public function getFunctions()
{
return $this->tokenFactory->getFunctions();
}
/**
* Set division by zero exception reporting
*
* @param bool $exception default true
*
* @return MathExecutor
*/
public function setDivisionByZeroException($exception = true)
{
$this->tokenFactory->setDivisionByZeroException($exception);
return $this;
}
/**
* Get division by zero exception status
*
* @return bool
*/
public function getDivisionByZeroException()
{
return $this->tokenFactory->getDivisionByZeroException();
}
/**
* Execute expression
*
* @param $expression
* @return number
*/
public function execute($expression)
{
$cachekey = (string)$expression;
if (!array_key_exists($cachekey, $this->cache)) {
$lexer = new Lexer($this->tokenFactory);
$tokensStream = $lexer->stringToTokensStream($expression);
$tokens = $lexer->buildReversePolishNotation($tokensStream);
$this->cache[$cachekey] = $tokens;
} else {
$tokens = $this->cache[$cachekey];
}
$calculator = new Calculator();
$result = $calculator->calculate($tokens, $this->variables);
return $result;
}
/**
* Set default operands and functions
*/
protected function addDefaults()
{
$this->tokenFactory = new TokenFactory();
foreach ($this->defaultOperators() as $operatorClass) {
$this->tokenFactory->addOperator($operatorClass);
}
foreach ($this->defaultFunctions() as $name => $callable) {
$this->tokenFactory->addFunction($name, $callable);
}
$this->setVars($this->defaultVars());
}
protected function defaultOperators()
{
return [
'NXP\Classes\Token\TokenPlus',
'NXP\Classes\Token\TokenMinus',
'NXP\Classes\Token\TokenMultiply',
'NXP\Classes\Token\TokenDivision',
'NXP\Classes\Token\TokenDegree',
'NXP\Classes\Token\TokenAnd',
'NXP\Classes\Token\TokenOr',
'NXP\Classes\Token\TokenEqual',
'NXP\Classes\Token\TokenUnequal',
'NXP\Classes\Token\TokenGreaterThanOrEqual',
'NXP\Classes\Token\TokenGreaterThan',
'NXP\Classes\Token\TokenLessThanOrEqual',
'NXP\Classes\Token\TokenLessThan',
];
}
protected function defaultFunctions()
{
return [
'sin' => function ($arg) {
return sin($arg);
},
'cos' => function ($arg) {
return cos($arg);
},
'tn' => function ($arg) {
return tan($arg);
},
'asin' => function ($arg) {
return asin($arg);
},
'acos' => function ($arg) {
return acos($arg);
},
'atn' => function ($arg) {
return atan($arg);
},
'min' => function ($arg1, $arg2) {
return min($arg1, $arg2);
},
'max' => function ($arg1, $arg2) {
return max($arg1, $arg2);
},
'avg' => function ($arg1, $arg2) {
return ($arg1 + $arg2) / 2;
},
'if' => function ($expr, $trueval, $falseval) {
if ($expr === true || $expr === false) {
$exres = $expr;
} else {
$exres = $this->execute($expr);
}
if ($exres) {
return $this->execute($trueval);
} else {
return $this->execute($falseval);
}
}
];
}
protected function defaultVars()
{
return [
'pi' => 3.14159265359,
'e' => 2.71828182846
];
}
}
|