(expr: Expression)
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | /** Apply the distributive law if the expression is a product of sums. |
| 200 | * For example, a(b + c) = ab + ac |
| 201 | * Expand the expression if it is a power of a sum. |
| 202 | * Expand the terms of the expression if it is a sum or negate. |
| 203 | * If the expression is a fraction, expand the numerator. |
| 204 | * If the exression is a relational operator, expand the operands. |
| 205 | * Return null if the expression cannot be expanded. |
| 206 | */ |
| 207 | export function expand(expr: Expression): Expression { |
| 208 | // To expand an expression, we need to use its canonical form |
| 209 | expr = expr.canonical; |
| 210 | |
| 211 | if (typeof expr.operator !== 'string') return expr; |
| 212 | |
| 213 | // |
| 214 | // Expand relational operators |
| 215 | // |
| 216 | if (isRelationalOperator(expr.operator)) { |
| 217 | const ops = isFunction(expr) ? expr.ops : []; |
| 218 | return expr.engine._fn( |
| 219 | expr.operator, |
| 220 | ops.map((x) => expand(x)) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | return ( |
| 225 | expandFunction( |
no test coverage detected