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
mod num_integer;
mod num_rational;
mod poly;
mod repr;

use std::fmt;
use std::ops::{Add, Mul};

pub use num_integer::*;
pub use num_rational::Rational;
pub use repr::*;

pub type SymbolicResult<T> = Result<T, Form>;

#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub enum Number {
  Z(Integer),
  Q(Rational),
}

impl Number {
  pub fn num(&self) -> i128 {
    match self {
      // ```num(z) ∈ ℤ = num(z/1) ∈ ℚ = z,```
      Number::Z(z) => {
        z.num() //.
      }

      // ```num(n/d) ∈ ℚ = n,```
      Number::Q(q) => {
        q.num() //.
      }
    }
  }

  pub fn den(&self) -> i128 {
    match self {
      // ```den(z) ∈ ℤ = den(z/1) ∈ ℚ = 1,```
      Number::Z(z) => {
        z.den() //.
      }

      // ```den(n/d) ∈ ℚ = d,```
      Number::Q(q) => {
        q.den() //.
      }
    }
  }

  pub fn len(&self) -> u64 { 1 + (self.den() != 1) as u64 }

  pub fn dom(&self) -> Set {
    if self.den() != 1 {
      Set::Q
    } else {
      if self.num() < 0 {
        Set::Z
      } else {
        Set::N
      }
    }
  }

  pub fn inv(self) -> SymbolicResult<Number> { Number::Q(Rational::new(self.den(), self.num())).trivial() }

  pub fn pow(self, n: i128) -> SymbolicResult<Number> {
    if self.num() != 0 {
      if n > 0 {
        // ```l^n = 1^(n - 1)*l```
        self.clone().pow(n - 1)?.mul(self)
      } else if n < 0 {
        // ```l^-n = (1/l)^n```
        self.inv()?.pow(-n)
      } else {
        Ok(Number::Z(1))
      }
    } else {
      if n > 0 {
        Ok(Number::Z(0))
      } else {
        Err(Form {})
      }
    }
  }

  pub fn trivial(self) -> SymbolicResult<Number> {
    match self {
      Number::Q(q) => {
        if q.den() == 0 {
          return Err(Form {});
        }

        let g = Integer::gcd(
          &q.num(), //.
          &q.den(),
        );

        let num = q.num() / g * q.den().signum();
        let den = q.den() / g * q.den().signum();

        if den != 1 {
          Ok(Number::Q(Rational::new(num, den)))
        } else {
          Ok(Number::Z(num))
        }
      }

      _ => {
        Ok(self) //.
      }
    }
  }
}

impl Add for Number {
  type Output = SymbolicResult<Number>;

  fn add(self, o: Self) -> Self::Output {
    match (self, o) {
      (Number::Z(lhs), Number::Z(rhs)) => Number::Z(lhs + rhs),
      (Number::Q(lhs), Number::Q(rhs)) => Number::Q(lhs + rhs),

      (
        Number::Z(z),
        Number::Q(q),
      )
      | //.
      (
        Number::Q(q),
        Number::Z(z),
      ) => {
        Number::Q(
          // ```a/b + c/1 = (a + c)/b```
          q + Rational::from(z),
        )
      }
    }
    .trivial()
  }
}

impl Mul for Number {
  type Output = SymbolicResult<Number>;

  fn mul(self, o: Self) -> Self::Output {
    match (self, o) {
      (Number::Z(lhs), Number::Z(rhs)) => Number::Z(lhs * rhs),
      (Number::Q(lhs), Number::Q(rhs)) => Number::Q(lhs * rhs),

      (
        Number::Z(z),
        Number::Q(q),
      )
      | //.
      (
        Number::Q(q),
        Number::Z(z),
      ) => {
        Number::Q(
          // ```a/b * c/1 = a*b/c```
          q * Rational::from(z),
        )
      }
    }
    .trivial()
  }
}

impl fmt::Display for Number {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Number::Z(z) => write!(f, "{}", z),

      Number::Q(q) => {
        write!(
          //.
          f,
          "{}/{}",
          q.num(),
          q.den()
        )
      }
    }
  }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Copy)]
/// Mathematical error
pub struct Form {}

impl fmt::Display for Form {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "?") }
}

#[allow(nonstandard_style)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Copy)]
/// Special constants
pub enum Constant {
  /// ∞ Infinity
  oo,

  /// \[π] pi, Archimede's constant
  pi,
  /// \[φ] Golden ratio
  GoldenRatio,
  /// \[G] Catalan's constant
  Catalan,
  /// \[γ] Euler-Mascheroni constant
  Euler,
  /// \[K] Khinchin's constant
  Khinchin,
  /// \[A] Glaisher's constant
  Glaisher,
  /// \[ζ3] Apéry's constant
  Apery,

  /// \[i] Imaginary number
  I,
}

impl fmt::Display for Constant {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }
}