Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions include/polyxx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<lp_variable_db_t> mVariableDB;
Expand All @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions include/polyxx/polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions include/polyxx/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/polyxx/polynomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){};
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 1 addition & 2 deletions src/polyxx/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()};
}

Expand Down
1 change: 1 addition & 0 deletions test/polyxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(polyxx_tests
test_algebraic_number
test_assignment
test_context
test_dyadic_interval
test_dyadic_rational
test_integer
Expand Down
14 changes: 14 additions & 0 deletions test/polyxx/test_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <polyxx.h>

#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!
}