void to_ring_elem(ring_elem &result, const ElementType& a) const { gmp_QQ b = getmemstructtype(gmp_QQ); mpq_init(b); fmpq_get_mpq(b,&a); result.poly_val = reinterpret_cast<Nterm*>(b); }
RCP<const Number> bernoulli(unsigned long n) { #ifdef HAVE_SYMENGINE_ARB fmpq_t res; fmpq_init(res); bernoulli_fmpq_ui(res, n); mpq_t a; mpq_init(a); fmpq_get_mpq(a, res); rational_class b(a); fmpq_clear(res); mpq_clear(a); return Rational::from_mpq(std::move(b)); #else // TODO: implement a faster algorithm std::vector<rational_class> v(n + 1); for (unsigned m = 0; m <= n; ++m) { v[m] = rational_class(1, m + 1); for (unsigned j = m; j >= 1; --j) { v[j - 1] = j * (v[j - 1] - v[j]); } } return Rational::from_mpq(v[0]); #endif }
void padic_get_mpq(mpq_t rop, const padic_t op, const padic_ctx_t ctx) { fmpq_t t; fmpq_init(t); padic_get_fmpq(t, op, ctx); fmpq_get_mpq(rop, t); fmpq_clear(t); }
renf_elem_class::operator mpq_class() const noexcept { mpq_class z; if (nf == nullptr) fmpq_get_mpq(z.__get_mp(), b); else if (is_rational()) { assert(is_rational() && "renf_elem_class not a rational"); ::fmpq_t q; fmpq_init(q); nf_elem_get_fmpq(q, a->elem, parent().renf_t()->nf); fmpq_get_mpq(z.__get_mp(), q); fmpq_clear(q); } return z; }
RCP<const Basic> URatPSeriesFlint::get_coeff(int n) const { const fmpq_wrapper fc(p_.get_coeff(n)); mpq_t gc; mpq_init(gc); fmpq_get_mpq(gc, fc.get_fmpq_t()); rational_class r(gc); mpq_clear(gc); return Rational::from_mpq(std::move(r)); }
RCP<const Basic> URatPSeriesFlint::get_coeff(int n) const { const flint::fmpqxx fc(p_.get_coeff(n)); mpq_t gc; mpq_init(gc); fmpq_get_mpq(gc, fc._data().inner); rational_class r(gc); mpq_clear(gc); return Rational::from_mpq(std::move(r)); }
void fmpq_poly_evaluate_mpz(mpq_t res, const fmpq_poly_t poly, const mpz_t a) { fmpq_t r; fmpz_t b; fmpq_init(r); fmpz_init(b); fmpz_set_mpz(b, a); fmpq_poly_evaluate_fmpz(r, poly, b); fmpq_get_mpq(res, r); fmpq_clear(r); fmpz_clear(b); }
umap_int_basic URatPSeriesFlint::as_dict() const { umap_int_basic map; mpq_t gc; mpq_init(gc); for (int n = 0; n < degree_; n++) { const fmpq_wrapper fc(p_.get_coeff(n)); if (not fc.is_zero()) { fmpq_get_mpq(gc, fc.get_fmpq_t()); RCP<const Number> basic = Rational::from_mpq(rational_class(gc)); map[n] = basic; } } mpq_clear(gc); return map; }
RCP<const Number> bernoulli(ulong n) { #ifdef HAVE_CSYMPY_ARB fmpq_t res; fmpq_init(res); bernoulli_fmpq_ui(res, n); mpq_t a; mpq_init(a); fmpq_get_mpq(a, res); mpq_class b (a); fmpq_clear(res); mpq_clear(a); return Rational::from_mpq(b); #else throw std::runtime_error("Currently supported only if ARB is installed"); #endif }
RCP<const Basic> URatPSeriesFlint::as_basic() const { RCP<const Symbol> x = symbol(var_); RCP<const Number> zcoef; umap_basic_num dict_; mpq_t gc; mpq_init(gc); for (int n = 0; n < degree_; n++) { const fmpq_wrapper fc(p_.get_coeff(n)); if (not fc.is_zero()) { fmpq_get_mpq(gc, fc.get_fmpq_t()); RCP<const Number> basic = Rational::from_mpq(rational_class(gc)); auto term = SymEngine::mul(SymEngine::pow(x, SymEngine::integer(n)), basic); if (n == 0) zcoef = basic; Add::coef_dict_add_term(outArg(basic), dict_, one, term); } else if (n == 0) zcoef = integer(0); } mpq_clear(gc); return std::move(Add::from_dict(zcoef, std::move(dict_))); }
int main(void) { int i; FLINT_TEST_INIT(state); flint_printf("submul...."); fflush(stdout); /* x -= y * z */ for (i = 0; i < 10000; i++) { fmpq_t x, y, z; mpq_t X, Y, Z; fmpq_init(x); fmpq_init(y); fmpq_init(z); mpq_init(X); mpq_init(Y); mpq_init(Z); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_randtest(z, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_get_mpq(Z, z); fmpq_submul(x, y, z); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_submul(X, Y, Z); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_submul(x,y,z) != mpq_submul(X,Y,Z)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\nz = "); fmpq_print(z); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); mpq_clear(X); mpq_clear(Y); mpq_clear(Z); } /* x -= x * y */ for (i = 0; i < 10000; i++) { fmpq_t x, y; mpq_t X, Y; fmpq_init(x); fmpq_init(y); mpq_init(X); mpq_init(Y); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_submul(x, x, y); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_submul(X, X, Y); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_submul(x,x,y) != mpq_submul(X,X,Y)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); mpq_clear(X); mpq_clear(Y); } /* x -= y * x */ for (i = 0; i < 10000; i++) { fmpq_t x, y; mpq_t X, Y; fmpq_init(x); fmpq_init(y); mpq_init(X); mpq_init(Y); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_submul(x, y, x); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_submul(X, Y, X); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_submul(x,y,x) != mpq_submul(X,Y,X)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); mpq_clear(X); mpq_clear(Y); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }