From 5d22332cd5bf737a18ac165406986149fd8a32c9 Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Fri, 24 Oct 2025 10:55:03 -0700 Subject: [PATCH 1/6] Removes inline on variable printing Fixes issue #96. There is no need for inline on printing code. --- include/polyxx/variable.h | 3 +-- src/polyxx/variable.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/polyxx/variable.h b/include/polyxx/variable.h index cb114a69..81b4ae75 100644 --- a/include/polyxx/variable.h +++ b/include/polyxx/variable.h @@ -47,8 +47,7 @@ namespace poly { /** Stream the given Variable from the given context. * Use as follows: os << stream_variable(c, v) << ... */ - inline detail::variable_printer stream_variable(const Context& c, - const Variable& v); + detail::variable_printer stream_variable(const Context& c, const Variable& v); /** Compare two variables for equality. */ bool operator==(const Variable& lhs, const Variable& rhs); diff --git a/src/polyxx/variable.cpp b/src/polyxx/variable.cpp index 81667761..5a4cffaf 100644 --- a/src/polyxx/variable.cpp +++ b/src/polyxx/variable.cpp @@ -24,8 +24,7 @@ namespace poly { Context::get_context().get_variable_db(), v.get_internal()); } - detail::variable_printer stream_variable(const Context& c, - const Variable& v) { + detail::variable_printer stream_variable(const Context& c, const Variable& v) { return detail::variable_printer{c.get_variable_db(), v.get_internal()}; } From b7d93857225a746dc418a0813ca4bf79444c21e7 Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Fri, 24 Oct 2025 14:18:05 -0700 Subject: [PATCH 2/6] Update context.h comments and fixed typos. --- include/polyxx/context.h | 17 ++++++++++------- include/polyxx/polynomial.h | 2 +- include/polyxx/variable.h | 3 +-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/polyxx/context.h b/include/polyxx/context.h index b92ecd16..a1b21106 100644 --- a/include/polyxx/context.h +++ b/include/polyxx/context.h @@ -6,7 +6,10 @@ #include "utils.h" namespace poly { - + /** + * The wrapper class for the polynomial context. + * Only supports the lp_Z ring right now. + */ class Context { /** A variable database. */ deleting_unique_ptr mVariableDB; @@ -17,18 +20,18 @@ namespace poly { public: Context(); - /** Get a non-const pointer to the internal lp_variable_db_t. Handle with - * care! + /** Get a non-const pointer to the internal lp_variable_db_t. + * Handle with care! */ lp_variable_db_t* get_variable_db() const; - /** Get a non-const pointer to the internal lp_variable_order_t. Handle with - * care! + /** Get a non-const pointer to the internal lp_variable_order_t. + * Handle with care! */ lp_variable_order_t* get_variable_order() const; - /** Get a non-const pointer to the internal lp_polynomial_context_t. Handle - * with care! + /** Get a non-const pointer to the internal lp_polynomial_context_t. + * Handle with care! */ lp_polynomial_context_t* get_polynomial_context() const; diff --git a/include/polyxx/polynomial.h b/include/polyxx/polynomial.h index cafea229..2dba92c8 100644 --- a/include/polyxx/polynomial.h +++ b/include/polyxx/polynomial.h @@ -26,7 +26,7 @@ namespace poly { Polynomial(lp_polynomial_t* poly); /** Copy from an internal lp_polynomial_t pointer. */ Polynomial(const lp_polynomial_t* poly); - /** Construct a zero polynomial from an interval context pointer. */ + /** Construct a zero polynomial from an internal context pointer. */ Polynomial(const lp_polynomial_context_t* c); /** Construct a zero polynomial from a custom context. */ Polynomial(const Context& c); diff --git a/include/polyxx/variable.h b/include/polyxx/variable.h index 81b4ae75..614994ce 100644 --- a/include/polyxx/variable.h +++ b/include/polyxx/variable.h @@ -19,8 +19,7 @@ namespace poly { Variable(); /** Construct from a lp_variable_t. */ Variable(lp_variable_t var); - /** Construct a new variable with the given name in the specified context. - */ + /** Construct a new variable with the given name in the specified context. */ Variable(const Context& c, const char* name); /** Construct a new variable with the given name in the default context. */ Variable(const char* name); From 99aea6596d8a27d96fbd511b777e4df41dd6321f Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Fri, 24 Oct 2025 15:09:49 -0700 Subject: [PATCH 3/6] Avoid unnecessary copying of Integer objects on Polynomial constructors. --- include/polyxx/polynomial.h | 8 ++++---- src/polyxx/polynomial.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/polyxx/polynomial.h b/include/polyxx/polynomial.h index 2dba92c8..fd4f8e68 100644 --- a/include/polyxx/polynomial.h +++ b/include/polyxx/polynomial.h @@ -39,14 +39,14 @@ namespace poly { Polynomial(Variable v); /** Construct i * v^n from a custom context. */ - Polynomial(const Context& c, Integer i, Variable v, unsigned n); + Polynomial(const Context& c, const Integer& i, Variable v, unsigned n); /** Construct i * v^n. */ - Polynomial(Integer i, Variable v, unsigned n); + Polynomial(const Integer& i, Variable v, unsigned n); /** Construct from an integer and a custom context. */ - Polynomial(const Context& c, Integer i); + Polynomial(const Context& c, const Integer& i); /** Construct from an integer. */ - Polynomial(Integer i); + Polynomial(const Integer& i); /** Construct from an integer and a custom context. */ Polynomial(const Context& c, long i); diff --git a/src/polyxx/polynomial.cpp b/src/polyxx/polynomial.cpp index a71c51f7..2cb9cee7 100644 --- a/src/polyxx/polynomial.cpp +++ b/src/polyxx/polynomial.cpp @@ -39,19 +39,19 @@ namespace poly { Polynomial::Polynomial(const Context& c, Variable v) : Polynomial(c, Integer(1), v, 1) {} Polynomial::Polynomial(Variable v) : Polynomial(Context::get_context(), v) {} - Polynomial::Polynomial(const Context& c, Integer i, Variable v, unsigned n) + Polynomial::Polynomial(const Context& c, const Integer &i, Variable v, unsigned n) : mPoly(lp_polynomial_alloc(), polynomial_deleter) { lp_polynomial_construct_simple(get_internal(), c.get_polynomial_context(), i.get_internal(), v.get_internal(), n); } - Polynomial::Polynomial(Integer i, Variable v, unsigned n) + Polynomial::Polynomial(const Integer& i, Variable v, unsigned n) : Polynomial(Context::get_context(), i, v, n) {} - Polynomial::Polynomial(const Context& c, Integer i) + Polynomial::Polynomial(const Context& c, const Integer& i) : mPoly(lp_polynomial_alloc(), polynomial_deleter) { lp_polynomial_construct_simple(get_internal(), c.get_polynomial_context(), i.get_internal(), lp_variable_null, 0); } - Polynomial::Polynomial(Integer i) : Polynomial(Context::get_context(), i){}; + Polynomial::Polynomial(const Integer& i) : Polynomial(Context::get_context(), i){}; Polynomial::Polynomial(const Context& c, long i) : Polynomial(c, Integer(i)) {} Polynomial::Polynomial(long i) : Polynomial(Context::get_context(), i){}; From 21a45b6d226de2965df3cb0c36615227228fb814 Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Fri, 24 Oct 2025 17:38:47 -0700 Subject: [PATCH 4/6] Fixes #95 temporarily. --- src/polyxx/polynomial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/polyxx/polynomial.cpp b/src/polyxx/polynomial.cpp index 2cb9cee7..a4f6f297 100644 --- a/src/polyxx/polynomial.cpp +++ b/src/polyxx/polynomial.cpp @@ -370,7 +370,7 @@ namespace poly { if (degree(p) == 1) { // Derivative is constant, making the resultant trivial (and resultant() // does not cope with that) - return Polynomial(Integer(1)); + return Polynomial(detail::context(p)) + Integer(1); } return div(resultant(p, derivative(p)), leading_coefficient(p)); } From 31895432991d11a44e05667e5d443a3289c16cd2 Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Fri, 24 Oct 2025 19:01:16 -0700 Subject: [PATCH 5/6] Test for context issues --- test/polyxx/CMakeLists.txt | 1 + test/polyxx/test_context.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/polyxx/test_context.cpp diff --git a/test/polyxx/CMakeLists.txt b/test/polyxx/CMakeLists.txt index 9dd506e2..1f13c036 100644 --- a/test/polyxx/CMakeLists.txt +++ b/test/polyxx/CMakeLists.txt @@ -1,6 +1,7 @@ set(polyxx_tests test_algebraic_number test_assignment + test_context test_dyadic_interval test_dyadic_rational test_integer diff --git a/test/polyxx/test_context.cpp b/test/polyxx/test_context.cpp new file mode 100644 index 00000000..112aa08b --- /dev/null +++ b/test/polyxx/test_context.cpp @@ -0,0 +1,14 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include + +#include "doctest.h" + +using namespace poly; + +TEST_CASE("context::discriminant") { + Context ctx; + Variable x(ctx, "x"); + Polynomial p1(ctx, x); + Polynomial d = poly::discriminant(p1); // d built in the default context + Polynomial p2 = p1 + d; // assertion failure! +} From 8c4da154b1cd4c7f8a4ceb2ccf3967083036437b Mon Sep 17 00:00:00 2001 From: Thomas Hader Date: Tue, 28 Oct 2025 14:50:34 -0700 Subject: [PATCH 6/6] Adding comment --- src/polyxx/polynomial.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/polyxx/polynomial.cpp b/src/polyxx/polynomial.cpp index a4f6f297..87f33fd0 100644 --- a/src/polyxx/polynomial.cpp +++ b/src/polyxx/polynomial.cpp @@ -370,6 +370,7 @@ namespace poly { if (degree(p) == 1) { // Derivative is constant, making the resultant trivial (and resultant() // does not cope with that) + // This creates the constant polynomial 1 in the context of p. return Polynomial(detail::context(p)) + Integer(1); } return div(resultant(p, derivative(p)), leading_coefficient(p));