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