ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
EvalMath Class Reference
+ Collaboration diagram for EvalMath:

Public Member Functions

 __construct ()
 
 e ($expr)
 
 evaluate ($expr)
 
 vars ()
 
 funcs ()
 
 nfx ($expr)
 
 pfx ($tokens, $vars=array())
 
 trigger ($msg)
 
 from_hexbin ($token)
 

Data Fields

 $suppress_errors = false
 
 $last_error = null
 
 $v = array('e' => 2.71,'pi' => 3.14)
 
 $f = array()
 
 $vb = array('e', 'pi')
 
 $fb
 

Detailed Description

Definition at line 89 of file class.EvalMath.php.

Constructor & Destructor Documentation

◆ __construct()

EvalMath::__construct ( )

Definition at line 103 of file class.EvalMath.php.

104  {
105  // mjansen-patch: end
106  // make the variables a little more accurate
107  $this->v['pi'] = pi();
108  $this->v['exp'] = exp(1);
109  // PATCH BEGIN
110  $this->v['e'] = exp(1); // different result for exp(1) and e
111  $this->fb[] = 'exp'; // usage of php exp function in formula
112  // PATCH END
113  }

Member Function Documentation

◆ e()

EvalMath::e (   $expr)

Definition at line 115 of file class.EvalMath.php.

References evaluate().

116  {
117  return $this->evaluate($expr);
118  }
evaluate($expr)
+ Here is the call graph for this function:

◆ evaluate()

EvalMath::evaluate (   $expr)

Definition at line 120 of file class.EvalMath.php.

References $i, PHPMailer\PHPMailer\$token, nfx(), pfx(), and trigger().

Referenced by e().

