public function complexity()
    {
        if ($this instanceof IntegerNode || $this instanceof VariableNode || $this instanceof ConstantNode) {
            return 1;
        } elseif ($this instanceof RationalNode || $this instanceof NumberNode) {
            return 2;
        } elseif ($this instanceof FunctionNode) {
            return 5 + $this->getOperand()->complexity();
        } elseif ($this instanceof ExpressionNode) {
            $operator = $this->getOperator();
            $left = $this->getLeft();
            $right = $this->getRight();
            switch ($operator) {
                case '+':
                case '-':
                case '*':
                    return 1 + $left->complexity() + (($right === null) ? 0 : $right->complexity());

                case '/':
                    return 3 + $left->complexity() + (($right === null) ? 0 : $right->complexity());

                case '^':
                    return 8 + $left->complexity() + (($right === null) ? 0 : $right->complexity());
            }
        }
        // This shouldn't happen under normal circumstances

        return 1000;
    }
public function complexity()
    {
        if ($this instanceof IntegerNode || $this instanceof VariableNode || $this instanceof ConstantNode) {
            return 1;
        } elseif ($this instanceof RationalNode || $this instanceof NumberNode) {
            return 2;
        } elseif ($this instanceof FunctionNode) {
            return 5 + $this->getOperand()->complexity();
        } elseif ($this instanceof ExpressionNode) {
            $operator = $this->getOperator();
            $left = $this->getLeft();
            $right = $this->getRight();
            switch ($operator) {
                case '+':
                case '-':
                case '*':
                    return 1 + $left->complexity() + (($right === null) ? 0 : $right->complexity());

                case '/':
                    return 3 + $left->complexity() + (($right === null) ? 0 : $right->complexity());

                case '^':
                    return 8 + $left->complexity() + (($right === null) ? 0 : $right->complexity());
            }
        }
        // This shouldn't happen under normal circumstances

        return 1000;
    }
public function complexity ( ) { if ( $ this instanceof IntegerNode || $ this instanceof VariableNode || $ this instanceof ConstantNode ) { return 1 ; } elseif ( $ this instanceof RationalNode || $ this instanceof NumberNode ) { return 2 ; } elseif ( $ this instanceof FunctionNode ) { return 5 + $ this -> getOperand ( ) -> complexity ( ) ; } elseif ( $ this instanceof ExpressionNode ) { $ operator = $ this -> getOperator ( ) ; $ left = $ this -> getLeft ( ) ; $ right = $ this -> getRight ( ) ; switch ( $ operator ) { case '+' : case '-' : case '*' : return 1 + $ left -> complexity ( ) + ( ( $ right === null ) ? 0 : $ right -> complexity ( ) ) ; case '/' : return 3 + $ left -> complexity ( ) + ( ( $ right === null ) ? 0 : $ right -> complexity ( ) ) ; case '^' : return 8 + $ left -> complexity ( ) + ( ( $ right === null ) ? 0 : $ right -> complexity ( ) ) ; } } // This shouldn't happen under normal circumstances return 1000 ; }
Rough estimate of the complexity of the AST.

Gives a rough measure of the complexity of an AST. This can
be useful to choose between different simplification rules
or how to print a tree ("e^{...}" or ("\exp(...)") for example.

More precisely, the complexity is computed as the sum of
the complexity of all nodes of the AST, and

NumberNodes, VariableNodes and ConstantNodes have complexity 1,
FunctionNodes have complexity 5 (plus the complexity of its operand),
ExpressionNodes have complexity 1 (for `+`, `-`, `*`), 2 (for `/`),
or 8 (for `^`)
Rough estimate of the complexity of the AST .