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
use crate::base::ring::num_integer::Integer;
use crate::base::ring::repr::*;

use std::cmp::Ordering;
use std::ops::{Add, Div, Mul, Neg, Sub};

#[derive(Debug, Clone, Hash, Copy)]
pub struct Rational {
  num: Integer,
  den: Integer,
}

impl Rational {
  #[inline]
  pub const fn new(num: Integer, den: Integer) -> Rational {
    Rational {
      // Q
      num,
      den,
    }
  }

  pub fn zero() -> Rational {
    Rational::new(
      0, //.
      1,
    )
  }
  pub fn one() -> Rational {
    Rational::new(
      1, //.
      1,
    )
  }

  // decompose
  // self = self.trunc() + self.fract()

  pub fn trunc(&self) -> Rational { Rational::from(self.num / self.den) }

  pub fn fract(&self) -> Rational { Rational::new(self.num % self.den, self.den) }

  pub fn is_positive(&self) -> bool { !self.is_negative() }
  pub fn is_negative(&self) -> bool {
    (self.num < 0 && self.den > 0) //.
      || (self.num > 0 && self.den < 0)
  }
}

impl Domain for Rational {
  fn name(&self) -> String { String::from("ℚ") }

  fn num(&self) -> i128 { self.num }
  fn den(&self) -> i128 { self.den }

  /// ```gcd(a/b, c/d) = gcd(a*d, c*b)/(b*d)```
  fn gcd(u: &Self, v: &Self) -> Self {
    let (a, c) = (u.num, v.num);
    let (b, d) = (u.den, v.den);
    let ad = a * d;
    let cb = c * b;

    Self::new(
      Integer::gcd(&ad, &cb), //.
      b * d,
    )
  }

  /// ```lcm(a/b, c/d) = lcm(a, c)/gcd(b, d)```
  fn lcm(u: &Self, v: &Self) -> Self {
    let (a, c) = (u.num, v.num);
    let (b, d) = (u.den, v.den);

    Self::new(
      Integer::lcm(&a, &c), //.
      Integer::gcd(&b, &d),
    )
  }
}

impl Ring for Rational {}
impl Field for Rational {}

impl PartialEq for Rational {
  fn eq(&self, other: &Rational) -> bool { self.cmp(other) == Ordering::Equal }
}

impl Eq for Rational {}
impl PartialOrd for Rational {
  fn partial_cmp(&self, other: &Rational) -> Option<Ordering> { Some(self.cmp(other)) }
}

impl Ord for Rational {
  fn cmp(
    &self,
    other: &Rational, // finite recursive quo/rem cmp
  ) -> Ordering {
    let (lhs_int, rhs_int) = (self.num / self.den, other.num / other.den);
    let (lhs_rem, rhs_rem) = (self.num % self.den, other.num % other.den);
    let int_cmp = lhs_int.cmp(&rhs_int);

    if int_cmp != Ordering::Equal {
      int_cmp
    } else {
      match (lhs_rem != 0, rhs_rem != 0) {
        (false, false) => {
          Ordering::Equal //.
        }
        (false, true) => {
          Ordering::Less //.
        }
        (true, false) => {
          Ordering::Greater //.
        }
        (true, true) => {
          // ```a/(a % b) < c/(c % d)```
          let lhs_recip = Rational::new(self.den, lhs_rem);
          let rhs_recip = Rational::new(other.den, rhs_rem);
          lhs_recip.cmp(&rhs_recip).reverse()
        }
      }
    }
  }
}

impl From<Integer> for Rational {
  fn from(n: Integer) -> Self { Rational { num: n, den: 1 } }
}

impl Add for Rational {
  type Output = Rational;

  /// ```a/b + c/d = (a*lcm/b + c*lcm/d)/lcm where lcm = lcm(b,d)```
  fn add(self, o: Self) -> Self::Output {
    let (a, c) = (self.num, o.num);
    let (b, d) = (self.den, o.den);

    let lcm = Integer::lcm(&b, &d);
    Rational::new(
      a * lcm / b + c * lcm / d, //.
      lcm,
    )
  }
}

impl Sub for Rational {
  type Output = Rational;

  /// ```a/b - c/d = (lcm/b*a - lcm/d*c)/lcm, where lcm = lcm(b,d)```
  fn sub(self, o: Self) -> Self::Output {
    let (a, c) = (self.num, o.num);
    let (b, d) = (self.den, o.den);

    let lcm = Integer::lcm(&b, &d);
    Rational::new(
      lcm / b * a - lcm / d * c, //.
      lcm,
    )
  }
}

impl Mul for Rational {
  type Output = Rational;

  /// ```a/b * c/d = (a/gcd_ad)*(c/gcd_bc) / ((d/gcd_ad)*(b/gcd_bc))```
  fn mul(self, o: Self) -> Self::Output {
    let (a, c) = (self.num, o.num);
    let (b, d) = (self.den, o.den);

    let gcd_ad = Integer::gcd(&a, &d);
    let gcd_bc = Integer::gcd(&b, &c);

    Rational::new(
      a / gcd_ad * c / gcd_bc, //.
      d / gcd_ad * b / gcd_bc,
    )
  }
}

impl Div for Rational {
  type Output = Rational;

