function polynomial(expr, scope, extended, rules) {
    var variables = [];
    var node = simplify(expr, rules, scope, {
      exactFractions: false
    }); // Resolves any variables and functions with all defined parameters

    extended = !!extended;
    var oper = '+-*' + (extended ? '/' : '');
    recPoly(node);
    var retFunc = {};
    retFunc.expression = node;
    retFunc.variables = variables;
    return retFunc; // -------------------------------------------------------------------------------------------------------

    /**
     *  Function to simplify an expression using an optional scope and
     *  return it if the expression is a polynomial expression, i.e.
     *  an expression with one or more variables and the operators
     *  +, -, *, and ^, where the exponent can only be a positive integer.
     *
     * Syntax:
     *
     *     recPoly(node)
     *
     *
     * @param  {Node} node               The current sub tree expression in recursion
     *
     * @return                           nothing, throw an exception if error
     */

    function recPoly(node) {
      var tp = node.type; // node type

      if (tp === 'FunctionNode') {
        // No function call in polynomial expression
        throw new Error('There is an unsolved function call');
      } else if (tp === 'OperatorNode') {
        if (node.op === '^') {
          if (node.args[1].fn === 'unaryMinus') {
            node = node.args[0];
          }

          if (node.args[1].type !== 'ConstantNode' || !number.isInteger(parseFloat(node.args[1].value))) {
            throw new Error('There is a non-integer exponent');
          } else {
            recPoly(node.args[0]);
          }
        } else {
          if (oper.indexOf(node.op) === -1) {
            throw new Error('Operator ' + node.op + ' invalid in polynomial expression');
          }

          for (var i = 0; i < node.args.length; i++) {
            recPoly(node.args[i]);
          }
        } // type of operator

      } else if (tp === 'SymbolNode') {
        var name = node.name; // variable name

        var pos = variables.indexOf(name);

        if (pos === -1) {
          // new variable in expression
          variables.push(name);
        }
      } else if (tp === 'ParenthesisNode') {
        recPoly(node.content);
      } else if (tp !== 'ConstantNode') {
        throw new Error('type ' + tp + ' is not allowed in polynomial expression');
      }
    } // end of recPoly

  }
Function to simplify an expression using an optional scope and
 return it if the expression is a polynomial expression, i.e.
 an expression with one or more variables and the operators
 +, -, *, and ^, where the exponent can only be a positive integer.

Syntax:

    polynomial(expr,scope,extended, rules)

@param  {Node | string} expr     The expression to simplify and check if is polynomial expression
@param  {object} scope           Optional scope for expression simplification
@param  {boolean} extended       Optional. Default is false. When true allows divide operator.
@param  {array}  rules           Optional. Default is no rule.


@return {Object}
           {Object} node:   node simplified expression
           {Array}  variables:  variable names