Tue, 20 Feb 2024 12:33:16 -0500
Logarithmic logging base correction
5 | 1 | /*! |
2 | Some useful (numerical) types and traits. | |
3 | ||
4 | The traits are based on corresponding ones in [`num_traits`], but try to fill some gaps in the | |
5 | super-traits and available constants. | |
0 | 6 | |
5 | 7 | As [`nalgebra`] unnecessarily provides many of the same methods as [`num_traits`], to avoid having |
8 | to refer to the methods with the full path, it is often necesary to use [`ToNalgebraRealField`][crate::nalgebra_support::ToNalgebraRealField] to hide the nalgebra implementations until | |
9 | absolutely necessary to use nalgebra. | |
10 | */ | |
11 | ||
12 | //use trait_set::trait_set; | |
0 | 13 | pub use num_traits::Float as NumTraitsFloat; // needed to re-export functions. |
14 | pub use num_traits::cast::AsPrimitive; | |
15 | ||
16 | /// Typical integer type | |
17 | #[allow(non_camel_case_types)] | |
18 | pub type int = i64; | |
19 | ||
20 | /// Typical unsigned integer type | |
21 | #[allow(non_camel_case_types)] | |
22 | pub type uint = u64; | |
23 | ||
24 | /// Typical floating point number type | |
25 | #[allow(non_camel_case_types)] | |
26 | pub type float = f64; | |
27 | ||
28 | /// Casts of abstract numerical types to others via the standard `as` keyword. | |
29 | pub trait CastFrom<T : 'static + Copy> : num_traits::cast::AsPrimitive<T> { | |
30 | fn cast_from(other : T) -> Self; | |
31 | } | |
32 | ||
33 | macro_rules! impl_casts { | |
34 | ($($type:ty)*) => { $( | |
35 | impl_casts!(@phase2, $type, | |
36 | u8 u16 u32 u64 u128 usize | |
37 | i8 i16 i32 i64 i128 isize | |
38 | f32 f64); | |
39 | )* }; | |
40 | (@phase2, $type:ty, $($type2:ty)*) => { $( | |
41 | impl CastFrom<$type2> for $type { | |
42 | #[inline] | |
43 | fn cast_from(other : $type2) -> Self { other as $type } | |
44 | } | |
45 | )* }; | |
46 | } | |
47 | ||
48 | impl_casts!(u8 u16 u32 u64 u128 usize | |
49 | i8 i16 i32 i64 i128 isize | |
50 | f32 f64); | |
51 | ||
5 | 52 | /// Trait for general numeric types |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
53 | pub trait Num : 'static + Copy + Sync + Send + num::Num + num_traits::NumAssign |
0 | 54 | + std::iter::Sum + std::iter::Product |
55 | + std::fmt::Debug + std::fmt::Display + serde::Serialize | |
56 | + CastFrom<u8> + CastFrom<u16> + CastFrom<u32> + CastFrom<u64> | |
57 | + CastFrom<u128> + CastFrom<usize> | |
58 | + CastFrom<i8> + CastFrom<i16> + CastFrom<i32> + CastFrom<i64> | |
59 | + CastFrom<i128> + CastFrom<isize> | |
60 | + CastFrom<f32> + CastFrom<f64> { | |
61 | ||
62 | const ZERO : Self; | |
63 | const ONE : Self; | |
64 | const TWO : Self; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
65 | /// Generic version of `Self::MAX` |
0 | 66 | const RANGE_MAX : Self; |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
67 | /// Generic version of `Self::MIN` |
0 | 68 | const RANGE_MIN : Self; |
69 | } | |
70 | ||
71 | /// Trait for signed numeric types | |
72 | pub trait SignedNum : Num + num::Signed + std::ops::Neg<Output=Self> {} | |
73 | impl<U : Num + num::Signed + std::ops::Neg<Output=Self>> SignedNum for U { } | |
74 | ||
75 | /// Trait for floating point numbers | |
5 | 76 | pub trait Float : SignedNum + num::Float /*+ From<Self::CompatibleSize>*/ { |
77 | // An unsigned integer that can be used for indexing operations and | |
78 | // converted to F without loss. | |
79 | //type CompatibleSize : CompatibleUnsigned<Self>; | |
0 | 80 | |
81 | const PI : Self; | |
82 | const E : Self; | |
83 | const EPSILON : Self; | |
84 | const SQRT_2 : Self; | |
85 | const INFINITY : Self; | |
86 | const NEG_INFINITY : Self; | |
87 | const FRAC_2_SQRT_PI : Self; | |
88 | } | |
89 | ||
90 | /// Trait for integers | |
91 | pub trait Integer : Num + num::Integer {} | |
92 | ||
93 | /// Trait for unsigned integers | |
94 | pub trait Unsigned : Num + Integer + num::Unsigned {} | |
95 | ||
96 | /// Trait for signed integers | |
97 | pub trait Signed : SignedNum + Integer {} | |
98 | ||
99 | macro_rules! impl_num_consts { | |
100 | ($($type:ty)*) => { $( | |
101 | impl Num for $type { | |
102 | const ZERO : Self = 0 as $type; | |
103 | const ONE : Self = 1 as $type; | |
104 | const TWO : Self = 2 as $type; | |
105 | const RANGE_MAX : Self = <$type>::MAX; | |
106 | const RANGE_MIN : Self = <$type>::MIN; | |
107 | } | |
108 | )* } | |
109 | } | |
110 | ||
111 | macro_rules! impl_integers { | |
112 | ($signed:ty : $($type:ty)*) => { $( | |
113 | impl_num_consts!($type); | |
114 | impl Integer for $type {} | |
115 | impl $signed for $type {} | |
116 | )* } | |
117 | } | |
118 | ||
119 | impl_integers!(Signed: i8 i16 i32 i64 i128 isize); | |
120 | impl_integers!(Unsigned: u8 u16 u32 u64 u128 usize); | |
121 | ||
122 | impl_num_consts!(f32 f64); | |
123 | ||
124 | impl Float for f64 { | |
5 | 125 | /*#[cfg(any(target_pointer_width = "128", target_pointer_width = "64"))] |
0 | 126 | type CompatibleSize = u32; |
127 | #[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))] | |
5 | 128 | type CompatibleSize = usize;*/ |
0 | 129 | |
130 | const PI : Self = std::f64::consts::PI; | |
131 | const E : Self = std::f64::consts::E; | |
132 | const EPSILON : Self = std::f64::EPSILON; | |
133 | const SQRT_2 : Self = std::f64::consts::SQRT_2; | |
134 | const INFINITY : Self = std::f64::INFINITY; | |
135 | const NEG_INFINITY : Self = std::f64::NEG_INFINITY; | |
136 | const FRAC_2_SQRT_PI : Self = std::f64::consts::FRAC_2_SQRT_PI; | |
137 | } | |
138 | ||
139 | impl Float for f32 { | |
5 | 140 | /* |
0 | 141 | #[cfg(any(target_pointer_width = "128", target_pointer_width = "64", target_pointer_width = "32"))] |
142 | type CompatibleSize = u16; | |
143 | #[cfg(any(target_pointer_width = "16"))] | |
144 | type CompatibleSize = usize; | |
5 | 145 | */ |
0 | 146 | |
147 | const PI : Self = std::f32::consts::PI; | |
148 | const E : Self = std::f32::consts::E; | |
149 | const EPSILON : Self = std::f32::EPSILON; | |
150 | const SQRT_2 : Self = std::f32::consts::SQRT_2; | |
151 | const INFINITY : Self = std::f32::INFINITY; | |
152 | const NEG_INFINITY : Self = std::f32::NEG_INFINITY; | |
153 | const FRAC_2_SQRT_PI : Self = std::f32::consts::FRAC_2_SQRT_PI; | |
154 | } | |
155 | ||
5 | 156 | /* |
0 | 157 | trait_set! { |
158 | pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>; | |
159 | pub trait CompatibleSigned<F : Float> = Signed + Into<F>; | |
160 | } | |
5 | 161 | */ |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
162 |