This PR adds some new methods for the RealAlgebraicNumber class: division, computing the (multiplicative) inverse and a specialization of std::hash.
{
return lhs.getValue() * rhs.getValue();
}
+RealAlgebraicNumber operator/(const RealAlgebraicNumber& lhs,
+ const RealAlgebraicNumber& rhs)
+{
+#ifdef CVC5_POLY_IMP
+ Assert(!isZero(rhs)) << "Can not divide by zero";
+ return lhs.getValue() / rhs.getValue();
+#else
+ return lhs;
+#endif
+}
RealAlgebraicNumber& operator+=(RealAlgebraicNumber& lhs,
const RealAlgebraicNumber& rhs)
int sgn(const RealAlgebraicNumber& ran) { return sgn(ran.getValue()); }
bool isZero(const RealAlgebraicNumber& ran) { return is_zero(ran.getValue()); }
bool isOne(const RealAlgebraicNumber& ran) { return is_one(ran.getValue()); }
+RealAlgebraicNumber inverse(const RealAlgebraicNumber& ran)
+{
+#ifdef CVC5_POLY_IMP
+ Assert(!isZero(ran)) << "Can not invert zero";
+ return inverse(ran.getValue());
+#else
+ return ran;
+#endif
+}
} // namespace cvc5
+
+namespace std {
+size_t hash<cvc5::RealAlgebraicNumber>::operator()(
+ const cvc5::RealAlgebraicNumber& ran) const
+{
+#ifdef CVC5_POLY_IMP
+ return lp_algebraic_number_hash_approx(ran.getValue().get_internal(), 2);
+#else
+ return 0;
+#endif
+}
+} // namespace std
/** Multiply two real algebraic numbers. */
RealAlgebraicNumber operator*(const RealAlgebraicNumber& lhs,
const RealAlgebraicNumber& rhs);
+/** Divide two real algebraic numbers. */
+RealAlgebraicNumber operator/(const RealAlgebraicNumber& lhs,
+ const RealAlgebraicNumber& rhs);
/** Add and assign two real algebraic numbers. */
RealAlgebraicNumber& operator+=(RealAlgebraicNumber& lhs,
bool isZero(const RealAlgebraicNumber& ran);
/** Check whether a real algebraic number is one. */
bool isOne(const RealAlgebraicNumber& ran);
+/** Compute the inverse of a real algebraic number. */
+RealAlgebraicNumber inverse(const RealAlgebraicNumber& ran);
+
+using RealAlgebraicNumberHashFunction = std::hash<RealAlgebraicNumber>;
} // namespace cvc5
+namespace std {
+template <>
+struct hash<cvc5::RealAlgebraicNumber>
+{
+ /**
+ * Computes a hash of the given real algebraic number. Given that the internal
+ * representation of real algebraic numbers are inherently mutable (th
+ * interval may be refined for comparisons) we hash a well-defined rational
+ * approximation.
+ */
+ size_t operator()(const cvc5::RealAlgebraicNumber& ran) const;
+};
+} // namespace std
+
#endif /* CVC5__REAL_ALGEBRAIC_NUMBER_H */
ASSERT_TRUE(sqrt2 < RealAlgebraicNumber(Integer(2)));
}
-TEST_F(TestUtilBlackRealAlgebraicNumber, comprison)
+TEST_F(TestUtilBlackRealAlgebraicNumber, comparison)
{
RealAlgebraicNumber msqrt3({-3, 0, 1}, -2, -1);
RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
ASSERT_EQ(-msqrt2 + sqrt2, sqrt2 + sqrt2);
ASSERT_EQ(msqrt2 * sqrt2, RealAlgebraicNumber(Integer(-2)));
}
+
+TEST_F(TestUtilBlackRealAlgebraicNumber, division)
+{
+ RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
+ RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
+ RealAlgebraicNumber mone({1, 1}, -2, 0);
+
+ ASSERT_EQ(msqrt2 / sqrt2, mone);
+ ASSERT_TRUE(isOne(sqrt2 / sqrt2));
+}
+
} // namespace test
} // namespace cvc5