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