diff --git a/include/polyxx/context.h b/include/polyxx/context.h index b92ecd1..a1b2110 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 cafea22..fd4f8e6 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); @@ -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/include/polyxx/variable.h b/include/polyxx/variable.h index cb114a6..614994c 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); @@ -47,8 +46,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/polynomial.cpp b/src/polyxx/polynomial.cpp index a71c51f..87f33fd 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){}; @@ -370,7 +370,8 @@ namespace poly { if (degree(p) == 1) { // Derivative is constant, making the resultant trivial (and resultant() // does not cope with that) - return Polynomial(Integer(1)); + // 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)); } diff --git a/src/polyxx/variable.cpp b/src/polyxx/variable.cpp index 8166776..5a4cffa 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()}; } diff --git a/test/polyxx/CMakeLists.txt b/test/polyxx/CMakeLists.txt index 9dd506e..1f13c03 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 0000000..112aa08 --- /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! +}