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

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

use num_traits::float::Float;

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


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


pub trait Real<T = Self> where T: Float
{
    ///
    /// frac: Gives the fractional part of a number
    ///
    fn frac(self) -> Self;

    ///
    /// fmod: calculates x mod y
    ///
    fn fmod(self, rhs: Self) -> Self;
}

macro_rules! impl_real {
    ($t:ty) => (
        impl Real for $t {
            fn frac(self) -> Self {
                self - self.floor()
            }

            fn fmod(self, rhs: Self) -> Self {
                self - rhs * (self / rhs).floor()
            }
        }
    );
}

impl_real!(f64);
impl_real!(f32);


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

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

impl Default for PertPair {
    fn default() -> Self {
        Self::from_zero()
    }
}

impl Neg for PertPair {
    type Output = Self;

    fn neg(self) -> Self {
        Self(-self.0, self.1)
    }
}

impl Add for PertPair {
    type Output = Self;

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

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

impl Mul<i32> for PertPair {
    type Output = Self;

    fn mul(self, rhs: i32) -> Self {
        if rhs == 0 {
            Self::from_zero()
        } else if rhs == 1 {
            self
        } else if rhs == 2 {
            Self(
                2.0 * self.0 * self.1,
                self.1 * self.1 - self.0 * self.0
            )
        } else if rhs < 0 {
            let pair = self.mul(-rhs);
            pair.neg()
        } else if rhs & 1 == 0 {
            let pair = self.mul(rhs >> 1);
            pair.mul(2)
        } else {
            let pair = self.mul(rhs - 1);
            pair.add(self)
        }
    }
}

impl Mul<PertPair> for i32 {
    type Output = PertPair;

    fn mul(self, rhs: PertPair) -> PertPair {
        rhs.mul(self)
    }
}

impl MulAssign<i32> for PertPair {
    fn mul_assign(&mut self, rhs: i32) {
        *self = self.mul(rhs);
    }
}

impl PertPair {
    pub fn from_zero() -> PertPair {
        PertPair(0.0, 1.0)
    }

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

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

    pub fn sin_cos(&self) -> (f64, f64) {
        let PertPair(s, c) = *self;
        (s, c)
    }
}