Thu, 01 May 2025 12:29:18 -0500
numseal
| 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; | |
| 123 | 13 | pub use num_traits::cast::AsPrimitive; |
| 0 | 14 | pub use num_traits::Float as NumTraitsFloat; // needed to re-export functions. |
|
62
d8305c9b6fdf
Move origin stuff to AXPY form Euclidean
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
15 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
16 | pub use simba::scalar::{ |
| 123 | 17 | ClosedAdd, ClosedAddAssign, ClosedDiv, ClosedDivAssign, ClosedMul, ClosedMulAssign, ClosedNeg, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
18 | ClosedSub, ClosedSubAssign, |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
19 | }; |
| 0 | 20 | |
| 21 | /// Typical integer type | |
| 22 | #[allow(non_camel_case_types)] | |
| 23 | pub type int = i64; | |
| 24 | ||
| 25 | /// Typical unsigned integer type | |
| 26 | #[allow(non_camel_case_types)] | |
| 27 | pub type uint = u64; | |
| 28 | ||
| 29 | /// Typical floating point number type | |
| 30 | #[allow(non_camel_case_types)] | |
| 31 | pub type float = f64; | |
| 32 | ||
| 33 | /// Casts of abstract numerical types to others via the standard `as` keyword. | |
| 123 | 34 | pub trait CastFrom<T: 'static + Copy>: num_traits::cast::AsPrimitive<T> { |
| 35 | fn cast_from(other: T) -> Self; | |
| 36 | } | |
| 37 | ||
| 38 | mod private { | |
| 39 | pub trait NumSeal {} | |
| 0 | 40 | } |
| 41 | ||
| 42 | macro_rules! impl_casts { | |
| 43 | ($($type:ty)*) => { $( | |
| 44 | impl_casts!(@phase2, $type, | |
| 45 | u8 u16 u32 u64 u128 usize | |
| 46 | i8 i16 i32 i64 i128 isize | |
| 47 | f32 f64); | |
| 48 | )* }; | |
| 49 | (@phase2, $type:ty, $($type2:ty)*) => { $( | |
| 50 | impl CastFrom<$type2> for $type { | |
| 51 | #[inline] | |
| 52 | fn cast_from(other : $type2) -> Self { other as $type } | |
| 53 | } | |
| 54 | )* }; | |
| 55 | } | |
| 56 | ||
| 57 | impl_casts!(u8 u16 u32 u64 u128 usize | |
| 58 | i8 i16 i32 i64 i128 isize | |
| 59 | f32 f64); | |
| 60 | ||
| 5 | 61 | /// Trait for general numeric types |
| 123 | 62 | pub trait Num: |
| 63 | 'static | |
| 64 | + Copy | |
| 65 | + Sync | |
| 66 | + Send | |
| 67 | + num::Num | |
| 68 | + num_traits::NumAssign | |
| 69 | + std::iter::Sum | |
| 70 | + std::iter::Product | |
| 71 | + std::fmt::Debug | |
| 72 | + std::fmt::Display | |
| 73 | + serde::Serialize | |
| 74 | + CastFrom<u8> | |
| 75 | + CastFrom<u16> | |
| 76 | + CastFrom<u32> | |
| 77 | + CastFrom<u64> | |
| 78 | + CastFrom<u128> | |
| 79 | + CastFrom<usize> | |
| 80 | + CastFrom<i8> | |
| 81 | + CastFrom<i16> | |
| 82 | + CastFrom<i32> | |
| 83 | + CastFrom<i64> | |
| 84 | + CastFrom<i128> | |
| 85 | + CastFrom<isize> | |
| 86 | + CastFrom<f32> | |
| 87 | + CastFrom<f64> | |
| 88 | + crate::instance::Space | |
| 89 | + private::NumSeal | |
| 90 | { | |
| 91 | const ZERO: Self; | |
| 92 | const ONE: Self; | |
| 93 | const TWO: Self; | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
94 | /// Generic version of `Self::MAX` |
| 123 | 95 | const RANGE_MAX: Self; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
96 | /// Generic version of `Self::MIN` |
| 123 | 97 | const RANGE_MIN: Self; |
| 0 | 98 | } |
| 99 | ||
| 100 | /// Trait for signed numeric types | |
| 123 | 101 | pub trait SignedNum: Num + num::Signed + std::ops::Neg<Output = Self> {} |
| 102 | impl<U: Num + num::Signed + std::ops::Neg<Output = Self>> SignedNum for U {} | |
| 0 | 103 | |
| 104 | /// Trait for floating point numbers | |
| 123 | 105 | pub trait Float: SignedNum + num::Float /*+ From<Self::CompatibleSize>*/ { |
| 5 | 106 | // An unsigned integer that can be used for indexing operations and |
| 107 | // converted to F without loss. | |
| 108 | //type CompatibleSize : CompatibleUnsigned<Self>; | |
| 0 | 109 | |
| 123 | 110 | const PI: Self; |
| 111 | const E: Self; | |
| 112 | const EPSILON: Self; | |
| 113 | const SQRT_2: Self; | |
| 114 | const INFINITY: Self; | |
| 115 | const NEG_INFINITY: Self; | |
| 116 | const NAN: Self; | |
| 117 | const FRAC_2_SQRT_PI: Self; | |
| 0 | 118 | } |
| 119 | ||
| 120 | /// Trait for integers | |
| 123 | 121 | pub trait Integer: Num + num::Integer {} |
| 0 | 122 | |
| 123 | /// Trait for unsigned integers | |
| 123 | 124 | pub trait Unsigned: Num + Integer + num::Unsigned {} |
| 0 | 125 | |
| 126 | /// Trait for signed integers | |
| 123 | 127 | pub trait Signed: SignedNum + Integer {} |
| 0 | 128 | |
| 129 | macro_rules! impl_num_consts { | |
| 130 | ($($type:ty)*) => { $( | |
| 131 | impl Num for $type { | |
| 132 | const ZERO : Self = 0 as $type; | |
| 133 | const ONE : Self = 1 as $type; | |
| 134 | const TWO : Self = 2 as $type; | |
| 135 | const RANGE_MAX : Self = <$type>::MAX; | |
| 136 | const RANGE_MIN : Self = <$type>::MIN; | |
| 137 | } | |
| 138 | )* } | |
| 139 | } | |
| 140 | ||
| 141 | macro_rules! impl_integers { | |
| 142 | ($signed:ty : $($type:ty)*) => { $( | |
| 143 | impl_num_consts!($type); | |
| 144 | impl Integer for $type {} | |
| 145 | impl $signed for $type {} | |
| 123 | 146 | impl private::NumSeal for $type {} |
| 0 | 147 | )* } |
| 148 | } | |
| 149 | ||
| 150 | impl_integers!(Signed: i8 i16 i32 i64 i128 isize); | |
| 151 | impl_integers!(Unsigned: u8 u16 u32 u64 u128 usize); | |
| 152 | ||
| 153 | impl_num_consts!(f32 f64); | |
| 154 | ||
| 155 | impl Float for f64 { | |
| 5 | 156 | /*#[cfg(any(target_pointer_width = "128", target_pointer_width = "64"))] |
| 0 | 157 | type CompatibleSize = u32; |
| 158 | #[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))] | |
| 5 | 159 | type CompatibleSize = usize;*/ |
| 0 | 160 | |
| 123 | 161 | const PI: Self = std::f64::consts::PI; |
| 162 | const E: Self = std::f64::consts::E; | |
| 163 | const EPSILON: Self = std::f64::EPSILON; | |
| 164 | const SQRT_2: Self = std::f64::consts::SQRT_2; | |
| 165 | const INFINITY: Self = std::f64::INFINITY; | |
| 166 | const NEG_INFINITY: Self = std::f64::NEG_INFINITY; | |
| 167 | const NAN: Self = std::f64::NAN; | |
| 168 | const FRAC_2_SQRT_PI: Self = std::f64::consts::FRAC_2_SQRT_PI; | |
| 0 | 169 | } |
| 170 | ||
| 171 | impl Float for f32 { | |
| 5 | 172 | /* |
| 0 | 173 | #[cfg(any(target_pointer_width = "128", target_pointer_width = "64", target_pointer_width = "32"))] |
| 174 | type CompatibleSize = u16; | |
| 175 | #[cfg(any(target_pointer_width = "16"))] | |
| 176 | type CompatibleSize = usize; | |
| 5 | 177 | */ |
| 0 | 178 | |
| 123 | 179 | const PI: Self = std::f32::consts::PI; |
| 180 | const E: Self = std::f32::consts::E; | |
| 181 | const EPSILON: Self = std::f32::EPSILON; | |
| 182 | const SQRT_2: Self = std::f32::consts::SQRT_2; | |
| 183 | const INFINITY: Self = std::f32::INFINITY; | |
| 184 | const NEG_INFINITY: Self = std::f32::NEG_INFINITY; | |
| 185 | const NAN: Self = std::f32::NAN; | |
| 186 | const FRAC_2_SQRT_PI: Self = std::f32::consts::FRAC_2_SQRT_PI; | |
| 0 | 187 | } |
| 188 | ||
| 123 | 189 | impl private::NumSeal for f32 {} |
| 190 | impl private::NumSeal for f64 {} | |
| 191 | ||
| 5 | 192 | /* |
| 0 | 193 | trait_set! { |
| 194 | pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>; | |
| 195 | pub trait CompatibleSigned<F : Float> = Signed + Into<F>; | |
| 196 | } | |
| 5 | 197 | */ |