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
extern crate num;
#[allow(unused_imports)] use num::Float;
use utils::vec_traits::{ScalarMult,
                        ScalarDiv,
                        AddVec,
                        SubVec};

/// ## First Equation of Motion:
///
/// Returns the final velocity (v) assuming a constant
/// acceleration (a) during a time (t).
///
/// # Args
/// * `v_i`: `Vec<f64>`   - Initial velocity (m * s)
/// * `a`: `mut Vec<f64>`     - constant acceleration (m * s^(2))
/// * `t`: `f64`     - time (s)
///
/// # Notes:
///   - This derivation can be [understood here](https://en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration)
///   - This only holds under *constant acceleration*
pub fn first_velocity<T>(mut acc: Vec<T>, v_i: Vec<T>, time: T) -> Vec<T>
    where T: Float
{
    assert!(time >= T::zero(), "time cannot be negative");

    acc.scalar_mult(time);
    v_i.add_vec(acc)
}

/// ## First Equation of Motion:
///
/// Returns the acceleration (a) given initial and final velocities (v_i and v_f, respectively),
/// over a time (t)
///
/// # Args
/// * `v_i`: `f64`   - Initial velocity (m * s)
/// * `v_f`: `f64`   - Final velocity (m * s)
/// * `t`: `f64`     - time (s)
///
/// # Notes:
///   - This derivation can be [understood here](https://en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration)
pub fn first_accel<T>(v_f: Vec<T>, v_i: Vec<T>, time: T) -> Vec<T>
    where T: Float
{
    assert!(time >= T::zero(), "time cannot be negative");

    let mut a = v_f.sub_vec(v_i);
    a.scalar_div(time);
    a
}

/// ## Second Equation of Motion:
///
/// Returns the final position (x) given the initial velocity (v_i),
/// the total time (t), and the acceleration (a).
///
/// # Args
/// * `v_i`: `f64`   - Initial velocity (m * s)
/// * `x_i`: `f64`   - Initial position (m)
/// * `t`: `f64`     - time (s)
/// * `a`: `f64`     - acceleration (m * s^(2))
///
/// # Notes:
///   - This derivation can be [understood here](https://en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration)
pub fn second_position<T>(x_i: Vec<T>, mut v_i: Vec<T>, time: T, mut acc: Vec<T>) -> Vec<T>
    where T: Float
{
    assert!(time >= T::zero(), "Time cannot be negative");

    v_i.scalar_mult(time);
    let t_sqrd = time * time;
    acc.scalar_mult(t_sqrd);
    acc.scalar_div(num::cast(2.0).unwrap());

    x_i.add_vec(v_i).add_vec(acc)
}

#[cfg(test)]
mod tests {
    extern crate num;

    use super::*;
    #[allow(unused_imports)] use num::Float;

    #[test]
    fn first_velocity_1() {
        let a = vec![1.0, 2.0, 3.0];
        let v_i = vec![1.0, 1.0, 1.0];
        let t = 10.0;
        let v = first_velocity(a, v_i, t);

        let expected = vec![11.0, 21.0, 31.0];
        assert_eq!(v, expected);
    }

    #[test]
    fn first_velocity_2() {
        let v_i = vec![200.0, 200.0, 200.0];
        let a = vec![0.0; 3];
        let t = 1200.0;
        let v = first_velocity(a, v_i, t);

        let expected = vec![200.0, 200.0, 200.0];
        assert_eq!(v, expected);
    }

    #[test]
    fn first_accel_1() {
        let v_i = vec![1.0, 1.0, 1.0];
        let v_f = vec![1.0, 1.0, 1.0];
        let t = 10.0;
        let a = first_accel(v_f, v_i, t);

        let expected = vec![0.0, 0.0, 0.0];
        assert_eq!(a, expected);
    }

    #[test]
    fn first_accel_2() {
        let v_i = vec![1.0, 1.0, 1.0];
        let v_f = vec![1.0, 1.0, 1.0];
        let t = 0.0;
        let a = first_accel(v_f, v_i, t);

        let expected = vec![Float::infinity(), Float::infinity(), Float::infinity()];
        assert_eq!(a, expected);
    }

    #[test]
    fn test_second_position1() {
        let v_i = vec![1.0, 1.0, 1.0];
        let x_i = vec![0.0, 0.0, 0.0];
        let t = 10.0;
        let a = vec![2.0, 2.0, 2.0];
        let x = second_position(x_i, v_i, t, a);

        let expected = vec![110.0, 110.0, 110.0];
        assert_eq!(x, expected);
    }

    #[test]
    #[should_panic]
    fn test_second_position2() {
        let v_i = vec![0.0, 0.0, 0.0];
        let x_i = vec![0.0, 0.0, 0.0];
        let t = -30.0;
        let a = vec![0.0, 0.0, -9.8];
        let x = second_position(x_i, v_i, t, a);

        let expected = vec![0.0, 0.0, -4410.0];
        assert_eq!(x, expected);
    }
}