From d75d6d4684530021910457e1b9256a6deaf3c223 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Fri, 19 Jul 2013 16:50:50 -0700 Subject: [PATCH] Add power operator to mpq Signed-off-by: Leonardo de Moura --- src/numerics/mpq.cpp | 6 ++++++ src/numerics/mpq.h | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/numerics/mpq.cpp b/src/numerics/mpq.cpp index e0d3a235c..90dad4f9a 100644 --- a/src/numerics/mpq.cpp +++ b/src/numerics/mpq.cpp @@ -59,6 +59,12 @@ mpz ceil(mpq const & a) { return r; } +void power(mpq & a, mpq const & b, unsigned k) { + mpz_pow_ui(mpq_numref(a.m_val), mpq_numref(b.m_val), k); + mpz_pow_ui(mpq_denref(a.m_val), mpq_denref(b.m_val), k); + mpq_canonicalize(a.m_val); +} + extern void display(std::ostream & out, __mpz_struct const * v); std::ostream & operator<<(std::ostream & out, mpq const & v) { diff --git a/src/numerics/mpq.h b/src/numerics/mpq.h index 396e88f65..af388417a 100644 --- a/src/numerics/mpq.h +++ b/src/numerics/mpq.h @@ -194,6 +194,9 @@ public: void ceil(); friend mpz ceil(mpq const & a); + friend void power(mpq & a, mpq const & b, unsigned k); + friend mpq power(mpq a, unsigned k) { power(a, a, k); return a; } + friend std::ostream & operator<<(std::ostream & out, mpq const & v); friend void display_decimal(std::ostream & out, mpq const & a, unsigned prec);