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
use num_traits::Zero;
use std::fmt;
use std::ops;
mod tree;
pub use self::tree::{Symbol, SymbolValues, TDim, UndeterminedSymbol};
type TractError = anyhow::Error;
type TractResult<T> = anyhow::Result<T>;
pub trait DimLike:
Clone
+ Default
+ PartialEq
+ From<usize>
+ for<'a> std::convert::TryFrom<&'a TDim, Error = TractError>
+ ::num_traits::Zero
+ fmt::Debug
+ fmt::Display
+ std::hash::Hash
+ ops::Add<Self, Output = Self>
+ ops::Add<usize, Output = Self>
+ for<'a> ops::Add<&'a Self, Output = Self>
+ ops::Sub<Self, Output = Self>
+ ops::Sub<usize, Output = Self>
+ for<'a> ops::Sub<&'a Self, Output = Self>
+ ops::Mul<Self, Output = Self>
+ ops::Mul<usize, Output = Self>
+ for<'a> ops::Mul<&'a Self, Output = Self>
+ ops::Div<usize, Output = Self>
+ ops::Rem<usize, Output = Self>
+ Send
+ Sync
+ 'static
+ std::iter::Sum
+ std::iter::Product
+ ToDim
{
fn maybe_div(&self, other: &Self) -> TractResult<(Self, u64)>;
fn div_ceil(&self, other: usize) -> Self {
(self.clone() + other - 1) / other
}
fn to_i64(&self) -> TractResult<i64>;
fn to_usize(&self) -> TractResult<usize> {
self.to_i64().map(|d| d as usize)
}
fn to_isize(&self) -> TractResult<isize> {
self.to_i64().map(|d| d as isize)
}
fn to_i32(&self) -> TractResult<i32> {
self.to_i64().map(|d| d as i32)
}
fn one() -> Self;
fn eval(&self, values: &SymbolValues) -> Self;
}
impl DimLike for TDim {
fn maybe_div(&self, other: &Self) -> TractResult<(Self, u64)> {
if self.is_zero() {
return Ok((TDim::zero(), 1));
} else if other.is_zero() {
anyhow::bail!("Division by zero")
}
fn expand(dim: &TDim) -> (i64, Vec<TDim>) {
match dim {
TDim::Mul(terms) => terms.iter().map(expand).fold((1i64, vec![]), |acc, t| {
(acc.0 * t.0, acc.1.into_iter().chain(t.1.into_iter()).collect())
}),
TDim::MulInt(a, terms) => {
let (b, v) = expand(terms);
(a * b, v)
}
TDim::Val(x) => (*x, vec![]),
it => (1, vec![it.clone()]),
}
}
let (mut num_int, mut num) = expand(self);
let (mut denum_int, denum) = expand(other);
for it in denum {
if let Some(pos) = num.iter().position(|n| n == &it) {
num.remove(pos);
} else {
anyhow::bail!("Can't divide {} by {}", self, other)
}
}
use num_integer::Integer;
if denum_int < 0 {
num_int *= -1;
denum_int *= -1;
}
let gcd = num_int.gcd(&denum_int);
num_int /= gcd;
denum_int /= gcd;
Ok(((TDim::Mul(num) * num_int).reduce(), denum_int as u64))
}
fn to_i64(&self) -> TractResult<i64> {
TDim::to_i64(self)
}
fn one() -> Self {
Self::from(1)
}
fn eval(&self, values: &SymbolValues) -> Self {
self.eval(values)
}
}
impl<'a> std::convert::TryFrom<&'a TDim> for TDim {
type Error = anyhow::Error;
fn try_from(d: &'a TDim) -> TractResult<TDim> {
Ok(d.clone())
}
}
impl DimLike for usize {
fn maybe_div(&self, other: &Self) -> TractResult<(Self, u64)> {
use num_integer::Integer;
let gcd = self.gcd(other);
Ok((self / gcd, (other / gcd) as u64))
}
fn to_i64(&self) -> TractResult<i64> {
Ok(*self as i64)
}
fn one() -> usize {
1
}
fn eval(&self, _values: &SymbolValues) -> Self {
*self
}
}
impl<'a> std::convert::TryFrom<&'a TDim> for usize {
type Error = anyhow::Error;
fn try_from(d: &'a TDim) -> anyhow::Result<usize> {
d.to_usize()
}
}
pub trait ToDim {
fn to_dim(&self) -> TDim;
}
impl<I: Into<TDim> + Clone> ToDim for I {
fn to_dim(&self) -> TDim {
self.clone().into()
}
}
impl<'a> ToDim for &'a TDim {
fn to_dim(&self) -> TDim {
self.clone().clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
lazy_static::lazy_static! {
static ref S: Symbol = crate::dim::Symbol::new('S');
}
pub fn s() -> TDim {
(*S).into()
}
#[test]
fn div() {
assert_eq!(TDim::from(12).maybe_div(&TDim::from(4)).unwrap(), (3.into(), 1));
}
#[test]
fn div_sym_int() {
assert_eq!((s() * 12).maybe_div(&TDim::from(4)).unwrap(), (s() * 3, 1));
}
#[test]
fn div_sym_sym() {
assert_eq!((s() * 12).maybe_div(&(s() * 4)).unwrap(), (3.into(), 1));
}
#[test]
fn div_sym_sym_ratio() {
assert_eq!((s() * 13).maybe_div(&(s() * 4)).unwrap(), (13.into(), 4));
}
#[test]
fn div_sym_sym_rem() {
assert!((s() + 1).maybe_div(&(s() * 4)).is_err());
}
#[test]
fn div_sym_sym_complex() {
assert_eq!(
(256.to_dim() * 's' * 'b').maybe_div(&(1.to_dim() * 's' * 'b')).unwrap(),
(256.into(), 1)
);
}
}