Skip to main content

light_curve_feature/
float_trait.rs

1use crate::periodogram::FftFloat;
2use num_complex::Complex;
3
4#[cfg(feature = "fftw")]
5use crate::periodogram::FftwFloat;
6
7use conv::prelude::*;
8use lazy_static::lazy_static;
9use ndarray::{Array0, ScalarOperand};
10use num_traits::{FromPrimitive, float::Float as NumFloat, float::FloatConst};
11use schemars::JsonSchema;
12use serde::Serialize;
13use serde::de::DeserializeOwned;
14use std::cmp::PartialOrd;
15use std::fmt::{Debug, Display, LowerExp};
16use std::iter::Sum;
17use std::ops::{AddAssign, DivAssign, MulAssign};
18
19lazy_static! {
20    static ref ARRAY0_UNITY_F32: Array0<f32> = Array0::from_elem((), 1.0);
21}
22
23lazy_static! {
24    static ref ARRAY0_UNITY_F64: Array0<f64> = Array0::from_elem((), 1.0);
25}
26
27// Note: Two trait definitions are needed because Rust doesn't support conditional trait bounds.
28// The only difference is the `+ FftwFloat` bound when the fftw feature is enabled.
29
30/// Floating number trait, it is implemented for [f32] and [f64] only
31#[cfg(feature = "fftw")]
32pub trait Float:
33    'static
34    + Sized
35    + NumFloat
36    + FloatConst
37    + FromPrimitive
38    + PartialOrd
39    + Sum
40    + ValueFrom<u32>
41    + ValueFrom<usize>
42    + ValueFrom<f32>
43    + ValueInto<f64>
44    + ApproxFrom<usize>
45    + ApproxFrom<f64>
46    + ApproxInto<u32, RoundToNearest>
47    + ApproxInto<usize, RoundToNearest>
48    + ApproxInto<f32>
49    + ApproxInto<f64>
50    + Clone
51    + Copy
52    + Send
53    + Sync
54    + AddAssign
55    + MulAssign
56    + DivAssign
57    + ScalarOperand
58    + Display
59    + Debug
60    + LowerExp
61    + FftFloat<Complex = Complex<Self>>
62    + FftwFloat
63    + DeserializeOwned
64    + Serialize
65    + JsonSchema
66{
67    fn half() -> Self;
68    fn two() -> Self;
69    fn three() -> Self;
70    fn four() -> Self;
71    fn five() -> Self;
72    fn ten() -> Self;
73    fn hundred() -> Self;
74    fn array0_unity() -> &'static Array0<Self>;
75}
76
77/// Floating number trait, it is implemented for [f32] and [f64] only
78#[cfg(not(feature = "fftw"))]
79pub trait Float:
80    'static
81    + Sized
82    + NumFloat
83    + FloatConst
84    + FromPrimitive
85    + PartialOrd
86    + Sum
87    + ValueFrom<u32>
88    + ValueFrom<usize>
89    + ValueFrom<f32>
90    + ValueInto<f64>
91    + ApproxFrom<usize>
92    + ApproxFrom<f64>
93    + ApproxInto<u32, RoundToNearest>
94    + ApproxInto<usize, RoundToNearest>
95    + ApproxInto<f32>
96    + ApproxInto<f64>
97    + Clone
98    + Copy
99    + Send
100    + Sync
101    + AddAssign
102    + MulAssign
103    + DivAssign
104    + ScalarOperand
105    + Display
106    + Debug
107    + LowerExp
108    + FftFloat<Complex = Complex<Self>>
109    + DeserializeOwned
110    + Serialize
111    + JsonSchema
112{
113    fn half() -> Self;
114    fn two() -> Self;
115    fn three() -> Self;
116    fn four() -> Self;
117    fn five() -> Self;
118    fn ten() -> Self;
119    fn hundred() -> Self;
120    fn array0_unity() -> &'static Array0<Self>;
121}
122
123impl Float for f32 {
124    #[inline]
125    fn half() -> Self {
126        0.5
127    }
128
129    #[inline]
130    fn two() -> Self {
131        2.0
132    }
133
134    #[inline]
135    fn three() -> Self {
136        3.0
137    }
138
139    #[inline]
140    fn four() -> Self {
141        4.0
142    }
143
144    #[inline]
145    fn five() -> Self {
146        5.0
147    }
148
149    #[inline]
150    fn ten() -> Self {
151        10.0
152    }
153
154    #[inline]
155    fn hundred() -> Self {
156        100.0
157    }
158
159    fn array0_unity() -> &'static Array0<Self> {
160        &ARRAY0_UNITY_F32
161    }
162}
163
164impl Float for f64 {
165    #[inline]
166    fn half() -> Self {
167        0.5
168    }
169
170    #[inline]
171    fn two() -> Self {
172        2.0
173    }
174
175    #[inline]
176    fn three() -> Self {
177        3.0
178    }
179
180    #[inline]
181    fn four() -> Self {
182        4.0
183    }
184
185    #[inline]
186    fn five() -> Self {
187        5.0
188    }
189
190    #[inline]
191    fn ten() -> Self {
192        10.0
193    }
194
195    #[inline]
196    fn hundred() -> Self {
197        100.0
198    }
199
200    fn array0_unity() -> &'static Array0<Self> {
201        &ARRAY0_UNITY_F64
202    }
203}