Exemplo n.º 1
0
 public function testCanEvaluateAdditiion()
 {
     $x = $this->variables['x'];
     $this->assertResult('3+x', Complex::add(3, $x));
     $this->assertResult('3+x+1', Complex::add(4, $x));
 }
Exemplo n.º 2
0
 /**
  * Evaluate an ExpressionNode
  *
  * Computes the value of an ExpressionNode `x op y`
  * where `op` is one of `+`, `-`, `*`, `/` or `^`
  *
  * @throws UnknownOperatorException if the operator is something other than
  *      `+`, `-`, `*`, `/` or `^`
  *
  * @param ExpressionNode $node AST to be evaluated
  * @retval float
  */
 public function visitExpressionNode(ExpressionNode $node)
 {
     $operator = $node->getOperator();
     $a = $node->getLeft()->accept($this);
     if ($node->getRight()) {
         $b = $node->getRight()->accept($this);
     } else {
         $b = null;
     }
     // Perform the right operation based on the operator
     switch ($operator) {
         case '+':
             return Complex::add($a, $b);
         case '-':
             if ($b === null) {
                 return Complex::mul($a, -1);
             }
             return Complex::sub($a, $b);
         case '*':
             return Complex::mul($a, $b);
         case '/':
             return Complex::div($a, $b);
         case '^':
             // This needs to be improved.
             return Complex::pow($a, $b);
         default:
             throw new UnknownOperatorException($operator);
     }
 }
Exemplo n.º 3
0
 public function testCanDoAritmethic()
 {
     $z = new Complex(1, 2);
     $w = new Complex(2, -1);
     $this->assertEquals(new Complex(3, 1), Complex::add($z, $w));
     $this->assertEquals(new Complex(-1, 3), Complex::sub($z, $w));
     $this->assertEquals(new Complex(4, 3), Complex::mul($z, $w));
     $this->assertEquals(new Complex(0, 1), Complex::div($z, $w));
 }