* Expand recursively a tree node for handling with expressions with exponents * (it's not for constants, symbols or functions with exponents) * PS: The other parameters are internal for recursion * * Syntax: * * expandPower(node) * * @param {Node} node Current
(node, parent, indParent)
| 386 | * @return {node} node expression with all powers expanded. |
| 387 | */ |
| 388 | function expandPower (node, parent, indParent) { |
| 389 | const tp = node.type |
| 390 | const internal = (arguments.length > 1) // TRUE in internal calls |
| 391 | |
| 392 | if (tp === 'OperatorNode' && node.isBinary()) { |
| 393 | let does = false |
| 394 | let val |
| 395 | if (node.op === '^') { // First operator: Parenthesis or UnaryMinus |
| 396 | if ((node.args[0].type === 'ParenthesisNode' || |
| 397 | node.args[0].type === 'OperatorNode') && |
| 398 | (node.args[1].type === 'ConstantNode')) { // Second operator: Constant |
| 399 | val = parseFloat(node.args[1].value) |
| 400 | does = (val >= 2 && isInteger(val)) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (does) { // Exponent >= 2 |
| 405 | // Before: |
| 406 | // operator A --> Subtree |
| 407 | // parent pow |
| 408 | // constant |
| 409 | // |
| 410 | if (val > 2) { // Exponent > 2, |
| 411 | // AFTER: (exponent > 2) |
| 412 | // operator A --> Subtree |
| 413 | // parent * |
| 414 | // deep clone (operator A --> Subtree |
| 415 | // pow |
| 416 | // constant - 1 |
| 417 | // |
| 418 | const nEsqTopo = node.args[0] |
| 419 | const nDirTopo = new OperatorNode('^', 'pow', [node.args[0].cloneDeep(), new ConstantNode(val - 1)]) |
| 420 | node = new OperatorNode('*', 'multiply', [nEsqTopo, nDirTopo]) |
| 421 | } else { // Expo = 2 - no power |
| 422 | // AFTER: (exponent = 2) |
| 423 | // operator A --> Subtree |
| 424 | // parent oper |
| 425 | // deep clone (operator A --> Subtree) |
| 426 | // |
| 427 | node = new OperatorNode('*', 'multiply', [node.args[0], node.args[0].cloneDeep()]) |
| 428 | } |
| 429 | |
| 430 | if (internal) { |
| 431 | // Change parent references in internal recursive calls |
| 432 | if (indParent === 'content') { parent.content = node } else { parent.args[indParent] = node } |
| 433 | } |
| 434 | } // does |
| 435 | } // binary OperatorNode |
| 436 | |
| 437 | if (tp === 'ParenthesisNode') { |
| 438 | // Recursion |
| 439 | expandPower(node.content, node, 'content') |
| 440 | } else if (tp !== 'ConstantNode' && tp !== 'SymbolNode') { |
| 441 | for (let i = 0; i < node.args.length; i++) { |
| 442 | expandPower(node.args[i], node, i) |
| 443 | } |
| 444 | } |
| 445 |
no test coverage detected
searching dependent graphs…