예제 #1
0
 /**
  * Print a ConstantNode.
  *
  * @param ConstantNode $node
  */
 public function visitConstantNode(ConstantNode $node)
 {
     return $node->getName();
 }
예제 #2
0
 /**
  * Evaluate a ConstantNode
  *
  * Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.
  *
  * @see \MathParser\Lexer\StdMathLexer StdMathLexer
  * @see \MathParser\StdMathParser StdMathParser
  * @throws UnknownConstantException if the variable respresented by the
  *      ConstantNode is *not* recognized.
  *
  * @param ConstantNode $node AST to be evaluated
  * @retval float
  */
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
             return new Complex(M_PI, 0);
         case 'e':
             return new Complex(M_E, 0);
         case 'i':
             return new Complex(0, 1);
         default:
             throw new UnknownConstantException($node->getName());
     }
 }
예제 #3
0
 /**
  * Evaluate a ConstantNode
  *
  * Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.
  *
  * @see \MathParser\Lexer\StdMathLexer StdMathLexer
  * @see \MathParser\StdMathParser StdMathParser
  * @throws UnknownConstantException if the variable respresented by the
  *      ConstantNode is *not* recognized.
  *
  * @param ConstantNode $node AST to be evaluated
  * @retval float
  */
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
             return M_PI;
         case 'e':
             return exp(1);
         default:
             throw new UnknownConstantException($node->getName());
     }
 }
예제 #4
0
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
             return 'pi';
         case 'e':
             return 'e';
         case 'i':
             return 'i';
         case 'NAN':
             return 'NAN';
         case 'INF':
             return 'INF';
         default:
             throw new UnknownConstantException($node->getName());
     }
 }
예제 #5
0
 /**
  * Differentiate a ConstantNode
  *
  * Create a NumberNode representing '0'. (The derivative of
  * a constant is indentically 0).
  *
  * @param ConstantNode $node AST to be differentiated
  * @retval Node
  */
 public function visitConstantNode(ConstantNode $node)
 {
     if ($node->getName() == 'NAN') {
         return $node;
     }
     return new IntegerNode(0);
 }
예제 #6
0
 /**
  * Evaluate a ConstantNode
  *
  * Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.
  *
  * @see \MathParser\Lexer\StdMathLexer StdMathLexer
  * @see \MathParser\StdMathParser StdMathParser
  * @throws UnknownConstantException if the variable respresented by the
  *      ConstantNode is *not* recognized.
  *
  * @param ConstantNode $node AST to be evaluated
  * @retval float
  */
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
         case 'e':
             throw new \UnexpectedValueException();
         default:
             throw new UnknownConstantException($node->getName());
     }
 }
예제 #7
0
 /**
  * Generate LaTeX code for a ConstantNode
  *
  * Create a string giving LaTeX code for a ConstantNode.
  * `pi` typesets as `\pi` and `e` simply as `e`.
  *
  * @throws UnknownConstantException for nodes representing other constants.
  * @param ConstantNode $node AST to be typeset
  * @retval string
  */
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
             return '\\pi{}';
         case 'e':
             return 'e';
         case 'i':
             return 'i';
         case 'NAN':
             return '\\operatorname{NAN}';
         case 'INF':
             return '\\infty{}';
         default:
             throw new UnknownConstantException($node->getName());
     }
 }
예제 #8
0
 public function testCanPrintConstant()
 {
     $this->assertResult('pi', 'pi');
     $this->assertResult('e', 'e');
     $node = new ConstantNode('xcv');
     $this->setExpectedException(UnknownConstantException::class);
     $node->accept($this->printer);
 }
예제 #9
0
 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);
 }
예제 #10
0
 /**
  * Evaluate a ConstantNode
  *
  * Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.
  *
  * @see \MathParser\Lexer\StdMathLexer StdMathLexer
  * @see \MathParser\StdMathParser StdMathParser
  * @throws UnknownConstantException if the variable respresented by the
  *      ConstantNode is *not* recognized.
  *
  * @param ConstantNode $node AST to be evaluated
  * @retval float
  */
 public function visitConstantNode(ConstantNode $node)
 {
     switch ($node->getName()) {
         case 'pi':
         case 'e':
         case 'i':
         case 'NAN':
         case 'INF':
             throw new \UnexpectedValueException("Expecting rational number");
         default:
             throw new UnknownConstantException($node->getName());
     }
 }