fractional.rs
10.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Some code to support fractional numbers for full precision rational number
// calculations. (At least for the standard operations.)
// This also implements a sqrt on fractional numbers, which can not be precise
// because of the irrational nature of most sqare roots.
// Fractions can only represent rational numbers precise.
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::cmp::Ordering;
use std::ops::{Add,Sub,Neg,Mul,Div};
use std::fmt;
use std::convert::{TryFrom, TryInto};
use std::num::TryFromIntError;
#[derive(Debug, Eq, Clone, Copy)]
pub struct Fractional (pub i64, pub i64);
pub type Continuous = Vec<i64>;
pub type Error = &'static str;
#[inline]
fn hcf(x :i64, y :i64) -> i64 {
match y {
0 => x,
_ => hcf(y, x % y),
}
}
// calculate a sqrt as continued fraction sequence. Taken from:
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Continued_fraction_expansion
fn sqrt_cfrac(x :i64, a0 :i64) -> Continuous {
let v :Continuous = Vec::new();
fn inner(mut v :Continuous,
x :i64,
a0 :i64,
mn :i64,
dn :i64,
an :i64) -> Continuous {
let mn_1 = dn * an - mn;
let dn_1 = (x - mn_1 * mn_1) / dn;
let an_1 = (a0 + mn_1) / dn_1;
v.push(an);
match v.len() {
10 => v,
_ => inner(v, x, a0, mn_1, dn_1, an_1),
}
// This convergence criterium is not good for very small x
// thus I decided to break the iteration at constant time.
// match an_1 == 2 * a0 {
// true => v,
// _ => inner(v, x, a0, mn_1, dn_1, an_1),
// }
}
inner(v, x, a0, 0, 1, a0)
}
impl Fractional {
#[inline]
pub fn gcd(self, other: Self) -> i64 {
let Fractional(_, d1) = self;
let Fractional(_, d2) = other;
(d1 * d2) / hcf(d1, d2)
}
#[inline]
pub fn reduce(self) -> Self {
let Fractional(n, d) = self;
let (_n, _d) = if n > d { (n, d) } else { (d, n) };
// if the difference from _n % _d to _n is very big we are close to
// a whole number and can ignore the fractional part... this reduces
// the precision but ensures smaller numbers for numerator and
// denominator.
if _d > 1 && (_n % _d) * 10000000 < _n {
Self(_n / _d, 1)
} else {
Self(n / hcf(n, d), d / hcf(n, d))
}
}
#[inline]
pub fn numerator(self) -> i64 {
self.0
}
#[inline]
pub fn denominator(self) -> i64 {
self.1
}
// This is a really bad approximation of sqrt for a fractional...
// for (9/3) it will result 3 which if way to far from the truth,
// which is ~1.7320508075
// BUT we can use this value as starting guess for creating a
// continous fraction for the sqrt... and create a much better
// fractional representation of the sqrt.
// So, if inner converges, but is not a perfect square (does not
// end up in an Ordering::Equal - which is the l > h case)
// we use the l - 1 as starting guess for sqrt_cfrac.
// taken from:
// https://www.geeksforgeeks.org/square-root-of-an-integer/
pub fn sqrt(self) -> Result<Self, Error> {
// find the sqrt of x in O(log x/2).
// This stops if a perfect sqare was found. Else it passes
// the found value as starting guess to the continous fraction
// sqrt function.
fn floor_sqrt(x :i64) -> Fractional {
fn inner(l :i64, h :i64, x :i64) -> Fractional {
if l > h {
(&sqrt_cfrac(x, l - 1)).into()
} else {
let m = (l + h) / 2;
match x.cmp(&(m * m)) {
Ordering::Equal => m.into(),
Ordering::Less => inner(l, m - 1, x),
Ordering::Greater => inner(m + 1, h, x),
}
}
}
match x {
0 => 0.into(),
1 => 1.into(),
_ => inner(1, x / 2, x),
}
}
let Fractional(n, d) = self;
let n = match n.cmp(&0) {
Ordering::Equal => 0.into(),
Ordering::Less => return Err("sqrt on negative undefined"),
Ordering::Greater => floor_sqrt(n),
};
let d = match d.cmp(&0) {
Ordering::Equal => 0.into(),
Ordering::Less => return Err("sqrt on negative undefined"),
Ordering::Greater => floor_sqrt(d),
};
Ok(n / d)
}
}
impl fmt::Display for Fractional {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}/{})", self.0, self.1)
}
}
impl From<i64> for Fractional {
fn from(x: i64) -> Self {
Self(x, 1)
}
}
impl TryFrom<usize> for Fractional {
type Error = &'static str;
fn try_from(x: usize) -> Result<Self, Self::Error> {
let v = i64::try_from(x);
match v {
Err(_) => Err("Conversion from usize to i32 failed"),
Ok(_v) => Ok(Self(_v, 1)),
}
}
}
pub fn from_vector(xs: &Vec<i64>) -> Vec<Fractional> {
xs.iter().map(|x| Fractional(*x, 1)).collect()
}
impl TryInto<f64> for Fractional {
type Error = TryFromIntError;
fn try_into(self) -> Result<f64, Self::Error> {
let n :i32 = self.0.try_into()?;
let d :i32 = self.1.try_into()?;
Ok(f64::from(n) / f64::from(d))
}
}
impl TryInto<(i32, i32)> for Fractional {
type Error = TryFromIntError;
fn try_into(self) -> Result<(i32, i32), Self::Error> {
let a :i32 = (self.0 / self.1).try_into()?;
let b :i32 = (self.0 % self.1).try_into()?;
Ok((a, b))
}
}
impl Into<Continuous> for Fractional {
// general continous fraction form of a fractional...
fn into(self) -> Continuous {
let v :Continuous = Vec::new();
fn inner(mut v :Continuous, f :Fractional) -> Continuous {
let Fractional(n, d) = f;
let a = n / d;
let Fractional(_n, _d) = f - a.into();
v.push(a);
match _n {
1 => { v.push(_d); v },
_ => inner(v, Fractional(_d, _n)),
}
}
inner(v, self)
}
}
impl From<&Continuous> for Fractional {
fn from(x :&Continuous) -> Self {
let Self(n, d) = x.iter().rev().fold(Fractional(0, 1), |acc, x| {
let Self(an, ad) = acc + (*x).into();
Self(ad, an)
});
Self(d, n)
}
}
impl PartialEq for Fractional {
fn eq(&self, other: &Self) -> bool {
let Fractional(n1, d1) = self;
let Fractional(n2, d2) = other;
n1 * (self.gcd(*other) / d1) == n2 * (self.gcd(*other) / d2)
}
}
impl PartialOrd for Fractional {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Fractional {
fn cmp(&self, other: &Self) -> Ordering {
let Fractional(n1, d1) = self;
let Fractional(n2, d2) = other;
let x = n1 * (self.gcd(*other) / d1);
let y = n2 * (self.gcd(*other) / d2);
x.cmp(&y)
}
}
impl Add for Fractional {
type Output = Self;
fn add(self, other: Self) -> Self {
let Fractional(n1, d1) = self;
let Fractional(n2, d2) = other;
let n = n1 * (self.gcd(other) / d1) + n2 * (self.gcd(other) / d2);
Self(n, self.gcd(other)).reduce()
}
}
impl Sub for Fractional {
type Output = Self;
fn sub(self, other: Self) -> Self {
self + -other
}
}
impl Neg for Fractional {
type Output = Self;
fn neg(self) -> Self {
let Fractional(n, d) = self;
Self(-n, d).reduce()
}
}
impl Mul for Fractional {
type Output = Self;
fn mul(self, other :Self) -> Self {
let Fractional(n1, d1) = self;
let Fractional(n2, d2) = other;
Self(n1 * n2, d1 * d2).reduce()
}
}
impl Div for Fractional {
type Output = Self;
fn div(self, other: Self) -> Self {
let Fractional(n, d) = other;
self * Fractional(d, n)
}
}
/* some stuff that could be tested...
let x = Fractional(1, 3);
let y = Fractional(1, 6);
println!(
"Greatest common denominator of {} and {}: {}", x, y, x.gcd(y));
println!("Numerator of {}: {}", x, x.numerator());
println!("Denominator of {}: {}", x, x.denominator());
assert_eq!(Fractional(1, 3), Fractional(2, 6));
assert_eq!(Fractional(1, 3), Fractional(1, 3));
assert_eq!(y < x, true);
assert_eq!(y > x, false);
assert_eq!(x == y, false);
assert_eq!(x == x, true);
assert_eq!(x + y, Fractional(1, 2));
println!("{} + {} = {}", x, y, x + y);
assert_eq!(x - y, Fractional(1, 6));
println!("{} - {} = {}", x, y, x - y);
assert_eq!(y - x, Fractional(-1, 6));
println!("{} - {} = {}", y, x, y - x);
assert_eq!(-x, Fractional(-1, 3));
println!("-{} = {}", x, -x);
assert_eq!(x * y, Fractional(1, 18));
println!("{} * {} = {}", x, y, x * y);
assert_eq!(x / y, Fractional(2, 1));
println!("{} / {} = {}", x, y, x / y);
assert_eq!(y / x, Fractional(1, 2));
println!("{} / {} = {}", y, x, y / x);
println!("Fractional from 3: {}", Fractional::from(3));
let z :f64 = Fractional::into(x);
println!("Floating point of {}: {}", x, z);
let (d, r) = Fractional::into(x);
println!("(div, rest) of {}: ({}, {})", x, d, r);
*/