Ejemplo n.º 1
0
Fraction& Fraction::operator+=(const Fraction& rhs)
{
    set_num((get_num() * rhs.get_den()) + (get_den() * rhs.get_num()));
    set_den(get_den() * rhs.get_den());
    set_int(get_int() + rhs.get_int());
    reduce();
    return *this;
}
Ejemplo n.º 2
0
    void bvisit(const Complex &x)
    {

        RCP<const Integer> den, den1, den2;
        RCP<const Integer> num1, num2;

        num1 = integer(get_num(x.real_));
        num2 = integer(get_num(x.imaginary_));

        den1 = integer(get_den(x.real_));
        den2 = integer(get_den(x.imaginary_));
        den = lcm(*den1, *den2);

        num1 = rcp_static_cast<const Integer>(mul(num1, div(den, den1)));
        num2 = rcp_static_cast<const Integer>(mul(num2, div(den, den2)));

        *numer_ = Complex::from_two_nums(*num1, *num2);
        *denom_ = den;
    }
Ejemplo n.º 3
0
flint::fmpqxx URatPSeriesFlint::convert(const rational_class &x) {
    flint::fmpqxx r;
    flint::fmpzxx i1;
    fmpz_set_mpz(i1._data().inner, get_mpz_t(get_num(x)));
    flint::fmpzxx i2;
    fmpz_set_mpz(i2._data().inner, get_mpz_t(get_den(x)));
    r.num() = i1;
    r.den() = i2;
    return r;
}
Ejemplo n.º 4
0
RCP<const Number> harmonic(unsigned long n, long m)
{
    rational_class res(0);
    if (m == 1) {
        for (unsigned i = 1; i <= n; ++i) {
            res += rational_class(1, i);
        }
        return Rational::from_mpq(res);
    } else {
        for (unsigned i = 1; i <= n; ++i) {
            if (m > 0) {
                rational_class t(1u, i);
                mp_pow_ui(get_den(t), get_den(t), m);
                res += t;
            } else {
                integer_class t(i);
                mp_pow_ui(t, t, -m);
                res += t;
            }
        }
        return Rational::from_mpq(res);
    }
}
Ejemplo n.º 5
0
RCP<const Basic> pow(const RCP<const Basic> &a, const RCP<const Basic> &b)
{
    if (is_a_Number(*b) and rcp_static_cast<const Number>(b)->is_zero()) {
        return pownum(rcp_static_cast<const Number>(b), zero);
    }
    if (eq(*b, *one))
        return a;
    if (eq(*a, *zero))
        return zero;
    if (eq(*a, *one))
        return one;
    if (eq(*a, *minus_one)) {
        if (is_a<Integer>(*b)) {
            return is_a<Integer>(*div(b, integer(2))) ? one : minus_one;
        } else if (is_a<Rational>(*b)
                   and (get_num(rcp_static_cast<const Rational>(b)->i) == 1)
                   and (get_den(rcp_static_cast<const Rational>(b)->i) == 2)) {
            return I;
        }
    }

    if (is_a_Number(*a) and is_a_Number(*b)) {
        if (is_a<Integer>(*b)) {
            if (is_a<Rational>(*a)) {
                RCP<const Rational> exp_new
                    = rcp_static_cast<const Rational>(a);
                return exp_new->powrat(*rcp_static_cast<const Integer>(b));
            } else if (is_a<Integer>(*a)) {
                RCP<const Integer> exp_new = rcp_static_cast<const Integer>(a);
                return exp_new->powint(*rcp_static_cast<const Integer>(b));
            } else if (is_a<Complex>(*a)) {
                RCP<const Complex> exp_new = rcp_static_cast<const Complex>(a);
                RCP<const Integer> pow_new = rcp_static_cast<const Integer>(b);
                RCP<const Number> res = exp_new->pow(*pow_new);
                return res;
            } else {
                return rcp_static_cast<const Number>(a)
                    ->pow(*rcp_static_cast<const Number>(b));
            }
        } else if (is_a<Rational>(*b)) {
            if (is_a<Rational>(*a)) {
                return static_cast<const Rational &>(*a)
                    .powrat(static_cast<const Rational &>(*b));
            } else if (is_a<Integer>(*a)) {
                return static_cast<const Rational &>(*b)
                    .rpowrat(static_cast<const Integer &>(*a));
            } else if (is_a<Complex>(*a)) {
                return make_rcp<const Pow>(a, b);
            } else {
                return rcp_static_cast<const Number>(a)
                    ->pow(*rcp_static_cast<const Number>(b));
            }
        } else if (is_a<Complex>(*b)) {
            return make_rcp<const Pow>(a, b);
        } else {
            return rcp_static_cast<const Number>(a)
                ->pow(*rcp_static_cast<const Number>(b));
        }
    }
    if (is_a<Mul>(*a) and is_a_Number(*b)) {
        map_basic_basic d;
        RCP<const Number> coef = one;
        rcp_static_cast<const Mul>(a)
            ->power_num(outArg(coef), d, rcp_static_cast<const Number>(b));
        return Mul::from_dict(coef, std::move(d));
    }
    if (is_a<Pow>(*a) and is_a<Integer>(*b)) {
        // Convert (x**y)**b = x**(b*y), where 'b' is an integer. This holds for
        // any complex 'x', 'y' and integer 'b'.
        RCP<const Pow> A = rcp_static_cast<const Pow>(a);
        return pow(A->get_base(), mul(A->get_exp(), b));
    }
    return make_rcp<const Pow>(a, b);
}