Exemplo n.º 1
0
 public function testCanEvaluateExponentiation()
 {
     $x = $this->variables['x'];
     $this->assertResult('x^3', Complex::pow($x, 3));
     $this->assertResult('x^x^x', Complex::pow($x, Complex::pow($x, $x)));
     $this->assertResult('(-1)^(-1)', Complex::parse(-1));
 }
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 testCanComputePowers()
 {
     $z = new Complex(1, 2);
     $this->assertEquals(new Complex(-3, 4), Complex::pow($z, 2));
     $this->assertEquals(new Complex(-11, -2), Complex::pow($z, 3));
     $this->assertEquals(new Complex(1 / 5, -2 / 5), Complex::pow($z, -1));
     $this->assertEquals(new Complex(0.229140186, 0.2381701151), Complex::pow($z, new Complex(0, 1)));
 }