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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use super::{HasDigits, HasApproximateDigits};
/// Can be truncated into `Truncation`
pub trait CanTruncate: HasDigits {
/// Output of quotient
type Quotient: HasDigits;
/// Output of truncation
type Truncation: HasDigits;
/// Split adic into digits [0, n) and [n, ...).
/// This splits the number into remainder and quotient.
/// See also: [`into_split`](Self::into_split)
///
/// `a = (a % p^n) + p^n * (a // p^n)`
///
/// ```
/// # use adic::{traits::CanTruncate, EAdic, QAdic, UAdic, ZAdic};
/// assert_eq!((UAdic::new(5, vec![1, 2]), UAdic::new(5, vec![3, 4])), UAdic::new(5, vec![1, 2, 3, 4]).split(2));
/// assert_eq!((UAdic::new(5, vec![1, 2, 4, 4]), EAdic::new_neg(5, vec![])), EAdic::new_neg(5, vec![1, 2]).split(4));
/// assert_eq!(
/// (UAdic::new(7, vec![1, 2, 3, 4, 5, 3]), EAdic::new_repeating(7, vec![], vec![4, 5, 3])),
/// EAdic::new_repeating(7, vec![1, 2], vec![3, 4, 5]).split(6)
/// );
/// assert_eq!(
/// (UAdic::new(5, vec![1, 2]), ZAdic::new_approx(5, 2, vec![3, 4])),
/// ZAdic::new_approx(5, 4, vec![1, 2, 3, 4]).split(2)
/// );
///
/// let r = EAdic::new_repeating(7, vec![1, 2], vec![3, 4, 5]);
/// assert_eq!("(543)21._7", r.to_string());
/// let q = QAdic::new(r, -6);
/// assert_eq!("(354).354321_7", q.to_string());
/// let (q_rem, q_int) = q.split(1);
/// assert_eq!("4.354321_7", q_rem.to_string());
/// assert_eq!("(435)._7", q_int.to_string());
/// let (q_rem, q_int) = q.split(-5);
/// assert_eq!("0.000001_7", q_rem.to_string());
/// assert_eq!("(543)2._7", q_int.to_string());
/// let (q_rem, q_int) = q.split(-7);
/// assert_eq!("0._7", q_rem.to_string());
/// assert_eq!("(543)210._7", q_int.to_string());
/// ```
///
/// Note especially the approximate behavior.
/// E.g. splitting an integer past certainty gives a `UAdic` and an empty `ZAdic`!
///
/// ```
/// # use adic::{traits::CanTruncate, UAdic, ZAdic};
/// assert_eq!(
/// (UAdic::new(5, vec![1, 2]), ZAdic::new_approx(5, 0, vec![])),
/// ZAdic::new_approx(5, 2, vec![1, 2]).split(4)
/// );
/// ```
fn split(&self, n: Self::DigitIndex) -> (Self::Truncation, Self::Quotient);
/// Split adic into digits [0, n) and [n, ...).
/// This splits the number into remainder and quotient.
/// See also: [`split`](Self::split)
///
/// `a = (a % p^n) + p^n * (a // p^n)`
///
/// ```
/// # use adic::{traits::CanTruncate, EAdic, QAdic, UAdic, ZAdic};
/// assert_eq!((UAdic::new(5, vec![1, 2]), UAdic::new(5, vec![3, 4])), UAdic::new(5, vec![1, 2, 3, 4]).into_split(2));
/// assert_eq!((UAdic::new(5, vec![1, 2, 4, 4]), EAdic::new_neg(5, vec![])), EAdic::new_neg(5, vec![1, 2]).into_split(4));
/// assert_eq!(
/// (UAdic::new(7, vec![1, 2, 3, 4, 5, 3]), EAdic::new_repeating(7, vec![], vec![4, 5, 3])),
/// EAdic::new_repeating(7, vec![1, 2], vec![3, 4, 5]).into_split(6)
/// );
/// assert_eq!(
/// (UAdic::new(5, vec![1, 2]), ZAdic::new_approx(5, 2, vec![3, 4])),
/// ZAdic::new_approx(5, 4, vec![1, 2, 3, 4]).into_split(2)
/// );
///
/// let r = EAdic::new_repeating(7, vec![1, 2], vec![3, 4, 5]);
/// assert_eq!("(543)21._7", r.to_string());
/// let q = QAdic::new(r, -6);
/// assert_eq!("(354).354321_7", q.to_string());
/// let (q_rem, q_int) = q.clone().into_split(1);
/// assert_eq!("4.354321_7", q_rem.to_string());
/// assert_eq!("(435)._7", q_int.to_string());
/// let (q_rem, q_int) = q.clone().into_split(-5);
/// assert_eq!("0.000001_7", q_rem.to_string());
/// assert_eq!("(543)2._7", q_int.to_string());
/// let (q_rem, q_int) = q.clone().into_split(-7);
/// assert_eq!("0._7", q_rem.to_string());
/// assert_eq!("(543)210._7", q_int.to_string());
/// ```
///
/// Note especially the approximate behavior: splitting past certainty gives a `UAdic` and an empty `ZAdic`!
///
/// ```
/// # use adic::{traits::CanTruncate, UAdic, ZAdic};
/// assert_eq!(
/// (UAdic::new(5, vec![1, 2]), ZAdic::new_approx(5, 0, vec![])),
/// ZAdic::new_approx(5, 2, vec![1, 2]).into_split(4)
/// );
/// ```
fn into_split(self, n: Self::DigitIndex) -> (Self::Truncation, Self::Quotient);
/// Truncate an adic number's expansion to n.
/// This can be thought of as the remainder `a % p^n`.
/// See also: [`into_truncation`](Self::into_truncation)
///
/// ```
/// # use adic::{traits::CanTruncate, EAdic, QAdic, UAdic};
/// let u = UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]);
/// assert_eq!(u, u.truncation(9));
/// assert_eq!(u, u.truncation(7));
/// let r = EAdic::new_repeating(5, vec![1, 3], vec![2, 1]);
/// assert_eq!(u, r.truncation(7));
///
/// let u = QAdic::new(UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]), -2);
/// assert_eq!(u, u.truncation(7));
/// assert_eq!(u, u.truncation(5));
/// let r = QAdic::new(EAdic::new_repeating(5, vec![1, 3], vec![2, 1]), -2);
/// assert_eq!(u, r.truncation(5));
/// ```
fn truncation(&self, n: Self::DigitIndex) -> Self::Truncation
where Self: Sized {
self.split(n).0
}
/// Truncate an adic number's expansion to n.
/// This can be thought of as the remainder `a % p^n`.
/// See also: [`truncation`](Self::truncation)
///
/// ```
/// # use adic::{traits::CanTruncate, EAdic, QAdic, UAdic};
/// let u = UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]);
/// assert_eq!(u, u.clone().into_truncation(9));
/// assert_eq!(u, u.clone().into_truncation(7));
/// let r = EAdic::new_repeating(5, vec![1, 3], vec![2, 1]);
/// assert_eq!(u, r.into_truncation(7));
///
/// let u = QAdic::new(UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]), -2);
/// assert_eq!(u, u.clone().into_truncation(7));
/// assert_eq!(u, u.clone().into_truncation(5));
/// let r = QAdic::new(EAdic::new_repeating(5, vec![1, 3], vec![2, 1]), -2);
/// assert_eq!(u, r.into_truncation(5));
/// ```
fn into_truncation(self, n: Self::DigitIndex) -> Self::Truncation
where Self: Sized {
self.into_split(n).0
}
#[must_use]
/// Divide an adic number by p^n.
/// This can be thought of as the quotient a // p^n
/// See also: [`into_quotient`](Self::into_quotient)
///
/// ```
/// # use adic::{traits::CanTruncate, QAdic, UAdic};
/// let u = UAdic::new(5, vec![1, 2, 3, 4]);
/// let q = u.quotient(2);
/// assert_eq!(UAdic::new(5, vec![3, 4]), q);
///
/// let q = QAdic::new(UAdic::new(5, vec![1, 2, 3, 4]), -1);
/// assert_eq!("432.1_5", q.to_string());
/// let quot = q.quotient(2);
/// assert_eq!(UAdic::new(5, vec![4]), quot);
/// ```
fn quotient(&self, n: Self::DigitIndex) -> Self::Quotient
where Self: Sized {
self.split(n).1
}
#[must_use]
/// Divide an adic number by p^n.
/// This can be thought of as the quotient a // p^n
/// See also: [`quotient`](Self::quotient)
///
/// ```
/// # use adic::{traits::CanTruncate, QAdic, UAdic};
/// let u = UAdic::new(5, vec![1, 2, 3, 4]);
/// let q = u.into_quotient(2);
/// assert_eq!(UAdic::new(5, vec![3, 4]), q);
///
/// let q = QAdic::new(UAdic::new(5, vec![1, 2, 3, 4]), -1);
/// assert_eq!("432.1_5", q.to_string());
/// let quot = q.quotient(2);
/// assert_eq!(UAdic::new(5, vec![4]), quot);
/// ```
fn into_quotient(self, n: Self::DigitIndex) -> Self::Quotient
where Self: Sized {
self.into_split(n).1
}
}
/// Can be approximated into `Approximate`
pub trait CanApproximate: HasDigits {
/// Output of the approximation
type Approximation: HasApproximateDigits;
/// Approximate an number expansion to digit index `n`.
/// See also: [`into_approximation`](Self::into_approximation)
///
/// ```
/// # use adic::{traits::CanApproximate, EAdic, QAdic, UAdic, ZAdic};
/// let u = UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]);
/// let z = ZAdic::new_approx(5, 9, vec![1, 3, 2, 1, 2, 1, 2, 0, 0]);
/// let zs = ZAdic::new_approx(5, 5, vec![1, 3, 2, 1, 2]);
/// assert_eq!(z, u.approximation(9));
/// assert_eq!(zs, u.approximation(5));
/// assert_eq!(zs, z.approximation(5));
/// assert_eq!(zs, zs.approximation(9));
/// let r = EAdic::new_repeating(5, vec![1, 3], vec![2, 1]);
/// assert_eq!(zs, r.approximation(5));
///
/// let q = QAdic::new(UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]), -5);
/// let z = QAdic::new(ZAdic::new_approx(5, 9, vec![1, 3, 2, 1, 2, 1, 2, 0, 0]), -5);
/// let zs = QAdic::new(ZAdic::new_approx(5, 6, vec![1, 3, 2, 1, 2, 1]), -5);
/// assert_eq!(z, q.approximation(4));
/// assert_eq!(zs, q.approximation(1));
/// let qr = QAdic::new(EAdic::new_repeating(5, vec![1, 3], vec![2, 1]), -5);
/// assert_eq!(zs, qr.approximation(1));
/// ```
fn approximation(&self, n: Self::DigitIndex) -> Self::Approximation;
/// Consume and get the approximation to digit index `n`.
/// See also: [`approximation`](Self::approximation)
///
/// ```
/// # use adic::{traits::CanApproximate, EAdic, QAdic, UAdic, ZAdic};
/// let u = UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]);
/// let z = ZAdic::new_approx(5, 9, vec![1, 3, 2, 1, 2, 1, 2, 0, 0]);
/// let zs = ZAdic::new_approx(5, 5, vec![1, 3, 2, 1, 2]);
/// assert_eq!(z, u.clone().into_approximation(9));
/// assert_eq!(zs, u.clone().into_approximation(5));
/// assert_eq!(zs, z.clone().into_approximation(5));
/// assert_eq!(zs, zs.clone().into_approximation(9));
/// let r = EAdic::new_repeating(5, vec![1, 3], vec![2, 1]);
/// assert_eq!(zs, r.into_approximation(5));
///
/// let q = QAdic::new(UAdic::new(5, vec![1, 3, 2, 1, 2, 1, 2]), -5);
/// let z = QAdic::new(ZAdic::new_approx(5, 9, vec![1, 3, 2, 1, 2, 1, 2, 0, 0]), -5);
/// let zs = QAdic::new(ZAdic::new_approx(5, 6, vec![1, 3, 2, 1, 2, 1]), -5);
/// assert_eq!(z, q.clone().into_approximation(4));
/// assert_eq!(zs, q.clone().into_approximation(1));
/// let qr = QAdic::new(EAdic::new_repeating(5, vec![1, 3], vec![2, 1]), -5);
/// assert_eq!(zs, qr.into_approximation(1));
/// ```
fn into_approximation(self, n: Self::DigitIndex) -> Self::Approximation;
}