ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
EvalMath Class Reference
+ Collaboration diagram for EvalMath:

Public Member Functions

 EvalMath ()
 
 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.

Member Function Documentation

◆ e()

EvalMath::e (   $expr)

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

113 {
114 return $this->evaluate($expr);
115 }
evaluate($expr)

References evaluate().

+ Here is the call graph for this function:

◆ EvalMath()

EvalMath::EvalMath ( )

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

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

References v().

+ Here is the call graph for this function:

◆ evaluate()

EvalMath::evaluate (   $expr)

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

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

References nfx(), pfx(), trigger(), and v().

Referenced by e().

+ 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 387 of file class.EvalMath.php.

387 {
388 if (strtoupper(substr($token, -1, 1))=='H') return hexdec($token);
389 if (strtoupper(substr($token, -1, 1))=='B') return bindec($token);
390 return FALSE;
391 }

Referenced by pfx().

+ Here is the caller graph for this function:

◆ funcs()

EvalMath::funcs ( )

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

173 {
174 $output = array();
175 foreach ($this->f as $fnn=>$dat)
176 $output[] = $fnn . '(' . implode(',', $dat['args']) . ')';
177 return $output;
178 }

◆ nfx()

EvalMath::nfx (   $expr)

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

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

References trigger().

Referenced by evaluate().

+ 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 309 of file class.EvalMath.php.

309 {
310
311 if ($tokens == false) return false;
312
313 $stack = new EvalMathStack;
314
315 foreach ($tokens as $token) { // nice and easy
316 // if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
317 if (in_array($token, array('+', '-', '*', '/', '^'))) {
318 if (is_null($op2 = $stack->pop())) return $this->trigger("internal error");
319 if (is_null($op1 = $stack->pop())) return $this->trigger("internal error");
320 include_once "class.ilMath.php";
321 switch ($token) {
322 case '+':
323 $stack->push(ilMath::_add($op1,$op2)); break;
324 case '-':
325 $stack->push(ilMath::_sub($op1,$op2)); break;
326 case '*':
327 $stack->push(ilMath::_mul($op1,$op2)); break;
328 case '/':
329 if ($op2 == 0) return $this->trigger("division by zero");
330 $stack->push(ilMath::_div($op1,$op2)); break;
331 case '^':
332 $stack->push(ilMath::_pow($op1,$op2)); break;
333 }
334 // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
335 } elseif ($token == "_") {
336 $stack->push(-1*$stack->pop());
337 // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
338 } elseif (preg_match("/^([a-z]\w*)\‍($/", $token, $matches)) { // it's a function!
339 $fnn = $matches[1];
340 if (in_array($fnn, $this->fb)) { // built-in function:
341 if (is_null($op1 = $stack->pop())) return $this->trigger("internal error");
342 $fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms
343 if ($fnn == 'log') {
344 $fnn = 'log10';
345 } elseif ($fnn == 'ln') {
346 $fnn = 'log';
347 }
348
349 $stack->push($fnn($op1)); // 'eval()' can be easily avoided here
350 } elseif (array_key_exists($fnn, $this->f)) { // user function
351 // get args
352 $args = array();
353 for ($i = count($this->f[$fnn]['args'])-1; $i >= 0; $i--) {
354 if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack->pop())) return $this->trigger("internal error");
355 }
356 $stack->push($this->pfx($this->f[$fnn]['func'], $args)); // yay... recursion!!!!
357 }
358 // if the token is a number or variable, push it on the stack
359 } else {
360 if (is_numeric($token)) {
361 $stack->push($token);
362 } elseif (($hex=$this->from_hexbin($token))!==FALSE) {
363 $stack->push($hex);
364 } elseif (array_key_exists($token, $this->v)) {
365 $stack->push($this->v[$token]);
366 } elseif (array_key_exists($token, $vars)) {
367 $stack->push($vars[$token]);
368 } else {
369 return $this->trigger("undefined variable '$token'");
370 }
371 }
372 }
373 // when we're out of tokens, the stack should have a single element, the final result
374 if ($stack->count != 1) return $this->trigger("internal error");
375 return $stack->pop();
376 }
from_hexbin($token)
static _mul($left_operand, $right_operand, $scale=50)
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)

References ilMath\_add(), ilMath\_div(), ilMath\_mul(), ilMath\_pow(), ilMath\_sub(), from_hexbin(), pfx(), trigger(), and v().

Referenced by evaluate(), and pfx().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trigger()

EvalMath::trigger (   $msg)

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

379 {
380 $this->last_error = $msg;
381 if (!$this->suppress_errors) trigger_error($msg, E_USER_WARNING);
382 return false;
383 }

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

+ Here is the caller graph for this function:

◆ vars()

EvalMath::vars ( )

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

166 {
167 $output = $this->v;
168 unset($output['pi']);
169 unset($output['e']);
170 return $output;
171 }

References $v.

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: