Wed, 03 Sep 2025 17:59:24 -0500
No supertraits for Instance
| 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; | |
| 150 | 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::{ |
| 150 | 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. | |
| 150 | 34 | pub trait CastFrom<T: 'static + Copy>: num_traits::cast::AsPrimitive<T> { |
| 35 | fn cast_from(other: T) -> Self; | |
| 0 | 36 | } |
| 37 | ||
| 38 | macro_rules! impl_casts { | |
| 39 | ($($type:ty)*) => { $( | |
| 40 | impl_casts!(@phase2, $type, | |
| 41 | u8 u16 u32 u64 u128 usize | |
| 42 | i8 i16 i32 i64 i128 isize | |
| 43 | f32 f64); | |
| 44 | )* }; | |
| 45 | (@phase2, $type:ty, $($type2:ty)*) => { $( | |
| 46 | impl CastFrom<$type2> for $type { | |
| 47 | #[inline] | |
| 48 | fn cast_from(other : $type2) -> Self { other as $type } | |
| 49 | } | |
| 50 | )* }; | |
| 51 | } | |
| 52 | ||
| 53 | impl_casts!(u8 u16 u32 u64 u128 usize | |
| 54 | i8 i16 i32 i64 i128 isize | |
| 55 | f32 f64); | |
| 56 | ||
| 5 | 57 | /// Trait for general numeric types |
| 150 | 58 | pub trait Num: |
| 59 | 'static | |
| 60 | + Copy | |
| 61 | + Sync | |
| 62 | + Send | |
| 63 | + num::Num | |
| 64 | + num_traits::NumAssign | |
| 65 | + std::iter::Sum | |
| 66 | + std::iter::Product | |
| 67 | + std::fmt::Debug | |
| 68 | + std::fmt::Display | |
| 69 | + serde::Serialize | |
| 70 | + CastFrom<u8> | |
| 71 | + CastFrom<u16> | |
| 72 | + CastFrom<u32> | |
| 73 | + CastFrom<u64> | |
| 74 | + CastFrom<u128> | |
| 75 | + CastFrom<usize> | |
| 76 | + CastFrom<i8> | |
| 77 | + CastFrom<i16> | |
| 78 | + CastFrom<i32> | |
| 79 | + CastFrom<i64> | |
| 80 | + CastFrom<i128> | |
| 81 | + CastFrom<isize> | |
| 82 | + CastFrom<f32> | |
| 83 | + CastFrom<f64> | |
| 84 | + crate::instance::ClosedSpace | |
| 85 | { | |
| 86 | const ZERO: Self; | |
| 87 | const ONE: Self; | |
| 88 | const TWO: Self; | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
89 | /// Generic version of `Self::MAX` |
| 150 | 90 | const RANGE_MAX: Self; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
91 | /// Generic version of `Self::MIN` |
| 150 | 92 | const RANGE_MIN: Self; |
| 0 | 93 | } |
| 94 | ||
| 95 | /// Trait for signed numeric types | |
| 150 | 96 | pub trait SignedNum: Num + num::Signed + std::ops::Neg<Output = Self> {} |
| 97 | impl<U: Num + num::Signed + std::ops::Neg<Output = Self>> SignedNum for U {} | |
| 0 | 98 | |
| 99 | /// Trait for floating point numbers | |
| 150 | 100 | pub trait Float: SignedNum + num::Float /*+ From<Self::CompatibleSize>*/ { |
| 5 | 101 | // An unsigned integer that can be used for indexing operations and |
| 102 | // converted to F without loss. | |
| 103 | //type CompatibleSize : CompatibleUnsigned<Self>; | |
| 0 | 104 | |
| 150 | 105 | const PI: Self; |
| 106 | const E: Self; | |
| 107 | const EPSILON: Self; | |
| 108 | const SQRT_2: Self; | |
| 109 | const INFINITY: Self; | |
| 110 | const NEG_INFINITY: Self; | |
| 111 | const NAN: Self; | |
| 112 | const FRAC_2_SQRT_PI: Self; | |
| 0 | 113 | } |
| 114 | ||
| 115 | /// Trait for integers | |
| 150 | 116 | pub trait Integer: Num + num::Integer {} |
| 0 | 117 | |
| 118 | /// Trait for unsigned integers | |
| 150 | 119 | pub trait Unsigned: Num + Integer + num::Unsigned {} |
| 0 | 120 | |
| 121 | /// Trait for signed integers | |
| 150 | 122 | pub trait Signed: SignedNum + Integer {} |
| 0 | 123 | |
| 124 | macro_rules! impl_num_consts { | |
| 125 | ($($type:ty)*) => { $( | |
| 126 | impl Num for $type { | |
| 127 | const ZERO : Self = 0 as $type; | |
| 128 | const ONE : Self = 1 as $type; | |
| 129 | const TWO : Self = 2 as $type; | |
| 130 | const RANGE_MAX : Self = <$type>::MAX; | |
| 131 | const RANGE_MIN : Self = <$type>::MIN; | |
| 132 | } | |
| 133 | )* } | |
| 134 | } | |
| 135 | ||
| 136 | macro_rules! impl_integers { | |
| 137 | ($signed:ty : $($type:ty)*) => { $( | |
| 138 | impl_num_consts!($type); | |
| 139 | impl Integer for $type {} | |
| 140 | impl $signed for $type {} | |
| 141 | )* } | |
| 142 | } | |
| 143 | ||
| 144 | impl_integers!(Signed: i8 i16 i32 i64 i128 isize); | |
| 145 | impl_integers!(Unsigned: u8 u16 u32 u64 u128 usize); | |
| 146 | ||
| 147 | impl_num_consts!(f32 f64); | |
| 148 | ||
| 149 | impl Float for f64 { | |
| 5 | 150 | /*#[cfg(any(target_pointer_width = "128", target_pointer_width = "64"))] |
| 0 | 151 | type CompatibleSize = u32; |
| 152 | #[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))] | |
| 5 | 153 | type CompatibleSize = usize;*/ |
| 0 | 154 | |
| 150 | 155 | const PI: Self = std::f64::consts::PI; |
| 156 | const E: Self = std::f64::consts::E; | |
| 157 | const EPSILON: Self = std::f64::EPSILON; | |
| 158 | const SQRT_2: Self = std::f64::consts::SQRT_2; | |
| 159 | const INFINITY: Self = std::f64::INFINITY; | |
| 160 | const NEG_INFINITY: Self = std::f64::NEG_INFINITY; | |
| 161 | const NAN: Self = std::f64::NAN; | |
| 162 | const FRAC_2_SQRT_PI: Self = std::f64::consts::FRAC_2_SQRT_PI; | |
| 0 | 163 | } |
| 164 | ||
| 165 | impl Float for f32 { | |
| 5 | 166 | /* |
| 0 | 167 | #[cfg(any(target_pointer_width = "128", target_pointer_width = "64", target_pointer_width = "32"))] |
| 168 | type CompatibleSize = u16; | |
| 169 | #[cfg(any(target_pointer_width = "16"))] | |
| 170 | type CompatibleSize = usize; | |
| 5 | 171 | */ |
| 0 | 172 | |
| 150 | 173 | const PI: Self = std::f32::consts::PI; |
| 174 | const E: Self = std::f32::consts::E; | |
| 175 | const EPSILON: Self = std::f32::EPSILON; | |
| 176 | const SQRT_2: Self = std::f32::consts::SQRT_2; | |
| 177 | const INFINITY: Self = std::f32::INFINITY; | |
| 178 | const NEG_INFINITY: Self = std::f32::NEG_INFINITY; | |
| 179 | const NAN: Self = std::f32::NAN; | |
| 180 | const FRAC_2_SQRT_PI: Self = std::f32::consts::FRAC_2_SQRT_PI; | |
| 0 | 181 | } |
| 182 | ||
| 5 | 183 | /* |
| 0 | 184 | trait_set! { |
| 185 | pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>; | |
| 186 | pub trait CompatibleSigned<F : Float> = Signed + Into<F>; | |
| 187 | } | |
| 5 | 188 | */ |