121  {
122  // convert exponential notation
123  $expr = preg_replace_callback(
124  "/(\\d{0,1})e(-{0,1}\\d+)/is",
125  function ($hit) {
126  return $hit[1] . ((strlen($hit[1])) ? '*' : '') . '10^(' . $hit[2] . ')';
127  },
128  $expr
129  );
130  // standard functionality
131  $this->last_error = null;
132  $expr = trim($expr);
133  if (substr($expr, -1, 1) == ';') {
134  $expr = substr($expr, 0, strlen($expr) - 1);
135  } // strip semicolons at the end
136  //===============
137  // is it a variable assignment?
138  if (preg_match('/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) {
139  if (in_array($matches[1], $this->vb)) { // make sure we're not assigning to a constant
140  return $this->trigger("cannot assign to constant '$matches[1]'");
141  }
142  if (($tmp = $this->pfx($this->nfx($matches[2]))) === false) {
143  return false;
144  } // get the result and make sure it's good
145  $this->v[$matches[1]] = $tmp; // if so, stick it in the variable array
146  return $this->v[$matches[1]]; // and return the resulting value
147  //===============
148  // is it a function assignment?
149  } elseif (preg_match('/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches)) {
150  $fnn = $matches[1]; // get the function name
151  if (in_array($matches[1], $this->fb)) { // make sure it isn't built in
152  return $this->trigger("cannot redefine built-in function '$matches[1]()'");
153  }
154  $args = explode(",", preg_replace("/\s+/", "", $matches[2])); // get the arguments
155  if (($stack = $this->nfx($matches[3])) === false) {
156  return false;
157  } // see if it can be converted to postfix
158  for ($i = 0; $i < count($stack); $i++) { // freeze the state of the non-argument variables
159  $token = $stack[$i];
160  if (preg_match('/^[a-z]\w*$/', $token) and !in_array($token, $args)) {
161  if (array_key_exists($token, $this->v)) {
162  $stack[$i] = $this->v[$token];
163  } else {
164  return $this->trigger("undefined variable '$token' in function definition");
165  }
166  }
167  }
168  $this->f[$fnn] = array('args' => $args, 'func' => $stack);
169  return true;
170  //===============
171  } else {
172  return $this->pfx($this->nfx($expr)); // straight up evaluation, woo
173  }
174  }
pfx($tokens, $vars=array())
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ from_hexbin()

EvalMath::from_hexbin (   $token)

Definition at line 429 of file class.EvalMath.php.

References PHPMailer\PHPMailer\$token.

Referenced by pfx().

430  {
431  if (strtoupper(substr($token, -1, 1)) == 'H') {
432  return hexdec($token);
433  }
434  if (strtoupper(substr($token, -1, 1)) == 'B') {
435  return bindec($token);
436  }
437  return false;
438  }
+ Here is the caller graph for this function:

◆ funcs()

EvalMath::funcs ( )

Definition at line 184 of file class.EvalMath.php.

References Sabre\VObject\$output.

185  {
186  $output = array();
187  foreach ($this->f as $fnn => $dat) {
188  $output[] = $fnn . '(' . implode(',', $dat['args']) . ')';
189  }
190  return $output;
191  }

◆ nfx()

EvalMath::nfx (   $expr)

Definition at line 196 of file class.EvalMath.php.

References $index, Sabre\VObject\$output, and trigger().

Referenced by evaluate().

197  {
198  $index = 0;
199  $stack = new EvalMathStack;
200  $output = array(); // postfix form of expression, to be passed to pfx()
201  $expr = trim(strtolower($expr));
202 
203  $ops = array('+', '-', '*', '/', '^', '_');
204  $ops_r = array('+' => 0,'-' => 0,'*' => 0,'/' => 0,'^' => 1); // right-associative operator?
205  $ops_p = array('+' => 0,'-' => 0,'*' => 1,'/' => 1,'_' => 1,'^' => 2); // operator precedence
206 
207  $expecting_op = false; // we use this in syntax-checking the expression
208  // and determining when a - is a negation
209 
210  if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches)) { // make sure the characters are all good
211  return $this->trigger("illegal character '{$matches[0]}'");
212  }
213 
214  while (1) { // 1 Infinite Loop ;)
215  $op = substr($expr, $index, 1); // get the first character at the current index
216  // find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
217  $ex = preg_match('/^([01]+[bB]|[\da-fA-F]+[hH]|[a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
218  //===============
219  if ($op == '-' and !$expecting_op) { // is it a negation instead of a minus?
220  $stack->push('_'); // put a negation on the stack
221  $index++;
222  } elseif ($op == '_') { // we have to explicitly deny this, because it's legal on the stack
223  return $this->trigger("illegal character '_'"); // but not in the input expression
224  //===============
225  } elseif ((in_array($op, $ops) or $ex) and $expecting_op) { // are we putting an operator on the stack?
226  if ($ex) { // are we expecting an operator but have a number/variable/function/opening parethesis?
227  $op = '*';
228  $index--; // it's an implicit multiplication
229  }
230  // heart of the algorithm:
231  while ($stack->count > 0 and ($o2 = $stack->last()) and in_array($o2, $ops) and ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2])) {
232  $output[] = $stack->pop(); // pop stuff off the stack into the output
233  }
234  // many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
235  $stack->push($op); // finally put OUR operator onto the stack
236  $index++;
237  $expecting_op = false;
238  //===============
239  } elseif ($op == ')' and $expecting_op) { // ready to close a parenthesis?
240  while (($o2 = $stack->pop()) != '(') { // pop off the stack back to the last (
241  if (is_null($o2)) {
242  return $this->trigger("unexpected ')'");
243  } else {
244  $output[] = $o2;
245  }
246  }
247  if (preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) { // did we just close a function?
248  $fnn = $matches[1]; // get the function name
249  $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
250  $output[] = $stack->pop(); // pop the function and push onto the output
251  if (in_array($fnn, $this->fb)) { // check the argument count
252  if ($arg_count > 1) {
253  return $this->trigger("too many arguments ($arg_count given, 1 expected)");
254  }
255  } elseif (array_key_exists($fnn, $this->f)) {
256  if ($arg_count != count($this->f[$fnn]['args'])) {
257  return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)");
258  }
259  } else { // did we somehow push a non-function on the stack? this should never happen
260  return $this->trigger("internal error");
261  }
262  }
263  $index++;
264  //===============
265  } elseif ($op == ',' and $expecting_op) { // did we just finish a function argument?
266  while (($o2 = $stack->pop()) != '(') {
267  if (is_null($o2)) {
268  return $this->trigger("unexpected ','");
269  } // oops, never had a (
270  else {
271  $output[] = $o2;
272  } // pop the argument expression stuff and push onto the output
273  }
274  // make sure there was a function
275  if (!preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
276  return $this->trigger("unexpected ','");
277  }
278  $stack->push($stack->pop() + 1); // increment the argument count
279  $stack->push('('); // put the ( back on, we'll need to pop back to it again
280  $index++;
281  $expecting_op = false;
282  //===============
283  } elseif ($op == '(' and !$expecting_op) {
284  $stack->push('('); // that was easy
285  $index++;
286  $allow_neg = true;
287  //===============
288  } elseif ($ex and !$expecting_op) { // do we now have a function/variable/number?
289  $expecting_op = true;
290  $val = $match[1];
291  if (preg_match("/^([a-z]\w*)\($/", $val, $matches)) { // may be func, or variable w/ implicit multiplication against parentheses...
292  if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) { // it's a func
293  $stack->push($val);
294  $stack->push(1);
295  $stack->push('(');
296  $expecting_op = false;
297  } else { // it's a var w/ implicit multiplication
298  $val = $matches[1];
299  $output[] = $val;
300  }
301  } else { // it's a plain old var or num
302  $output[] = $val;
303  }
304  $index += strlen($val);
305  //===============
306  } elseif ($op == ')') { // miscellaneous error checking
307  return $this->trigger("unexpected ')'");
308  } elseif (in_array($op, $ops) and !$expecting_op) {
309  return $this->trigger("unexpected operator '$op'");
310  } else { // I don't even want to know what you did to get here
311  return $this->trigger("an unexpected error occured");
312  }
313  if ($index == strlen($expr)) {
314  if (in_array($op, $ops)) { // did we end with an operator? bad.
315  return $this->trigger("operator '$op' lacks operand");
316  } else {
317  break;
318  }
319  }
320  while (substr($expr, $index, 1) == ' ') { // step the index past whitespace (pretty much turns whitespace
321  $index++; // into implicit multiplication if no operator is there)
322  }
323  }
324  while (!is_null($op = $stack->pop())) { // pop everything off the stack and push onto output
325  if ($op == '(') {
326  return $this->trigger("expecting ')'");
327  } // if there are (s on the stack, ()s were unbalanced
328  $output[] = $op;
329  }
330  return $output;
331  }
$index
Definition: metadata.php:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pfx()

