File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 17
17
#include < sstream>
18
18
#include < utility>
19
19
#include < cstdint>
20
+ #include < cassert>
20
21
21
22
#include < boost/math/tools/is_standalone.hpp>
22
23
#ifndef BOOST_MATH_STANDALONE
@@ -214,5 +215,34 @@ inline auto simple_continued_fraction_coefficients(Real x)
214
215
return temp.get_data ();
215
216
}
216
217
218
+ // Can be used with `boost::rational` from <boost/rational.hpp>
219
+ template <typename Rational, typename Real, typename Z = std::int64_t >
220
+ inline Rational to_rational (const simple_continued_fraction<Real, Z>& scf)
221
+ {
222
+ using int_t = typename Rational::int_type;
223
+
224
+ auto & coefs = scf.partial_denominators ();
225
+ const size_t size_ = coefs.size ();
226
+ assert (size_ >= 1 );
227
+ if (size_ == 1 ) return static_cast <int_t >(coefs[0 ]);
228
+
229
+ // p0 = a0, p1 = a1.a0 + 1, pn = an.pn-1 + pn-2 for 2 <= n
230
+ // q0 = 1, q1 = a1, qn = an.qn-1 + qn-2 for 2 <= n
231
+
232
+ int_t p0 = coefs[0 ];
233
+ int_t p1 = p0*coefs[1 ] + 1 ;
234
+ int_t q0 = 1 ;
235
+ int_t q1 = coefs[1 ];
236
+ for (size_t i = 2 ; i < size_; ++i) {
237
+ const Z cn = coefs[i];
238
+ const int_t pn = cn*p1 + p0;
239
+ const int_t qn = cn*q1 + q0;
240
+ p0 = std::exchange (p1, pn);
241
+ q0 = std::exchange (q1, qn);
242
+ }
243
+
244
+ return {p1, q1};
245
+ }
246
+
217
247
}
218
248
#endif
You can’t perform that action at this time.
0 commit comments