1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use malachite_base::num::conversion::traits::{ExactFrom, PowerOf2Digits};
use malachite_base::num::logic::traits::LowMask;
use malachite_base::rational_sequences::RationalSequence;
use malachite_nz::natural::Natural;
use Rational;
impl Rational {
/// Converts base-$2^k$ digits to a [`Rational`]. The inputs are taken by value.
///
/// The input consists of the digits of the integer portion of the [`Rational`] and the digits
/// of the fractional portion. The integer-portion digits are ordered from least- to
/// most-significant, and the fractional-portion digits from most- to least.
///
/// The fractional-portion digits may end in infinitely many zeros or $(2^k-1)$s; these are
/// handled correctly.
///
/// # Worst-case complexity
/// $T(n, m) = O(nm)$
///
/// $M(n, m) = O(nm)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is
/// `max(before_point.len(), after_point.component_len())`, and $m$ is
/// `base.significant_bits()`.
///
/// # Panics
/// Panics if `log_base` is zero.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::rational_sequences::RationalSequence;
/// use malachite_base::vecs::vec_from_str;
/// use malachite_q::Rational;
///
/// let before_point = vec_from_str("[1, 1]").unwrap();
/// let after_point = RationalSequence::from_vecs(
/// vec_from_str("[0]").unwrap(),
/// vec_from_str("[0, 0, 1]").unwrap(),
/// );
/// assert_eq!(
/// Rational::from_power_of_2_digits(1, before_point, after_point).to_string(),
/// "43/14"
/// );
///
/// // 21.34565656..._32
/// let before_point = vec_from_str("[1, 2]").unwrap();
/// let after_point = RationalSequence::from_vecs(
/// vec_from_str("[3, 4]").unwrap(),
/// vec_from_str("[5, 6]").unwrap(),
/// );
/// assert_eq!(
/// Rational::from_power_of_2_digits(5, before_point, after_point).to_string(),
/// "34096673/523776"
/// );
/// ```
pub fn from_power_of_2_digits(
log_base: u64,
before_point: Vec<Natural>,
after_point: RationalSequence<Natural>,
) -> Rational {
let (non_repeating, repeating) = after_point.into_vecs();
let r_len = u64::exact_from(repeating.len());
let nr_len = u64::exact_from(non_repeating.len());
let nr =
Natural::from_power_of_2_digits_asc(log_base, non_repeating.into_iter().rev()).unwrap();
let r = Natural::from_power_of_2_digits_asc(log_base, repeating.into_iter().rev()).unwrap();
let floor = Rational::from(
Natural::from_power_of_2_digits_asc(log_base, before_point.into_iter()).unwrap(),
);
floor
+ if r == 0u32 {
Rational::from(nr) >> (log_base * nr_len)
} else {
(Rational::from_naturals(r, Natural::low_mask(log_base * r_len))
+ Rational::from(nr))
>> (log_base * nr_len)
}
}
/// Converts base-$2^k$ digits to a [`Rational`]. The inputs are taken by reference.
///
/// The input consists of the digits of the integer portion of the [`Rational`] and the digits
/// of the fractional portion. The integer-portion digits are ordered from least- to
/// most-significant, and the fractional-portion digits from most- to least.
///
/// The fractional-portion digits may end in infinitely many zeros or $(2^k-1)$s; these are
/// handled correctly.
///
/// # Worst-case complexity
/// $T(n, m) = O(nm)$
///
/// $M(n, m) = O(nm)$
///
/// where $T$ is time, $M$ is additional memory, $n$ is
/// `max(before_point.len(), after_point.component_len())`, and $m$ is
/// `base.significant_bits()`.
///
/// # Panics
/// Panics if `log_base` is zero.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::rational_sequences::RationalSequence;
/// use malachite_base::vecs::vec_from_str;
/// use malachite_q::Rational;
///
/// let before_point = vec_from_str("[1, 1]").unwrap();
/// let after_point = RationalSequence::from_vecs(
/// vec_from_str("[0]").unwrap(),
/// vec_from_str("[0, 0, 1]").unwrap(),
/// );
/// assert_eq!(
/// Rational::from_power_of_2_digits_ref(1, &before_point, &after_point).to_string(),
/// "43/14"
/// );
///
/// // 21.34565656..._32
/// let before_point = vec_from_str("[1, 2]").unwrap();
/// let after_point = RationalSequence::from_vecs(
/// vec_from_str("[3, 4]").unwrap(),
/// vec_from_str("[5, 6]").unwrap(),
/// );
/// assert_eq!(
/// Rational::from_power_of_2_digits_ref(5, &before_point, &after_point).to_string(),
/// "34096673/523776"
/// );
/// ```
pub fn from_power_of_2_digits_ref(
log_base: u64,
before_point: &[Natural],
after_point: &RationalSequence<Natural>,
) -> Rational {
let (non_repeating, repeating) = after_point.to_vecs();
let r_len = u64::exact_from(repeating.len());
let nr_len = u64::exact_from(non_repeating.len());
let nr =
Natural::from_power_of_2_digits_asc(log_base, non_repeating.into_iter().rev()).unwrap();
let r = Natural::from_power_of_2_digits_asc(log_base, repeating.into_iter().rev()).unwrap();
let floor = Rational::from(
Natural::from_power_of_2_digits_asc(log_base, before_point.iter().cloned()).unwrap(),
);
floor
+ if r == 0u32 {
Rational::from(nr) >> (log_base * nr_len)
} else {
(Rational::from_naturals(r, Natural::low_mask(log_base * r_len))
+ Rational::from(nr))
>> (log_base * nr_len)
}
}
}