EvalMath::pfx (   $tokens,
  $vars = array() 
)

Definition at line 334 of file class.EvalMath.php.

References $i, PHPMailer\PHPMailer\$token, ilMath\_add(), ilMath\_div(), ilMath\_mul(), ilMath\_pow(), ilMath\_sub(), from_hexbin(), and trigger().

Referenced by evaluate().

335  {
336  if ($tokens == false) {
337  return false;
338  }
339 
340  $stack = new EvalMathStack;
341 
342  foreach ($tokens as $token) { // nice and easy
343  // if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
344  if (in_array($token, array('+', '-', '*', '/', '^'))) {
345  if (is_null($op2 = $stack->pop())) {
346  return $this->trigger("internal error");
347  }
348  if (is_null($op1 = $stack->pop())) {
349  return $this->trigger("internal error");
350  }
351  include_once "class.ilMath.php";
352  switch ($token) {
353  case '+':
354  $stack->push(ilMath::_add($op1, $op2)); break;
355  case '-':
356  $stack->push(ilMath::_sub($op1, $op2)); break;
357  case '*':
358  $stack->push(ilMath::_mul($op1, $op2)); break;
359  case '/':
360  if ($op2 == 0) {
361  return $this->trigger("division by zero");
362  }
363  $stack->push(ilMath::_div($op1, $op2)); break;
364  case '^':
365  $stack->push(ilMath::_pow($op1, $op2)); break;
366  }
367  // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
368  } elseif ($token == "_") {
369  $stack->push(-1 * $stack->pop());
370  // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
371  } elseif (preg_match("/^([a-z]\w*)\($/", $token, $matches)) { // it's a function!
372  $fnn = $matches[1];
373  if (in_array($fnn, $this->fb)) { // built-in function:
374  if (is_null($op1 = $stack->pop())) {
375  return $this->trigger("internal error");
376  }
377  $fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms
378  if ($fnn == 'log') {
379  $fnn = 'log10';
380  } elseif ($fnn == 'ln') {
381  $fnn = 'log';
382  }
383 
384  $stack->push($fnn($op1)); // 'eval()' can be easily avoided here
385  } elseif (array_key_exists($fnn, $this->f)) { // user function
386  // get args
387  $args = array();
388  for ($i = count($this->f[$fnn]['args']) - 1; $i >= 0; $i--) {
389  if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack->pop())) {
390  return $this->trigger("internal error");
391  }
392  }
393  $stack->push($this->pfx($this->f[$fnn]['func'], $args)); // yay... recursion!!!!
394  }
395  // if the token is a number or variable, push it on the stack
396  } else {
397  if (is_numeric($token)) {
398  $stack->push($token);
399  } elseif (($hex = $this->from_hexbin($token)) !== false) {
400  $stack->push($hex);
401  } elseif (array_key_exists($token, $this->v)) {
402  $stack->push($this->v[$token]);
403  } elseif (array_key_exists($token, $vars)) {
404  $stack->push($vars[$token]);
405  } else {
406  return $this->trigger("undefined variable '$token'");
407  }
408  }
409  }
410  // when we're out of tokens, the stack should have a single element, the final result
411  if ($stack->count != 1) {
412  return $this->trigger("internal error");
413  }
414  return $stack->pop();
415  }
from_hexbin($token)
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)
pfx($tokens, $vars=array())
static _mul($left_operand, $right_operand, $scale=50)
$i
Definition: disco.tpl.php:19
static _sub($left_operand, $right_operand, $scale=50)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trigger()

