Skip to content

Commit f4f94f1

Browse files
committed
Instead of normalizing after Joux's subtraction, find gcd(a(0), b(0))first.
1 parent e7b5e40 commit f4f94f1

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

include/boost/math/tools/polynomial_gcd.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,23 @@ struct gcd_traits_polynomial_defaults : public gcd_traits_defaults< boost::math:
6464
inline static void
6565
subtract(polynomial_type &a, polynomial_type const &b)
6666
{
67-
// We want to use Stepanov's implementation as often as possible because
68-
// it results in the smallest coefficients, however, whole numbers take
69-
// precedence.
67+
// We use Stepanov's implementation when the constant coefficients
68+
// divide evenly, and Joux's otherwise.
7069
T m = constant_coefficient(a);
7170
gcd_traits<T>::modulo(m, constant_coefficient(b));
7271
if (!m)
7372
{
7473
T const r = constant_coefficient(a) / constant_coefficient(b);
75-
// Stepanov's implementation; suffers from floating point inaccuracy
76-
// when r does not divide ___ evenly.
7774
a -= r * b;
7875
}
7976
else
8077
{
81-
// Antoine Joux's implementation: produces huge coefficients.
82-
T const tmp = constant_coefficient(a);
83-
a *= constant_coefficient(b);
84-
a -= tmp * b;
85-
normalize(a);
78+
// Antoine Joux's implementation tempered by coefficient gcd.
79+
T const a0 = constant_coefficient(a);
80+
T const b0 = constant_coefficient(b);
81+
T const gcd_a0b0 = gcd(a0, b0);
82+
a *= b0 / gcd_a0b0;
83+
a -= (a0 / gcd_a0b0) * b;
8684
}
8785
}
8886
};

0 commit comments

Comments
 (0)