@@ -5,26 +5,20 @@ const Node = require('../node');
5
5
// e.g. (2x+1)/(2x+3) -> True because of the following simplification
6
6
// (2x+1)/(2x+3) -> (2x + 3)/(2x + 3) - 2/(2x + 3) -> 1 - 2/(2x + 3)
7
7
// e.g. (2x+1)/(2x^2 + 3) -> False
8
- // =========================================================================
8
+ // ==============================================================================
9
9
// CHECKS
10
10
// - Check for division in parent node
11
- // - Check that the number of arguments in parent node is 2
12
- // - Check that the number of numerator args is equal to 2 or 1. In the case
13
- // - of 1, we need that node to have a symbol (so it can't be a constant)
14
- // - Check that denominator has two args
15
- // - Check that the denominator op is '+' or '-'
16
- // - If the numerator has 2 args, check that the second arg is a constant node
17
- // - Check if the denominator's second arg is a constant node
18
- // - Check to see that the denominator and numerator both don't have exponents
11
+ // - Numerator has to be addition/subtraction of a polynomial term to the power
12
+ // of 1 and a constant term, in that order OR a polynomial term to the
13
+ // power of 1
14
+ // - Denominator has to be addition/subtraction of a polynomial term to the power
15
+ // of 1 and a constant term, in that order.
19
16
// - Check to see that the denominator and numerator have the same symbol
20
17
21
18
function canFindDenominatorInNumerator ( node ) {
22
19
if ( node . op !== '/' ) {
23
20
return false ;
24
21
}
25
- if ( node . args . length !== 2 ) {
26
- return false ;
27
- }
28
22
let numerator = node . args [ 0 ] ;
29
23
let denominator = node . args [ 1 ] ;
30
24
if ( Node . Type . isParenthesis ( numerator ) ) {
@@ -35,35 +29,36 @@ function canFindDenominatorInNumerator(node) {
35
29
}
36
30
37
31
let numeratorArgsLength ;
38
- // If numerator is '*' op, it signifies a single 'ax', should be assigned a
39
- // length of 2
40
- if ( 'args' in numerator &&
41
- ( numerator . op === '+' || numerator . op === '-' ) ) {
32
+ // If numerator has args, but it's just a polynomial term, length is 1
33
+ // Ex. 3x/2x+3 => numeratorArgsLength=1
34
+ if ( Node . PolynomialTerm . isPolynomialTerm ( numerator ) ) {
35
+ numeratorArgsLength = 1 ;
36
+ }
37
+ // If numerator has args and args are two seperate values length is 2
38
+ // Ex. 3x+4/2x+3 => numeratorArgsLength=2
39
+ else if ( numerator . op === '+' || numerator . op === '-' ) {
42
40
numeratorArgsLength = numerator . args . length ;
43
41
}
42
+ // If numerator doesn't have args and isn't a polynomial term, there's
43
+ // nothing the added functionality can do
44
+ // Ex. 3/(2x + 3) => False
44
45
else {
45
- numeratorArgsLength = 1 ;
46
+ return false ;
46
47
}
47
48
let denominatorArgsLength ;
48
- if ( 'args' in denominator ) {
49
+ if ( denominator . op === '+' || denominator . op === '-' ) {
49
50
denominatorArgsLength = denominator . args . length ;
50
51
}
52
+ // If denominator doesn't have args, it's length is 1. This case is already
53
+ // resolved by splitting the denominator into all the numerators
54
+ // Ex. (x + 3)/2x => x/2x + 3/2x
51
55
else {
52
- denominatorArgsLength = 1 ;
53
- }
54
- // If numerator args isn't length 2 or length 1 with a polynomial return false
55
- if ( numeratorArgsLength !== 2 &&
56
- ( ! ( numeratorArgsLength === 1 && ! Node . Type . isConstant ( numerator ) ) ) ) {
57
56
return false ;
58
57
}
59
58
// Function doesn't support denominators with args > 2
60
- // If denominatorArgsLength = 1 the normal functionality already covers it
61
59
if ( denominatorArgsLength !== 2 ) {
62
60
return false ;
63
61
}
64
- if ( ! ( denominator . op === '+' || denominator . op === '-' ) ) {
65
- return false ;
66
- }
67
62
// Check if numerator's second argument is a constant if numerator has two arguments
68
63
if ( numeratorArgsLength === 2 ) {
69
64
if ( ! Node . Type . isConstant ( numerator . args [ 1 ] ) ) {
0 commit comments