EvalMath::trigger (   $msg)

Definition at line 418 of file class.EvalMath.php.

Referenced by evaluate(), nfx(), and pfx().

419  {
420  $this->last_error = $msg;
421  if (!$this->suppress_errors) {
422  trigger_error($msg, E_USER_WARNING);
423  }
424  return false;
425  }
+ Here is the caller graph for this function:

◆ vars()

EvalMath::vars ( )

Definition at line 176 of file class.EvalMath.php.

References Sabre\VObject\$output, and $v.

177  {
178  $output = $this->v;
179  unset($output['pi']);
180  unset($output['e']);
181  return $output;
182  }

Field Documentation

◆ $f

EvalMath::$f = array()

Definition at line 95 of file class.EvalMath.php.

◆ $fb

EvalMath::$fb
Initial value:
= array(
'sin','sinh','arcsin','asin','arcsinh','asinh',
'cos','cosh','arccos','acos','arccosh','acosh',
'tan','tanh','arctan','atan','arctanh','atanh',
'sqrt','abs','ln','log')

Definition at line 97 of file class.EvalMath.php.

◆ $last_error

EvalMath::$last_error = null

Definition at line 92 of file class.EvalMath.php.

◆ $suppress_errors

EvalMath::$suppress_errors = false

Definition at line 91 of file class.EvalMath.php.

◆ $v

EvalMath::$v = array('e' => 2.71,'pi' => 3.14)

Definition at line 94 of file class.EvalMath.php.

Referenced by vars().

◆ $vb

EvalMath::$vb = array('e', 'pi')

Definition at line 96 of file class.EvalMath.php.


The documentation for this class was generated from the following file: