forked from EbTech/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
num.rs
466 lines (447 loc) · 12.8 KB
/
num.rs
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Rational and Complex numbers, safe modular arithmetic, and linear algebra,
//! implemented minimally for contest use.
//! If you need more features, you might be interested in crates.io/crates/num
pub use std::f64::consts::PI;
use std::ops::{Add, Div, Index, IndexMut, Mul, Neg, Sub};
/// Fast iterative version of Euclid's GCD algorithm
pub fn fast_gcd(mut a: i64, mut b: i64) -> i64 {
while b != 0 {
a %= b;
std::mem::swap(&mut a, &mut b);
}
a.abs()
}
/// Represents a fraction reduced to lowest terms
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
pub struct Rational {
pub num: i64,
pub den: i64,
}
impl Rational {
pub fn new(num: i64, den: i64) -> Self {
let g = fast_gcd(num, den) * den.signum();
Self {
num: num / g,
den: den / g,
}
}
pub fn abs(self) -> Self {
Self {
num: self.num.abs(),
den: self.den,
}
}
pub fn recip(self) -> Self {
let g = self.num.signum();
Self {
num: self.den / g,
den: self.num / g,
}
}
}
impl From<i64> for Rational {
fn from(num: i64) -> Self {
Self { num, den: 1 }
}
}
impl Neg for Rational {
type Output = Self;
fn neg(self) -> Self {
Self {
num: -self.num,
den: self.den,
}
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Add for Rational {
type Output = Self;
fn add(self, other: Self) -> Self {
Self::new(
self.num * other.den + self.den * other.num,
self.den * other.den,
)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Sub for Rational {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::new(
self.num * other.den - self.den * other.num,
self.den * other.den,
)
}
}
impl Mul for Rational {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self::new(self.num * other.num, self.den * other.den)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Div for Rational {
type Output = Self;
fn div(self, other: Self) -> Self {
self * other.recip()
}
}
impl Ord for Rational {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.num * other.den).cmp(&(self.den * other.num))
}
}
impl PartialOrd for Rational {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
/// Represents a complex number using floating-point arithmetic
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Complex {
pub real: f64,
pub imag: f64,
}
impl Complex {
pub fn new(real: f64, imag: f64) -> Self {
Self { real, imag }
}
pub fn from_polar(r: f64, th: f64) -> Self {
Self::new(r * th.cos(), r * th.sin())
}
pub fn abs_square(self) -> f64 {
self.real * self.real + self.imag * self.imag
}
pub fn argument(self) -> f64 {
self.imag.atan2(self.real)
}
pub fn conjugate(self) -> Self {
Self::new(self.real, -self.imag)
}
pub fn recip(self) -> Self {
let denom = self.abs_square();
Self::new(self.real / denom, -self.imag / denom)
}
}
impl From<f64> for Complex {
fn from(real: f64) -> Self {
Self::new(real, 0.0)
}
}
impl Neg for Complex {
type Output = Self;
fn neg(self) -> Self {
Self::new(-self.real, -self.imag)
}
}
impl Add for Complex {
type Output = Self;
fn add(self, other: Self) -> Self {
Self::new(self.real + other.real, self.imag + other.imag)
}
}
impl Sub for Complex {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::new(self.real - other.real, self.imag - other.imag)
}
}
impl Mul for Complex {
type Output = Self;
fn mul(self, other: Self) -> Self {
let real = self.real * other.real - self.imag * other.imag;
let imag = self.imag * other.real + self.real * other.imag;
Self::new(real, imag)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Div for Complex {
type Output = Self;
fn div(self, other: Self) -> Self {
self * other.recip()
}
}
/// Represents an element of the finite (Galois) field of prime order M, where
/// 1 <= M < 2^31.5. If M is not prime, ring operations are still valid
/// but recip() and division are not. Note that the latter operations are also
/// the slowest, so precompute any inverses that you intend to use frequently.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
pub struct Modulo<const M: i64> {
pub val: i64,
}
impl<const M: i64> Modulo<M> {
/// Computes self^n in O(log n) time
pub fn pow(mut self, mut n: u64) -> Self {
let mut result = Self::from_small(1);
while n > 0 {
if n % 2 == 1 {
result = result * self;
}
self = self * self;
n /= 2;
}
result
}
/// Computes inverses of 1 to n in O(n) time
pub fn vec_of_recips(n: i64) -> Vec<Self> {
let mut recips = vec![Self::from(0), Self::from(1)];
for i in 2..=n {
let (md, dv) = (M % i, M / i);
recips.push(recips[md as usize] * Self::from_small(-dv));
}
recips
}
/// Computes self^-1 in O(log M) time
pub fn recip(self) -> Self {
self.pow(M as u64 - 2)
}
/// Avoids the % operation but requires -M <= x < M
fn from_small(s: i64) -> Self {
let val = if s < 0 { s + M } else { s };
Self { val }
}
}
impl<const M: i64> From<i64> for Modulo<M> {
fn from(val: i64) -> Self {
// Self { val: val.rem_euclid(M) }
Self::from_small(val % M)
}
}
impl<const M: i64> Neg for Modulo<M> {
type Output = Self;
fn neg(self) -> Self {
Self::from_small(-self.val)
}
}
impl<const M: i64> Add for Modulo<M> {
type Output = Self;
fn add(self, other: Self) -> Self {
Self::from_small(self.val + other.val - M)
}
}
impl<const M: i64> Sub for Modulo<M> {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self::from_small(self.val - other.val)
}
}
impl<const M: i64> Mul for Modulo<M> {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self::from(self.val * other.val)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl<const M: i64> Div for Modulo<M> {
type Output = Self;
fn div(self, other: Self) -> Self {
self * other.recip()
}
}
/// Prime modulus that's commonly used in programming competitions
pub const COMMON_PRIME: i64 = 998_244_353; // 2^23 * 7 * 17 + 1;
pub type CommonField = Modulo<COMMON_PRIME>;
#[derive(Clone, PartialEq, Debug)]
pub struct Matrix {
cols: usize,
inner: Box<[f64]>,
}
impl Matrix {
pub fn zero(rows: usize, cols: usize) -> Self {
let inner = vec![0.0; rows * cols].into_boxed_slice();
Self { cols, inner }
}
pub fn one(cols: usize) -> Self {
let mut matrix = Self::zero(cols, cols);
for i in 0..cols {
matrix[i][i] = 1.0;
}
matrix
}
pub fn vector(vec: &[f64], as_row: bool) -> Self {
let cols = if as_row { vec.len() } else { 1 };
let inner = vec.to_vec().into_boxed_slice();
Self { cols, inner }
}
pub fn pow(&self, mut n: u64) -> Self {
let mut base = self.clone();
let mut result = Self::one(self.cols);
while n > 0 {
if n % 2 == 1 {
result = &result * &base;
}
base = &base * &base;
n /= 2;
}
result
}
pub fn rows(&self) -> usize {
self.inner.len() / self.cols
}
pub fn transpose(&self) -> Self {
let mut matrix = Matrix::zero(self.cols, self.rows());
for i in 0..self.rows() {
for j in 0..self.cols {
matrix[j][i] = self[i][j];
}
}
matrix
}
pub fn recip(&self) -> Self {
unimplemented!();
}
}
impl Index<usize> for Matrix {
type Output = [f64];
fn index(&self, row: usize) -> &Self::Output {
let start = self.cols * row;
&self.inner[start..start + self.cols]
}
}
impl IndexMut<usize> for Matrix {
fn index_mut(&mut self, row: usize) -> &mut Self::Output {
let start = self.cols * row;
&mut self.inner[start..start + self.cols]
}
}
impl Neg for &Matrix {
type Output = Matrix;
fn neg(self) -> Matrix {
let inner = self.inner.iter().map(|&v| -v).collect();
Matrix {
cols: self.cols,
inner,
}
}
}
impl Add for &Matrix {
type Output = Matrix;
fn add(self, other: Self) -> Matrix {
let self_iter = self.inner.iter();
let inner = self_iter
.zip(other.inner.iter())
.map(|(&u, &v)| u + v)
.collect();
Matrix {
cols: self.cols,
inner,
}
}
}
impl Sub for &Matrix {
type Output = Matrix;
fn sub(self, other: Self) -> Matrix {
let self_iter = self.inner.iter();
let inner = self_iter
.zip(other.inner.iter())
.map(|(&u, &v)| u - v)
.collect();
Matrix {
cols: self.cols,
inner,
}
}
}
impl Mul<f64> for &Matrix {
type Output = Matrix;
fn mul(self, scalar: f64) -> Matrix {
let inner = self.inner.iter().map(|&v| v * scalar).collect();
Matrix {
cols: self.cols,
inner,
}
}
}
impl Mul for &Matrix {
type Output = Matrix;
fn mul(self, other: Self) -> Matrix {
assert_eq!(self.cols, other.rows());
let mut matrix = Matrix::zero(self.rows(), other.cols);
for i in 0..self.rows() {
for k in 0..self.cols {
for j in 0..other.cols {
matrix[i][j] += self[i][k] * other[k][j];
}
}
}
matrix
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_rational() {
let three = Rational::from(3);
let six = Rational::from(6);
let three_and_half = three + three / six;
assert_eq!(three_and_half.num, 7);
assert_eq!(three_and_half.den, 2);
assert_eq!(three_and_half, Rational::new(-35, -10));
assert!(three_and_half > Rational::from(3));
assert!(three_and_half < Rational::from(4));
let minus_three_and_half = six - three_and_half + three / (-three / six);
let zero = three_and_half + minus_three_and_half;
assert_eq!(minus_three_and_half.num, -7);
assert_eq!(minus_three_and_half.den, 2);
assert_eq!(three_and_half, -minus_three_and_half);
assert_eq!(zero.num, 0);
assert_eq!(zero.den, 1);
}
#[test]
fn test_complex() {
let four = Complex::new(4.0, 0.0);
let two_i = Complex::new(0.0, 2.0);
assert_eq!(four / two_i, -two_i);
assert_eq!(two_i * -two_i, four);
assert_eq!(two_i - two_i, Complex::from(0.0));
assert_eq!(four.abs_square(), 16.0);
assert_eq!(two_i.abs_square(), 4.0);
assert_eq!((-four).argument(), -PI);
assert_eq!((-two_i).argument(), -PI / 2.0);
assert_eq!(four.argument(), 0.0);
assert_eq!(two_i.argument(), PI / 2.0);
}
#[test]
fn test_field() {
let base = CommonField::from(1234);
let zero = base - base;
let one = base.recip() * base;
let two = CommonField::from(2 - 5 * COMMON_PRIME);
assert_eq!(zero.val, 0);
assert_eq!(one.val, 1);
assert_eq!(one + one, two);
assert_eq!(one / base * (base * base) - base / one, zero);
}
#[test]
fn test_vec_of_recips() {
let recips = CommonField::vec_of_recips(20);
assert_eq!(recips.len(), 21);
for i in 1..recips.len() {
assert_eq!(recips[i], CommonField::from(i as i64).recip());
}
}
#[test]
fn test_linalg() {
let zero = Matrix::zero(2, 2);
let one = Matrix::one(2);
let rotate_90 = Matrix {
cols: 2,
inner: Box::new([0.0, -1.0, 1.0, 0.0]),
};
let x_vec = Matrix::vector(&[1.0, 0.0], false);
let y_vec = Matrix::vector(&[0.0, 1.0], false);
let x_dot_x = &x_vec.transpose() * &x_vec;
let x_dot_y = &x_vec.transpose() * &y_vec;
assert_eq!(x_dot_x, Matrix::one(1));
assert_eq!(x_dot_x[0][0], 1.0);
assert_eq!(x_dot_y, Matrix::zero(1, 1));
assert_eq!(x_dot_y[0][0], 0.0);
assert_eq!(&one - &one, zero);
assert_eq!(&one * 0.0, zero);
assert_eq!(&rotate_90 * &rotate_90, -&one);
assert_eq!(&rotate_90 * &x_vec, y_vec);
assert_eq!(&rotate_90 * &y_vec, -&x_vec);
assert_eq!(&rotate_90 * &(&x_vec + &y_vec), &y_vec - &x_vec);
}
}