whiteboard/
lib.rs

1pub mod trig {
2  pub fn get_unit_vec(a: Vec<f64>) -> Vec<f64> {
3    let mut mag: f64 = 0.0;
4    for i in a.iter() {
5      mag += i*i;
6    }
7    a.iter().map(|x| x / mag.sqrt()).collect::<Vec<f64>>()
8  }
9
10  pub fn get_theta(a: Vec<f64>, b: Vec<f64>) -> f64 {
11    dot(a, b).acos()
12  }
13
14  pub fn dot(a: Vec<f64>, b: Vec<f64>) -> f64 {
15    let mut res: f64 = 0.0;
16    for (i, j) in a.iter().zip(b.iter()) {
17      res += i * j;
18    }
19    res
20  }
21}
22
23pub mod lin_alg {
24  pub fn gauss_jordan(mut aug: Vec<Vec<f64>>) -> Vec<Vec<f64>> {
25    let length: usize = aug.len();
26        
27    for i in 0..(length - 1) {
28      for j in (i+1)..length {
29        let co_i = aug[i][i];
30        let co_j = aug[j][i];
31        for k in i..(length + 1) {
32          aug[j][k] -= (co_j / co_i) * aug[i][k];
33        }
34      }
35    }
36    for i in (0..length).rev() {
37      let co_i: f64 = aug[i][i];
38      aug[i][i] /= co_i;
39      aug[i][length] /= co_i;
40      for j in 0..i {
41        let co_j: f64 = aug[j][i];
42        aug[j][length] -= co_j * aug[i][length];
43      }
44    }
45       
46    aug
47  }
48}
49
50pub mod misc {
51  pub fn rotate(point: (f64, f64), center: (f64, f64), theta: f64) -> (f64, f64) {
52    (
53      (point.0 - center.0) * theta.cos() - (point.1 - center.1) * theta.sin() + center.0,
54      (point.0 - center.0) * theta.sin() + (point.1 - center.1) * theta.cos() + center.1
55    )
56  }
57}