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
11 changes: 11 additions & 0 deletions include/polyxx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ namespace poly {
deleting_unique_ptr<lp_polynomial_context_t> mPolynomialContext;

public:
/** Constructs a new (empty) context */
Context();

/** Wraps the lp context. */
explicit Context(lp_polynomial_context_t* ctx);

/** Copy constructor. */
Context(const Context& other);

/** No assignment as this would mess with internal reference counting. */
Context& operator=(const Context& other) = delete;

/** Get a non-const pointer to the internal lp_variable_db_t.
* Handle with care!
*/
Expand Down
16 changes: 9 additions & 7 deletions include/polyxx/dyadic_interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ namespace poly {
/** Construct an open interval. */
DyadicInterval(const Integer& a, const Integer& b);
/** Construct an interval from the given bounds. */
DyadicInterval(const Integer& a, bool a_open, const Integer& b,
bool b_open);
DyadicInterval(const Integer& a, bool a_open,
const Integer& b, bool b_open);
/** Construct a point interval. */
explicit DyadicInterval(long i);
/** Construct an open interval. */
DyadicInterval(long a, long b);
explicit DyadicInterval(long a, long b);
/** Construct an interval from the given bounds. */
DyadicInterval(long a, bool a_open, long b, bool b_open);
explicit DyadicInterval(long a, bool a_open, long b, bool b_open);
/** Copy from a DyadicInterval. */
DyadicInterval(const DyadicInterval& i);
/** Move from a DyadicInterval. */
Expand All @@ -58,11 +60,11 @@ namespace poly {

/** Collapse this interval to a single point. */
void collapse(const DyadicRational& dr);
/** The the lower bound. */
/** The lower bound. */
void set_lower(const DyadicRational& dr, bool open);
/** The the upper bound. */
/** The upper bound. */
void set_upper(const DyadicRational& dr, bool open);
/** The this interval by 2^n. */
/** The interval by 2^n. */
void scale(int n);
};

Expand Down
2 changes: 2 additions & 0 deletions include/polyxx/dyadic_rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace poly {
explicit DyadicRational(double d);
/** Construct from an int. */
DyadicRational(int i);
/** Construct from a long. */
DyadicRational(long i);

/** Construct from an internal lp_dyadic_rational_t pointer. */
explicit DyadicRational(const lp_dyadic_rational_t* dr);
Expand Down
16 changes: 14 additions & 2 deletions include/polyxx/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace poly {
/** Constructs zero. */
Integer();
/** Constructs from an int. */
explicit Integer(int i);
Integer(int i);
/** Constructs from a long. */
explicit Integer(long i);
Integer(long i);
/** Constructs from a long into the given ring. */
Integer(const IntegerRing& ir, long i);
/** Constructs from a string. */
Expand Down Expand Up @@ -122,16 +122,28 @@ namespace poly {

/** Compare two integers. */
bool operator==(const Integer& lhs, const Integer& rhs);
bool operator==(const Integer& lhs, long rhs);
bool operator==(long lhs, const Integer& rhs);
/** Compare two integers. */
bool operator!=(const Integer& lhs, const Integer& rhs);
bool operator!=(const Integer& lhs, long rhs);
bool operator!=(long lhs, const Integer& rhs);
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
bool operator<(const Integer& lhs, const Integer& rhs);
bool operator<(const Integer& lhs, long rhs);
bool operator<(long lhs, const Integer& rhs);
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
bool operator<=(const Integer& lhs, const Integer& rhs);
bool operator<=(const Integer& lhs, long rhs);
bool operator<=(long lhs, const Integer& rhs);
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
bool operator>(const Integer& lhs, const Integer& rhs);
bool operator>(const Integer& lhs, long rhs);
bool operator>(long lhs, const Integer& rhs);
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
bool operator>=(const Integer& lhs, const Integer& rhs);
bool operator>=(const Integer& lhs, long rhs);
bool operator>=(long lhs, const Integer& rhs);

/** Compare two integers over the given ring. */
int compare(const IntegerRing& ir, const Integer& lhs, const Integer& rhs);
Expand Down
37 changes: 28 additions & 9 deletions include/polyxx/polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ namespace poly {
/** Construct a zero polynomial. */
Polynomial();

/** Construct a constant polynomial from an internal context pointer. */
Polynomial(const lp_polynomial_context_t* c, const Variable& v);
/** Construct from a variable and a custom context. */
Polynomial(const Context& c, Variable v);
Polynomial(const Context& c, const Variable& v);
/** Construct from a variable. */
Polynomial(Variable v);
Polynomial(const Variable& v);

/** Construct a constant polynomial from an internal context pointer. */
Polynomial(const lp_polynomial_context_t* c, const Integer& i, const Variable& v, unsigned n);
/** Construct i * v^n from a custom context. */
Polynomial(const Context& c, const Integer& i, Variable v, unsigned n);
Polynomial(const Context& c, const Integer& i, const Variable& v, unsigned n);
/** Construct i * v^n. */
Polynomial(const Integer& i, Variable v, unsigned n);
Polynomial(const Integer& i, const Variable& v, unsigned n);

/** Construct a constant polynomial from an internal context pointer. */
Polynomial(const lp_polynomial_context_t* c, const Integer& i);
/** Construct from an integer and a custom context. */
Polynomial(const Context& c, const Integer& i);
/** Construct from an integer. */
Polynomial(const Integer& i);

/** Construct from an integer and a custom context. */
Polynomial(const Context& c, long i);
/** Construct from an integer. */
Polynomial(long i);

/** Copy from a Polynomial. */
Polynomial(const Polynomial& p);
/** Move from a Polynomial. */
Expand Down Expand Up @@ -132,16 +133,28 @@ namespace poly {

/** Compare polynomials. */
bool operator==(const Polynomial& lhs, const Polynomial& rhs);
bool operator==(const Integer& lhs, const Polynomial& rhs);
bool operator==(const Polynomial& lhs, const Integer& rhs);
/** Compare polynomials. */
bool operator!=(const Polynomial& lhs, const Polynomial& rhs);
bool operator!=(const Integer& lhs, const Polynomial& rhs);
bool operator!=(const Polynomial& lhs, const Integer& rhs);
/** Compare polynomials. */
bool operator<(const Polynomial& lhs, const Polynomial& rhs);
bool operator<(const Integer& lhs, const Polynomial& rhs);
bool operator<(const Polynomial& lhs, const Integer& rhs);
/** Compare polynomials. */
bool operator<=(const Polynomial& lhs, const Polynomial& rhs);
bool operator<=(const Integer& lhs, const Polynomial& rhs);
bool operator<=(const Polynomial& lhs, const Integer& rhs);
/** Compare polynomials. */
bool operator>(const Polynomial& lhs, const Polynomial& rhs);
bool operator>(const Integer& lhs, const Polynomial& rhs);
bool operator>(const Polynomial& lhs, const Integer& rhs);
/** Compare polynomials. */
bool operator>=(const Polynomial& lhs, const Polynomial& rhs);
bool operator>=(const Integer& lhs, const Polynomial& rhs);
bool operator>=(const Polynomial& lhs, const Integer& rhs);

/** Add two polynomials. */
Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs);
Expand All @@ -151,6 +164,8 @@ namespace poly {
Polynomial operator+(const Integer& lhs, const Polynomial& rhs);
/** Add and assign two polynomials. */
Polynomial& operator+=(Polynomial& lhs, const Polynomial& rhs);
/** Add and assign a polynomial with an integer. */
Polynomial& operator+=(Polynomial& lhs, const Integer& rhs);
/** Compute lhs += rhs1 * rhs2. */
Polynomial& add_mul(Polynomial& lhs, const Polynomial& rhs1, const Polynomial& rhs2);

Expand All @@ -164,6 +179,8 @@ namespace poly {
Polynomial operator-(const Integer& lhs, const Polynomial& rhs);
/** Subtract and assign two polynomials. */
Polynomial& operator-=(Polynomial& lhs, const Polynomial& rhs);
/** Subtract and assigns a polynomial with an integer. */
Polynomial& operator-=(Polynomial& lhs, const Integer& rhs);
/** Compute lhs -= rhs1 * rhs2. */
Polynomial& sub_mul(Polynomial& lhs, const Polynomial& rhs1, const Polynomial& rhs2);

Expand All @@ -175,6 +192,8 @@ namespace poly {
Polynomial operator*(const Integer& lhs, const Polynomial& rhs);
/** Multiply and assign two polynomials. */
Polynomial& operator*=(Polynomial& lhs, const Polynomial& rhs);
/** Multiply and assign a polynomial with an integer */
Polynomial& operator*=(Polynomial& lhs, const Integer& rhs);

/** Multiply with x^n where x is the main variable. */
Polynomial shl(const Polynomial& lhs, unsigned n);
Expand Down
8 changes: 5 additions & 3 deletions include/polyxx/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace poly {

/**
* Implements a wrapper for lp_variable_t.
* WARNING: Variable does not store its context, i.e. the variable database
* it belongs to. Only use it with a Polynomial / Variable of the same context!
*/
class Variable {
/** The actual variable. */
Expand All @@ -18,11 +20,11 @@ namespace poly {
/** Construct with a null variable. */
Variable();
/** Construct from a lp_variable_t. */
Variable(lp_variable_t var);
explicit Variable(lp_variable_t var);
/** Construct a new variable with the given name in the specified context. */
Variable(const Context& c, const char* name);
explicit Variable(const Context& c, const char* name);
/** Construct a new variable with the given name in the default context. */
Variable(const char* name);
explicit Variable(const char* name);

/** Get the internal lp_variable_t. Note that it's only a type alias for
* long.
Expand Down
3 changes: 3 additions & 0 deletions include/variable_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ int lp_variable_db_print(const lp_variable_db_t* var_db, FILE* out);
/** Get the name of the variable */
const char* lp_variable_db_get_name(const lp_variable_db_t* var_db, lp_variable_t var);

/** Checks if var is a valid index */
int lp_variable_db_is_valid(const lp_variable_db_t* var_db, lp_variable_t var);

#ifdef __cplusplus
} /* close extern "C" { */
#endif
19 changes: 19 additions & 0 deletions src/polyxx/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ namespace poly {
lp_polynomial_context_detach(ptr);
});
}

Context::Context(lp_polynomial_context_t* ctx) {
mVariableDB = deleting_unique_ptr<lp_variable_db_t>(
ctx->var_db,
[](lp_variable_db_t *ptr) { lp_variable_db_detach(ptr); });
mVariableOrder = deleting_unique_ptr<lp_variable_order_t>(
ctx->var_order,
[](lp_variable_order_t *ptr) { lp_variable_order_detach(ptr); });
mPolynomialContext = deleting_unique_ptr<lp_polynomial_context_t>(
ctx,
[](lp_polynomial_context_t *ptr) { lp_polynomial_context_detach(ptr); });

lp_variable_db_attach(ctx->var_db);
lp_variable_order_attach(ctx->var_order);
lp_polynomial_context_attach(ctx);
}

Context::Context(const Context& other) : Context(other.get_polynomial_context()) {}

lp_variable_db_t* Context::get_variable_db() const {
return const_cast<lp_variable_db_t*>(mVariableDB.get());
}
Expand Down
2 changes: 2 additions & 0 deletions src/polyxx/dyadic_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace poly {
lp_dyadic_interval_construct_from_integer(get_internal(), a.get_internal(),
a_open, b.get_internal(), b_open);
}
DyadicInterval::DyadicInterval(long i)
: DyadicInterval(DyadicRational(i)) {}
DyadicInterval::DyadicInterval(long a, long b)
: DyadicInterval(a, true, b, true) {}
DyadicInterval::DyadicInterval(long a, bool a_open, long b, bool b_open) {
Expand Down
1 change: 1 addition & 0 deletions src/polyxx/dyadic_rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace poly {
lp_dyadic_rational_construct_from_double(&mDRat, d);
}
DyadicRational::DyadicRational(int i) : DyadicRational(i, 0) {}
DyadicRational::DyadicRational(long i) : DyadicRational(i, 0) {}

DyadicRational::DyadicRational(const lp_dyadic_rational_t* dr) {
lp_dyadic_rational_construct_copy(&mDRat, dr);
Expand Down
36 changes: 36 additions & 0 deletions src/polyxx/integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,57 @@ namespace poly {
bool operator==(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) == 0;
}
bool operator==(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) == 0;
}
bool operator==(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) == 0;
}
bool operator!=(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) != 0;
}
bool operator!=(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) != 0;
}
bool operator!=(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) != 0;
}
bool operator<(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) < 0;
}
bool operator<(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) < 0;
}
bool operator<(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) < 0;
}
bool operator<=(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) <= 0;
}
bool operator<=(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) <= 0;
}
bool operator<=(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) <= 0;
}
bool operator>(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) > 0;
}
bool operator>(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) > 0;
}
bool operator>(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) > 0;
}
bool operator>=(const Integer& lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) >= 0;
}
bool operator>=(const Integer& lhs, long rhs) {
return compare(IntegerRing::Z, lhs, rhs) >= 0;
}
bool operator>=(long lhs, const Integer& rhs) {
return compare(IntegerRing::Z, lhs, rhs) >= 0;
}

int compare(const IntegerRing& ir, const Integer& lhs, const Integer& rhs) {
return lp_integer_cmp(ir.get_internal(), lhs.get_internal(),
Expand Down
Loading