  /// ```a/b / c/d = (a/gcd_ac)*(d/gcd_bd) / ((c/gcd_ac)*(b/gcd_bd))```
  fn div(self, o: Self) -> Self::Output {
    let (a, c) = (self.num, o.num);
    let (b, d) = (self.den, o.den);

    let gcd_ac = Integer::gcd(&a, &c);
    let gcd_bd = Integer::gcd(&b, &d);

    Rational::new(
      a / gcd_ac * d / gcd_bd, //.
      c / gcd_ac * b / gcd_bd,
    )
  }
}

impl Neg for Rational {
  type Output = Rational;

  fn neg(self) -> Rational { Rational::new(-self.num, self.den) }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn dom() {
    let r1_2 = Rational::new(1, 2);
    let r2_1 = Rational::new(2, 1);
    let r2_5 = Rational::new(2, 5);
    let r4_5 = Rational::new(4, 5);
    let r7_2 = Rational::new(7, 2);
    let r3_9 = Rational::new(3, 9);

    // trivial path: u, v = 0
    assert_eq!(Domain::gcd(&r1_2, &Rational::zero()), r1_2);
    assert_eq!(Domain::lcm(&r1_2, &Rational::zero()), Rational::zero());
    // u = v
    assert_eq!(Domain::gcd(&r2_5, &r2_5), r2_5);
    assert_eq!(Domain::lcm(&r2_5, &r2_5), r2_5);
    // v % u = 0
    assert_eq!(Domain::gcd(&r2_5, &r4_5), r2_5);
    assert_eq!(Domain::lcm(&r2_5, &r4_5), r4_5);
    // v % u != 0
    assert_eq!(Domain::gcd(&r7_2, &r4_5), Rational::new(1, 10));
    assert_eq!(Domain::gcd(&r1_2, &r3_9), Rational::new(3, 18));
    assert_eq!(Domain::lcm(&r2_5, &r7_2), Rational::new(14, 1));
    assert_eq!(Domain::lcm(&r3_9, &r2_1), Rational::new(6, 1));

    let m0 = Rational::new(13, 121);
    let u0 = Rational::new(234, 19);
    let v0 = Rational::new(32, 7);

    // commutative ```gcd(u, v) = gcd(v, u)```
    assert_eq!(Domain::gcd(&u0, &v0), Domain::gcd(&v0, &u0));
    // associative ```gcd(u, gcd(v, m)) = gcd(gcd(u, v), m)```
    assert_eq!(Domain::gcd(&u0, &Domain::gcd(&v0, &m0)), Domain::gcd(&Domain::gcd(&u0, &v0), &m0));

    // ```lcm(u, gcd(u, v)) = u```
    assert_eq!(Domain::lcm(&u0, &Domain::gcd(&u0, &v0)), u0);
    // ```lcm(u, gcd(v, m)) = gcd(lcm(u, v), lcm(u, m))```
    assert_eq!(Domain::lcm(&u0, &Domain::gcd(&v0, &m0)), Domain::gcd(&Domain::lcm(&u0, &v0), &Domain::lcm(&u0, &m0)));
    // ```lcm(u, v)*gcd(u, v) = u*v```
    assert_eq!(Domain::lcm(&u0, &v0) * Domain::gcd(&u0, &v0), u0 * v0);
  }

  #[test]
  fn ops() {
    let r1_1 = Rational::one();
    let r1_2 = Rational::new(1, 2);
    let r2_1 = Rational::new(2, 1);
    let n1_2 = Rational::new(-1, 2);
    let r3_7 = Rational::new(3, 7);
    let r9_4 = Rational::new(9, 4);

    // u = v
    assert_eq!(r1_2 + r1_2, Rational::new(1, 1));
    assert_eq!(r1_2 * r1_2, Rational::new(1, 4));
    // a/b = b/a
    assert_eq!(r1_2 + r2_1, Rational::new(5, 2));
    assert_eq!(r1_2 * r2_1, Rational::new(1, 1));
    // a/b = a/c
    assert_eq!(r1_2 + r1_1, Rational::new(3, 2));
    assert_eq!(r1_2 * r1_1, Rational::new(1, 2));
    // a/b = -a/b
    assert_eq!(r1_2 + n1_2, Rational::new(0, 1));
    assert_eq!(r1_2 * n1_2, Rational::new(-1, 4));
    // a/b = c/d
    assert_eq!(r3_7 + r1_2, Rational::new(13, 14));
    assert_eq!(r3_7 * r1_2, Rational::new(3, 14));
    assert_eq!(r3_7 + r9_4, Rational::new(75, 28));
    assert_eq!(r3_7 * r9_4, Rational::new(27, 28));

    // ```a/b - c/d = a/b + -c/d```
    assert_eq!(r3_7 - r1_2, r3_7 + r1_2.neg());
    assert_eq!(r3_7 - r9_4, r3_7 + r9_4.neg());

    // ```a/b / c/d = a/b * d/c```
    assert_eq!(r3_7 / r1_2, r3_7 * Rational::new(r1_2.den, r1_2.num));
    assert_eq!(r3_7 / r9_4, r3_7 * Rational::new(r9_4.den, r9_4.num));
  }
}