/** * Print a VariableNode. * * @param VariableNode $node */ public function visitVariableNode(VariableNode $node) { return $node->getName(); }
/** * Differentiate a VariableNode * * Create a NumberNode representing '0' or '1' depending on * the differetiation variable. * * @param NumberNode $node AST to be differentiated * @retval Node */ public function visitVariableNode(VariableNode $node) { if ($node->getName() == $this->variable) { return new IntegerNode(1); } return new IntegerNode(0); }
/** * Evaluate a VariableNode * * Returns the current value of a VariableNode, as defined * either by the constructor or set using the `Evaluator::setVariables()` method. * @see Evaluator::setVariables() to define the variables * @throws UnknownVariableException if the variable respresented by the * VariableNode is *not* set. * * @param VariableNode $node AST to be evaluated * @retval float */ public function visitVariableNode(VariableNode $node) { $name = $node->getName(); if (array_key_exists($name, $this->variables)) { return $this->variables[$name]; } throw new UnknownVariableException($name); }
public function testCanComputeComplexity() { $node = new NumberNode(1); $this->assertEquals($node->complexity(), 2); $node = new IntegerNode(1); $this->assertEquals($node->complexity(), 1); $node = new RationalNode(1, 2); $this->assertEquals($node->complexity(), 2); $node = new VariableNode('x'); $this->assertEquals($node->complexity(), 1); $node = new ConstantNode('pi'); $this->assertEquals($node->complexity(), 1); $f = $this->parser->parse('x+y'); $this->assertEquals($f->complexity(), 4); $f = $this->parser->parse('x-y'); $this->assertEquals($f->complexity(), 4); $f = $this->parser->parse('x*y'); $this->assertEquals($f->complexity(), 4); $f = $this->parser->parse('x/y'); $this->assertEquals($f->complexity(), 6); $f = $this->parser->parse('x^y'); $this->assertEquals($f->complexity(), 10); $f = $this->parser->parse('sin(x)'); $this->assertEquals($f->complexity(), 6); $f = $this->parser->parse('x + sin(x^2)'); $this->assertEquals($f->complexity(), 18); $node = new SubExpressionNode('('); $this->assertEquals($node->complexity(), 1000); }