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
pub mod consts;
pub mod linalg;
pub mod error;

use std::convert::From;
use std::default::Default;
use std::ops::{Add, AddAssign, Neg};
use std::result;

use crate::base::error::Error;

type Result<T> = result::Result<T, Error>;

///
/// Frac: Gives the fractional part of a number
///
pub trait Fractional {
    fn fractional(self) -> Self;
}

macro_rules! impl_fractional {
    ($t:ty) => (
        impl Fractional for $t {
            fn fractional(self) -> Self {
                self - self.floor()
            }
        }
    );
}

impl_fractional!(f32);
impl_fractional!(f64);

///
/// FMod: calculates x mod y
///
pub trait Modulo {
    fn modulo(self, rhs: Self) -> Self;
}

macro_rules! impl_modulo {
    ($t:ty) => (
        impl Modulo for $t {
            fn modulo(self, rhs: Self) -> Self {
                self - rhs * (self / rhs).floor()
            }
        }
    );
}

impl_modulo!(f32);
impl_modulo!(f64);

///
/// Pair: Calculates cos(alpha+beta) and sin(alpha+beta) using addition
/// theorems
///
#[derive(Debug, Copy, Clone)]
pub struct PertPair {
    c: f64,
    s: f64
}

impl From<f64> for PertPair {
    fn from(angle: f64) -> Self {
        let v = angle.sin_cos();
        Self { c: v.1, s: v.0 }
    }
}

impl Default for PertPair {
    fn default() -> Self {
        Self { c: 1.0, s: 0.0 }
    }
}

impl Neg for PertPair {
    type Output = Self;

    fn neg(self) -> Self {
        PertPair { c: self.c, s: -self.s }
    }
}

impl Add for PertPair {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self {
            c: self.c * rhs.c - self.s * rhs.s,
            s: self.s * rhs.c + self.c * rhs.s
        }
    }
}

impl AddAssign for PertPair {
    fn add_assign(&mut self, rhs: Self) {
        *self = self.add(rhs);
    }
}

impl PertPair {
    pub fn c(&self) -> f64 {
        self.c
    }

    pub fn s(&self) -> f64 {
        self.s
    }
}