Skip to content

Commit ac1c13e

Browse files
authored
Adding missing operations (#100)
* Make context object copyable. * Adding +=, -=, and *= of polynomial with integer to avoid implicit constructor call of polynomial and potential issues with context missmatch. * Adding ==, !=, <, <=, >, and >= between Polynomial and Integer. * typos * missing constructor in dyadic_rational * added missing comparison for Integer * add missing constructor for dyadic_interval * Removed constructors Polynomial(int) but made Integer(int) implicit. * adding lp_variable_db_is_valid * Improved constructors of Polynomial * Add warning in variable.h * Fixed whitespaces. * Fixed whitespaces. * Added tests. * Make variable constructors explicit --------- Co-authored-by: Thomas Hader <[email protected]>
1 parent cc0941e commit ac1c13e

File tree

14 files changed

+242
-47
lines changed

14 files changed

+242
-47
lines changed

include/polyxx/context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ namespace poly {
1919
deleting_unique_ptr<lp_polynomial_context_t> mPolynomialContext;
2020

2121
public:
22+
/** Constructs a new (empty) context */
2223
Context();
24+
25+
/** Wraps the lp context. */
26+
explicit Context(lp_polynomial_context_t* ctx);
27+
28+
/** Copy constructor. */
29+
Context(const Context& other);
30+
31+
/** No assignment as this would mess with internal reference counting. */
32+
Context& operator=(const Context& other) = delete;
33+
2334
/** Get a non-const pointer to the internal lp_variable_db_t.
2435
* Handle with care!
2536
*/

include/polyxx/dyadic_interval.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ namespace poly {
3333
/** Construct an open interval. */
3434
DyadicInterval(const Integer& a, const Integer& b);
3535
/** Construct an interval from the given bounds. */
36-
DyadicInterval(const Integer& a, bool a_open, const Integer& b,
37-
bool b_open);
36+
DyadicInterval(const Integer& a, bool a_open,
37+
const Integer& b, bool b_open);
38+
/** Construct a point interval. */
39+
explicit DyadicInterval(long i);
3840
/** Construct an open interval. */
39-
DyadicInterval(long a, long b);
41+
explicit DyadicInterval(long a, long b);
4042
/** Construct an interval from the given bounds. */
41-
DyadicInterval(long a, bool a_open, long b, bool b_open);
43+
explicit DyadicInterval(long a, bool a_open, long b, bool b_open);
4244
/** Copy from a DyadicInterval. */
4345
DyadicInterval(const DyadicInterval& i);
4446
/** Move from a DyadicInterval. */
@@ -58,11 +60,11 @@ namespace poly {
5860

5961
/** Collapse this interval to a single point. */
6062
void collapse(const DyadicRational& dr);
61-
/** The the lower bound. */
63+
/** The lower bound. */
6264
void set_lower(const DyadicRational& dr, bool open);
63-
/** The the upper bound. */
65+
/** The upper bound. */
6466
void set_upper(const DyadicRational& dr, bool open);
65-
/** The this interval by 2^n. */
67+
/** The interval by 2^n. */
6668
void scale(int n);
6769
};
6870

include/polyxx/dyadic_rational.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ namespace poly {
3131
explicit DyadicRational(double d);
3232
/** Construct from an int. */
3333
DyadicRational(int i);
34+
/** Construct from a long. */
35+
DyadicRational(long i);
3436

3537
/** Construct from an internal lp_dyadic_rational_t pointer. */
3638
explicit DyadicRational(const lp_dyadic_rational_t* dr);

include/polyxx/integer.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ namespace poly {
2222
/** Constructs zero. */
2323
Integer();
2424
/** Constructs from an int. */
25-
explicit Integer(int i);
25+
Integer(int i);
2626
/** Constructs from a long. */
27-
explicit Integer(long i);
27+
Integer(long i);
2828
/** Constructs from a long into the given ring. */
2929
Integer(const IntegerRing& ir, long i);
3030
/** Constructs from a string. */
@@ -122,16 +122,28 @@ namespace poly {
122122

123123
/** Compare two integers. */
124124
bool operator==(const Integer& lhs, const Integer& rhs);
125+
bool operator==(const Integer& lhs, long rhs);
126+
bool operator==(long lhs, const Integer& rhs);
125127
/** Compare two integers. */
126128
bool operator!=(const Integer& lhs, const Integer& rhs);
129+
bool operator!=(const Integer& lhs, long rhs);
130+
bool operator!=(long lhs, const Integer& rhs);
127131
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
128132
bool operator<(const Integer& lhs, const Integer& rhs);
133+
bool operator<(const Integer& lhs, long rhs);
134+
bool operator<(long lhs, const Integer& rhs);
129135
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
130136
bool operator<=(const Integer& lhs, const Integer& rhs);
137+
bool operator<=(const Integer& lhs, long rhs);
138+
bool operator<=(long lhs, const Integer& rhs);
131139
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
132140
bool operator>(const Integer& lhs, const Integer& rhs);
141+
bool operator>(const Integer& lhs, long rhs);
142+
bool operator>(long lhs, const Integer& rhs);
133143
/** Compare two integers according to the lexicographic ordering on (lower bound,upper bound). */
134144
bool operator>=(const Integer& lhs, const Integer& rhs);
145+
bool operator>=(const Integer& lhs, long rhs);
146+
bool operator>=(long lhs, const Integer& rhs);
135147

136148
/** Compare two integers over the given ring. */
137149
int compare(const IntegerRing& ir, const Integer& lhs, const Integer& rhs);

include/polyxx/polynomial.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,27 @@ namespace poly {
3333
/** Construct a zero polynomial. */
3434
Polynomial();
3535

36+
/** Construct a constant polynomial from an internal context pointer. */
37+
Polynomial(const lp_polynomial_context_t* c, const Variable& v);
3638
/** Construct from a variable and a custom context. */
37-
Polynomial(const Context& c, Variable v);
39+
Polynomial(const Context& c, const Variable& v);
3840
/** Construct from a variable. */
39-
Polynomial(Variable v);
41+
Polynomial(const Variable& v);
4042

43+
/** Construct a constant polynomial from an internal context pointer. */
44+
Polynomial(const lp_polynomial_context_t* c, const Integer& i, const Variable& v, unsigned n);
4145
/** Construct i * v^n from a custom context. */
42-
Polynomial(const Context& c, const Integer& i, Variable v, unsigned n);
46+
Polynomial(const Context& c, const Integer& i, const Variable& v, unsigned n);
4347
/** Construct i * v^n. */
44-
Polynomial(const Integer& i, Variable v, unsigned n);
48+
Polynomial(const Integer& i, const Variable& v, unsigned n);
4549

50+
/** Construct a constant polynomial from an internal context pointer. */
51+
Polynomial(const lp_polynomial_context_t* c, const Integer& i);
4652
/** Construct from an integer and a custom context. */
4753
Polynomial(const Context& c, const Integer& i);
4854
/** Construct from an integer. */
4955
Polynomial(const Integer& i);
5056

51-
/** Construct from an integer and a custom context. */
52-
Polynomial(const Context& c, long i);
53-
/** Construct from an integer. */
54-
Polynomial(long i);
55-
5657
/** Copy from a Polynomial. */
5758
Polynomial(const Polynomial& p);
5859
/** Move from a Polynomial. */
@@ -132,16 +133,28 @@ namespace poly {
132133

133134
/** Compare polynomials. */
134135
bool operator==(const Polynomial& lhs, const Polynomial& rhs);
136+
bool operator==(const Integer& lhs, const Polynomial& rhs);
137+
bool operator==(const Polynomial& lhs, const Integer& rhs);
135138
/** Compare polynomials. */
136139
bool operator!=(const Polynomial& lhs, const Polynomial& rhs);
140+
bool operator!=(const Integer& lhs, const Polynomial& rhs);
141+
bool operator!=(const Polynomial& lhs, const Integer& rhs);
137142
/** Compare polynomials. */
138143
bool operator<(const Polynomial& lhs, const Polynomial& rhs);
144+
bool operator<(const Integer& lhs, const Polynomial& rhs);
145+
bool operator<(const Polynomial& lhs, const Integer& rhs);
139146
/** Compare polynomials. */
140147
bool operator<=(const Polynomial& lhs, const Polynomial& rhs);
148+
bool operator<=(const Integer& lhs, const Polynomial& rhs);
149+
bool operator<=(const Polynomial& lhs, const Integer& rhs);
141150
/** Compare polynomials. */
142151
bool operator>(const Polynomial& lhs, const Polynomial& rhs);
152+
bool operator>(const Integer& lhs, const Polynomial& rhs);
153+
bool operator>(const Polynomial& lhs, const Integer& rhs);
143154
/** Compare polynomials. */
144155
bool operator>=(const Polynomial& lhs, const Polynomial& rhs);
156+
bool operator>=(const Integer& lhs, const Polynomial& rhs);
157+
bool operator>=(const Polynomial& lhs, const Integer& rhs);
145158

146159
/** Add two polynomials. */
147160
Polynomial operator+(const Polynomial& lhs, const Polynomial& rhs);
@@ -151,6 +164,8 @@ namespace poly {
151164
Polynomial operator+(const Integer& lhs, const Polynomial& rhs);
152165
/** Add and assign two polynomials. */
153166
Polynomial& operator+=(Polynomial& lhs, const Polynomial& rhs);
167+
/** Add and assign a polynomial with an integer. */
168+
Polynomial& operator+=(Polynomial& lhs, const Integer& rhs);
154169
/** Compute lhs += rhs1 * rhs2. */
155170
Polynomial& add_mul(Polynomial& lhs, const Polynomial& rhs1, const Polynomial& rhs2);
156171

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

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

179198
/** Multiply with x^n where x is the main variable. */
180199
Polynomial shl(const Polynomial& lhs, unsigned n);

include/polyxx/variable.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace poly {
99

1010
/**
1111
* Implements a wrapper for lp_variable_t.
12+
* WARNING: Variable does not store its context, i.e. the variable database
13+
* it belongs to. Only use it with a Polynomial / Variable of the same context!
1214
*/
1315
class Variable {
1416
/** The actual variable. */
@@ -18,11 +20,11 @@ namespace poly {
1820
/** Construct with a null variable. */
1921
Variable();
2022
/** Construct from a lp_variable_t. */
21-
Variable(lp_variable_t var);
23+
explicit Variable(lp_variable_t var);
2224
/** Construct a new variable with the given name in the specified context. */
23-
Variable(const Context& c, const char* name);
25+
explicit Variable(const Context& c, const char* name);
2426
/** Construct a new variable with the given name in the default context. */
25-
Variable(const char* name);
27+
explicit Variable(const char* name);
2628

2729
/** Get the internal lp_variable_t. Note that it's only a type alias for
2830
* long.

include/variable_db.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ int lp_variable_db_print(const lp_variable_db_t* var_db, FILE* out);
4646
/** Get the name of the variable */
4747
const char* lp_variable_db_get_name(const lp_variable_db_t* var_db, lp_variable_t var);
4848

49+
/** Checks if var is a valid index */
50+
int lp_variable_db_is_valid(const lp_variable_db_t* var_db, lp_variable_t var);
51+
4952
#ifdef __cplusplus
5053
} /* close extern "C" { */
5154
#endif

src/polyxx/context.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ namespace poly {
1616
lp_polynomial_context_detach(ptr);
1717
});
1818
}
19+
20+
Context::Context(lp_polynomial_context_t* ctx) {
21+
mVariableDB = deleting_unique_ptr<lp_variable_db_t>(
22+
ctx->var_db,
23+
[](lp_variable_db_t *ptr) { lp_variable_db_detach(ptr); });
24+
mVariableOrder = deleting_unique_ptr<lp_variable_order_t>(
25+
ctx->var_order,
26+
[](lp_variable_order_t *ptr) { lp_variable_order_detach(ptr); });
27+
mPolynomialContext = deleting_unique_ptr<lp_polynomial_context_t>(
28+
ctx,
29+
[](lp_polynomial_context_t *ptr) { lp_polynomial_context_detach(ptr); });
30+
31+
lp_variable_db_attach(ctx->var_db);
32+
lp_variable_order_attach(ctx->var_order);
33+
lp_polynomial_context_attach(ctx);
34+
}
35+
36+
Context::Context(const Context& other) : Context(other.get_polynomial_context()) {}
37+
1938
lp_variable_db_t* Context::get_variable_db() const {
2039
return const_cast<lp_variable_db_t*>(mVariableDB.get());
2140
}

src/polyxx/dyadic_interval.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace poly {
2727
lp_dyadic_interval_construct_from_integer(get_internal(), a.get_internal(),
2828
a_open, b.get_internal(), b_open);
2929
}
30+
DyadicInterval::DyadicInterval(long i)
31+
: DyadicInterval(DyadicRational(i)) {}
3032
DyadicInterval::DyadicInterval(long a, long b)
3133
: DyadicInterval(a, true, b, true) {}
3234
DyadicInterval::DyadicInterval(long a, bool a_open, long b, bool b_open) {

src/polyxx/dyadic_rational.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace poly {
2525
lp_dyadic_rational_construct_from_double(&mDRat, d);
2626
}
2727
DyadicRational::DyadicRational(int i) : DyadicRational(i, 0) {}
28+
DyadicRational::DyadicRational(long i) : DyadicRational(i, 0) {}
2829

2930
DyadicRational::DyadicRational(const lp_dyadic_rational_t* dr) {
3031
lp_dyadic_rational_construct_copy(&mDRat, dr);

0 commit comments

Comments
 (0)