Esempio n. 1
0
 function testGetVariables()
 {
     $e = new Expression('2*x + y*sin(4*x)');
     $v = $e->getVariables();
     $this->assertEqual(sizeof($v), 2);
     $this->assertEqual($v[0], 'x');
     $this->assertEqual($v[1], 'y');
 }
Esempio n. 2
0
 /**
  * Check that an equation is correct with the variable values in this problem.
  * All variables in the equation must be defined in this problem.
  * @param $str :: An equation as a string.
  * @return :: A string 'true' if the equation is correct and error message otherwise.
  */
 function checkEquation($str)
 {
     $e = new Expression($str);
     if ($e->fun != '=') {
         return 'not equation';
     }
     $vars = $e->getVariables();
     $undef = $this->findUndefinedVariables($vars);
     if (sizeof($undef) > 0) {
         return 'undefined: ' . implode(', ', $undef);
     }
     $lhs = $e->terms[0];
     $rhs = $e->terms[1];
     $fun = $rhs->op;
     $lv = $lhs->eval_double($this->vars);
     $rv = $rhs->eval_double($this->vars);
     $res = 'oops';
     if ($fun == '=') {
         $res = $lv == $rv ? 'true' : 'false';
     } else {
         if ($fun == '>') {
             $res = $lv > $rv ? 'true' : 'false';
         } else {
             if ($fun == '<') {
                 $res = $lv < $rv ? 'true' : 'false';
             } else {
                 if ($fun == '>=') {
                     $res = $lv >= $rv ? 'true' : 'false';
                 } else {
                     if ($fun == '<=') {
                         $res = $lv <= $rv ? 'true' : 'false';
                     }
                 }
             }
         }
     }
     return $res;
 }