sobol_qmc/
type_support.rs1use crate::{
2 GaussianRender, InternalType, LossyFrom, MultiDimGaussianRender, Render, SobolType, UnitRender,
3};
4use statrs::distribution::ContinuousCDF as _;
5
6impl SobolType for f32 {
8 type IT = u32;
9 const MAX_RESOLUTION: usize = 24;
11}
12impl Render<f32> for UnitRender {
13 fn render(&self, _: usize, val: u32) -> f32 {
14 (val as f32) / 4_294_967_296_f32
16 }
17}
18impl Render<f32> for GaussianRender {
19 fn render(&self, dim: usize, val: u32) -> f32 {
20 self.0
21 .inverse_cdf(<UnitRender as Render<f32>>::render(&UnitRender, dim, val) as f64)
22 as f32
23 }
24}
25impl Render<f32> for MultiDimGaussianRender {
26 fn render(&self, dim: usize, val: u32) -> f32 {
27 self.0[dim].inverse_cdf(<UnitRender as Render<f32>>::render(&UnitRender, dim, val) as f64)
28 as f32
29 }
30 fn support_dims(&self) -> Option<usize> {
31 Some(self.0.len())
32 }
33}
34
35impl SobolType for f64 {
37 type IT = u64;
38 const MAX_RESOLUTION: usize = 53;
40}
41impl Render<f64> for UnitRender {
42 fn render(&self, _: usize, val: u64) -> f64 {
43 (val as f64) / 18_446_744_073_709_551_616_f64
45 }
46}
47impl Render<f64> for GaussianRender {
48 fn render(&self, dim: usize, val: u64) -> f64 {
49 self.0
50 .inverse_cdf(<UnitRender as Render<f64>>::render(&UnitRender, dim, val))
51 }
52}
53impl Render<f64> for MultiDimGaussianRender {
54 fn render(&self, dim: usize, val: u64) -> f64 {
55 self.0[dim].inverse_cdf(<UnitRender as Render<f64>>::render(&UnitRender, dim, val))
56 }
57 fn support_dims(&self) -> Option<usize> {
58 Some(self.0.len())
59 }
60}
61
62impl SobolType for u8 {
64 type IT = u8;
65}
66impl Render<u8> for UnitRender {
67 fn render(&self, _: usize, val: u8) -> u8 {
68 val
69 }
70}
71
72impl SobolType for u16 {
74 type IT = u16;
75}
76impl Render<u16> for UnitRender {
77 fn render(&self, _: usize, val: u16) -> u16 {
78 val
79 }
80}
81
82impl SobolType for u32 {
84 type IT = u32;
85}
86impl Render<u32> for UnitRender {
87 fn render(&self, _: usize, val: u32) -> u32 {
88 val
89 }
90}
91
92impl SobolType for u64 {
94 type IT = u64;
95}
96impl Render<u64> for UnitRender {
97 fn render(&self, _: usize, val: u64) -> u64 {
98 val
99 }
100}
101
102impl SobolType for u128 {
104 type IT = u128;
105}
106impl Render<u128> for UnitRender {
107 fn render(&self, _: usize, val: u128) -> u128 {
108 val
109 }
110}
111
112impl SobolType for i8 {
114 type IT = u8;
115}
116impl Render<i8> for UnitRender {
117 fn render(&self, _: usize, val: u8) -> i8 {
118 (val ^ 0x80) as i8
119 }
120}
121
122impl SobolType for i16 {
124 type IT = u16;
125}
126impl Render<i16> for UnitRender {
127 fn render(&self, _: usize, val: u16) -> i16 {
128 (val ^ 0x8000) as i16
129 }
130}
131
132impl SobolType for i32 {
134 type IT = u32;
135}
136impl Render<i32> for UnitRender {
137 fn render(&self, _: usize, val: u32) -> i32 {
138 (val ^ 0x8000_0000) as i32
139 }
140}
141
142impl SobolType for i64 {
144 type IT = u64;
145}
146impl Render<i64> for UnitRender {
147 fn render(&self, _: usize, val: u64) -> i64 {
148 (val ^ 0x8000_0000_0000_0000) as i64
149 }
150}
151
152impl SobolType for i128 {
154 type IT = u128;
155}
156impl Render<i128> for UnitRender {
157 fn render(&self, _: usize, val: u128) -> i128 {
158 (val ^ 0x8000_0000_0000_0000_0000_0000_0000_0000) as i128
159 }
160}
161
162impl InternalType for u8 {
164 const BITS: usize = 8;
165}
166
167impl InternalType for u16 {
169 const BITS: usize = 16;
170}
171
172impl InternalType for u32 {
174 const BITS: usize = 32;
175}
176
177impl InternalType for u64 {
179 const BITS: usize = 64;
180}
181
182impl InternalType for u128 {
184 const BITS: usize = 128;
185}
186
187impl<T> LossyFrom<T> for T {
189 fn lossy_from(val: T) -> T {
190 val
191 }
192}
193
194impl LossyFrom<u16> for u8 {
196 fn lossy_from(val: u16) -> u8 {
197 val as u8
198 }
199}
200
201impl LossyFrom<u16> for u32 {
203 fn lossy_from(val: u16) -> u32 {
204 u32::from(val)
205 }
206}
207
208impl LossyFrom<u16> for u64 {
210 fn lossy_from(val: u16) -> u64 {
211 u64::from(val)
212 }
213}
214
215impl LossyFrom<u16> for u128 {
217 fn lossy_from(val: u16) -> u128 {
218 u128::from(val)
219 }
220}
221
222impl LossyFrom<u32> for u8 {
224 fn lossy_from(val: u32) -> u8 {
225 val as u8
226 }
227}
228
229impl LossyFrom<u32> for u16 {
231 fn lossy_from(val: u32) -> u16 {
232 val as u16
233 }
234}
235
236impl LossyFrom<u32> for u64 {
238 fn lossy_from(val: u32) -> u64 {
239 u64::from(val)
240 }
241}
242
243impl LossyFrom<u32> for u128 {
245 fn lossy_from(val: u32) -> u128 {
246 u128::from(val)
247 }
248}
249
250impl LossyFrom<u64> for u8 {
252 fn lossy_from(val: u64) -> u8 {
253 val as u8
254 }
255}
256
257impl LossyFrom<u64> for u16 {
259 fn lossy_from(val: u64) -> u16 {
260 val as u16
261 }
262}
263
264impl LossyFrom<u64> for u32 {
266 fn lossy_from(val: u64) -> u32 {
267 val as u32
268 }
269}
270
271impl LossyFrom<u64> for u128 {
273 fn lossy_from(val: u64) -> u128 {
274 u128::from(val)
275 }
276}
277
278impl LossyFrom<u128> for u8 {
280 fn lossy_from(val: u128) -> u8 {
281 val as u8
282 }
283}
284
285impl LossyFrom<u128> for u16 {
287 fn lossy_from(val: u128) -> u16 {
288 val as u16
289 }
290}
291
292impl LossyFrom<u128> for u32 {
294 fn lossy_from(val: u128) -> u32 {
295 val as u32
296 }
297}
298
299impl LossyFrom<u128> for u64 {
301 fn lossy_from(val: u128) -> u64 {
302 val as u64
303 }
304}