diff options
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r-- | src/NXP/MathExecutor.php | 138 |
1 files changed, 118 insertions, 20 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index b7abcad..a774561 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -16,8 +16,8 @@ use NXP\Classes\CustomFunction; use NXP\Classes\Operator; use NXP\Classes\Tokenizer; use NXP\Exception\DivisionByZeroException; -use NXP\Exception\UnknownVariableException; use NXP\Exception\MathExecutorException; +use NXP\Exception\UnknownVariableException; use ReflectionException; /** @@ -85,28 +85,42 @@ class MathExecutor protected function defaultOperators() : array { return [ - '+' => [ + '+' => [ function ($a, $b) { return $a + $b; }, 170, false ], - '-' => [ + '-' => [ function ($a, $b) { return $a - $b; }, 170, false ], - '*' => [ + 'uPos' => [ // unary positive token + function ($a) { + return $a; + }, + 200, + false + ], + 'uNeg' => [ // unary minus token + function ($a) { + return 0 - $a; + }, + 200, + false + ], + '*' => [ function ($a, $b) { return $a * $b; }, 180, false ], - '/' => [ + '/' => [ function ($a, $b) { if ($b == 0) { throw new DivisionByZeroException(); @@ -116,28 +130,28 @@ class MathExecutor 180, false ], - '^' => [ + '^' => [ function ($a, $b) { return pow($a, $b); }, 220, true ], - '&&' => [ + '&&' => [ function ($a, $b) { return $a && $b; }, 100, false ], - '||' => [ + '||' => [ function ($a, $b) { return $a || $b; }, 90, false ], - '==' => [ + '==' => [ function ($a, $b) { if (is_string($a) || is_string($b)) { return strcmp($a, $b) == 0; @@ -148,7 +162,7 @@ class MathExecutor 140, false ], - '!=' => [ + '!=' => [ function ($a, $b) { if (is_string($a) || is_string($b)) { return strcmp($a, $b) != 0; @@ -159,28 +173,28 @@ class MathExecutor 140, false ], - '>=' => [ + '>=' => [ function ($a, $b) { return $a >= $b; }, 150, false ], - '>' => [ + '>' => [ function ($a, $b) { return $a > $b; }, 150, false ], - '<=' => [ + '<=' => [ function ($a, $b) { return $a <= $b; }, 150, false ], - '<' => [ + '<' => [ function ($a, $b) { return $a < $b; }, @@ -220,6 +234,36 @@ class MathExecutor 'acosh' => function ($arg) { return acosh($arg); }, + 'arcsin' => function ($arg) { + return asin($arg); + }, + 'arcctg' => function ($arg) { + return M_PI/2 - atan($arg); + }, + 'arccot' => function ($arg) { + return M_PI/2 - atan($arg); + }, + 'arccotan' => function ($arg) { + return M_PI/2 - atan($arg); + }, + 'arcsec' => function ($arg) { + return acos(1/$arg); + }, + 'arccosec' => function ($arg) { + return asin(1/$arg); + }, + 'arccsc' => function ($arg) { + return asin(1/$arg); + }, + 'arccos' => function ($arg) { + return acos($arg); + }, + 'arctan' => function ($arg) { + return atan($arg); + }, + 'arctg' => function ($arg) { + return atan($arg); + }, 'asin' => function ($arg) { return asin($arg); }, @@ -247,9 +291,30 @@ class MathExecutor 'cos' => function ($arg) { return cos($arg); }, + 'cosec' => function ($arg) { + return 1 / sin($arg); + }, + 'csc' => function ($arg) { + return 1 / sin($arg); + }, 'cosh' => function ($arg) { return cosh($arg); }, + 'ctg' => function ($arg) { + return cos($arg) / sin($arg); + }, + 'cot' => function ($arg) { + return cos($arg) / sin($arg); + }, + 'cotan' => function ($arg) { + return cos($arg) / sin($arg); + }, + 'cotg' => function ($arg) { + return cos($arg) / sin($arg); + }, + 'ctn' => function ($arg) { + return cos($arg) / sin($arg); + }, 'decbin' => function ($arg) { return decbin($arg); }, @@ -295,6 +360,12 @@ class MathExecutor 'intdiv' => function ($arg1, $arg2) { return intdiv($arg1, $arg2); }, + 'ln' => function ($arg) { + return log($arg); + }, + 'lg' => function ($arg) { + return log10($arg); + }, 'log' => function ($arg) { return log($arg); }, @@ -317,7 +388,7 @@ class MathExecutor return pi(); }, 'pow' => function ($arg1, $arg2) { - return pow($arg1, $arg2); + return $arg1 ** $arg2; }, 'rad2deg' => function ($arg) { return rad2deg($arg); @@ -331,6 +402,9 @@ class MathExecutor 'sinh' => function ($arg) { return sinh($arg); }, + 'sec' => function ($arg) { + return 1 / cos($arg); + }, 'sqrt' => function ($arg) { return sqrt($arg); }, @@ -342,6 +416,9 @@ class MathExecutor }, 'tn' => function ($arg) { return tan($arg); + }, + 'tg' => function ($arg) { + return tan($arg); } ]; } @@ -349,22 +426,26 @@ class MathExecutor /** * Execute expression * - * @param $expression + * @param string $expression + * @param bool $cache * @return number - * @throws Exception\IncorrectExpressionException * @throws Exception\IncorrectBracketsException + * @throws Exception\IncorrectExpressionException * @throws Exception\UnknownOperatorException - * @throws Exception\UnknownVariableException + * @throws UnknownVariableException */ - public function execute(string $expression) + public function execute(string $expression, bool $cache = true) { $cachekey = $expression; if (!array_key_exists($cachekey, $this->cache)) { $tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation(); - $this->cache[$cachekey] = $tokens; + if ($cache) { + $this->cache[$cachekey] = $tokens; + } } else { $tokens = $this->cache[$cachekey]; } + $calculator = new Calculator($this->functions, $this->operators); return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound); } @@ -533,6 +614,23 @@ class MathExecutor return $this; } + /** + * Get cache array with tokens + * @return array + */ + public function getCache() : array + { + return $this->cache; + } + + /** + * Clear token's cache + */ + public function clearCache() : void + { + $this->cache = []; + } + public function __clone() { $this->addDefaults(); |