continuous.rs
3.37 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
//
// A «continued fraction» is a representation of a fraction as a vector
// of integrals… Irrational fractions will result in infinite most of the
// time repetitive vectors. They can be used to get a resonable approximation
// for sqrt on fractionals.
//
// 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 crate::Fractional;
#[derive(Debug)]
pub struct Continuous (Vec<i64>);
impl Continuous {
// calculate a sqrt as continued fraction sequence. Taken from:
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#
// Continued_fraction_expansion
pub fn sqrt(x :i64, a0 :i64) -> Self {
fn inner(v :&mut [i64], x :i64, a0 :i64, mn :i64, dn :i64, an :i64) {
let mn_1 = dn * an - mn;
let dn_1 = (x - mn_1 * mn_1) / dn;
let an_1 = (a0 + mn_1) / dn_1;
v[0] = an;
// The convergence criteria „an_1 == 2 * a0“ is not good for
// very small x thus I decided to break the iteration at constant
// time. Which is the 10 below.
if v.len() > 1 {
inner(&mut v[1..], x, a0, mn_1, dn_1, an_1);
}
}
let mut v :Vec<i64> = vec!(0; 10);
inner(&mut v, x, a0, 0, 1, a0);
Continuous(v)
}
pub fn into_prec(&self, prec :usize) -> Fractional {
let Continuous(c) = self;
let p = if prec <= c.len() { prec } else { c.len() };
let to_frac = |acc :Fractional, x :&i64| {
let Fractional(an, ad) = acc.noreduce_add((*x).into());
Fractional(ad, an)
};
let Fractional(n, d) = c[..p]
. into_iter()
. rev()
. fold(Fractional(0, 1), to_frac);
Fractional(d, n)
}
}
impl From<&Fractional> for Continuous {
// general continous fraction form of a fractional...
fn from(x :&Fractional) -> Self {
fn inner(mut v :Vec<i64>, f :Fractional) -> Vec<i64> {
let Fractional(n, d) = f;
let a = n / d;
let Fractional(_n, _d) = f.noreduce_sub(a.into());
v.push(a);
match _n {
1 => { v.push(_d); v },
0 => v,
_ => inner(v, Fractional(_d, _n)),
}
}
Continuous(inner(Vec::new(), *x))
}
}
impl Into<Fractional> for &Continuous {
fn into(self) -> Fractional {
let Continuous(c) = self;
let Fractional(n, d) = c.iter().rev().fold( Fractional(0, 1)
, |acc, x| {
let Fractional(an, ad) = acc + (*x).into();
Fractional(ad, an)
});
Fractional(d, n)
}
}