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