Skip to content

Commit 967659f

Browse files
Brandon Wufacebook-github-bot
authored andcommitted
Fixed bug of degree insensitivity in polynomial addition
Summary: As title. Reviewed By: grievejia Differential Revision: D28890244 fbshipit-source-id: 28a9f04ec80293ec4ebf4b3846812fd1ea981e08
1 parent c50efff commit 967659f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

source/analysis/test/typeTest.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ let test_polynomial_create_from_list _ =
31503150
[
31513151
2, [y, 1]; 1, [x, 1]; 1, [x, 2; y, 1]; 1, [z, 1]; 1, [y, 1; z, 1; x, 1]; 1, [x, 1; y, 2]; 2, [];
31523152
]
3153-
"2 + x + 2y + z + x^2y + xy^2 + xyz";
3153+
"2 + x + 2y + z + xyz + xy^2 + x^2y";
31543154
()
31553155
31563156
@@ -3270,6 +3270,7 @@ let test_add_polynomials _ =
32703270
assert_add [3, []] [2, []] "5";
32713271
assert_add [3, []] [-3, []; 2, [x, 2]] "2x^2";
32723272
assert_add [1, []; 3, [x, 1]; 2, [y, 1]] [2, []; 1, [x, 1]; 1, [z, 1]] "3 + 4x + 2y + z";
3273+
assert_add [1, []; 1, [x, 1; y, 2]] [1, [x, 2; y, 1]] "1 + xy^2 + x^2y";
32733274
()
32743275
32753276

source/analysis/type.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,18 @@ end = struct
610610
let total_degree_compare =
611611
Int.compare (sum_degrees left_variables) (sum_degrees right_variables)
612612
in
613+
let compare_variable_and_degree
614+
{ Monomial.variable = left_variable; degree = left_degree }
615+
{ Monomial.variable = right_variable; degree = right_degree }
616+
=
617+
match compare_variable compare_t left_variable right_variable with
618+
| result when not (Int.equal result 0) -> result
619+
| _ -> Int.compare left_degree right_degree
620+
in
613621
if total_degree_compare <> 0 then
614622
total_degree_compare
615623
else
616-
let variable_list variables = List.map variables ~f:(fun { variable; _ } -> variable) in
617-
List.compare
618-
(compare_variable compare_t)
619-
(variable_list left_variables)
620-
(variable_list right_variables)
624+
List.compare compare_variable_and_degree left_variables right_variables
621625

622626

623627
let normalize ~compare_t { constant_factor; variables } =

0 commit comments

Comments
 (0)