peroxide/util/
print.rs

1//! Easy to print any structures
2
3use crate::statistics::dist::*;
4use crate::statistics::stat::ConfusionMatrix;
5#[allow(unused_imports)]
6use crate::structure::{
7    ad::AD,
8    dataframe::{DType, DTypeArray, DataFrame, Scalar, Series},
9    matrix::Matrix,
10    multinomial::Multinomial,
11    polynomial::Polynomial,
12};
13use rand_distr::uniform::SampleUniform;
14use std::fmt::{Debug, LowerExp, UpperExp};
15
16pub trait Printable {
17    fn print(&self);
18}
19
20impl Printable for usize {
21    fn print(&self) {
22        println!("{}", self);
23    }
24}
25
26impl Printable for u8 {
27    fn print(&self) {
28        println!("{}", self);
29    }
30}
31
32impl Printable for u16 {
33    fn print(&self) {
34        println!("{}", self);
35    }
36}
37
38impl Printable for u32 {
39    fn print(&self) {
40        println!("{}", self);
41    }
42}
43
44impl Printable for u64 {
45    fn print(&self) {
46        println!("{}", self);
47    }
48}
49
50impl Printable for isize {
51    fn print(&self) {
52        println!("{}", self);
53    }
54}
55
56impl Printable for i8 {
57    fn print(&self) {
58        println!("{}", self);
59    }
60}
61
62impl Printable for i16 {
63    fn print(&self) {
64        println!("{}", self);
65    }
66}
67
68impl Printable for i32 {
69    fn print(&self) {
70        println!("{}", self);
71    }
72}
73
74impl Printable for i64 {
75    fn print(&self) {
76        println!("{}", self);
77    }
78}
79
80impl Printable for f32 {
81    fn print(&self) {
82        println!("{}", self);
83    }
84}
85
86impl Printable for f64 {
87    fn print(&self) {
88        println!("{}", self);
89    }
90}
91
92impl Printable for char {
93    fn print(&self) {
94        println!("{}", self);
95    }
96}
97
98impl Printable for bool {
99    fn print(&self) {
100        println!("{}", self);
101    }
102}
103
104impl Printable for String {
105    fn print(&self) {
106        println!("{}", self);
107    }
108}
109
110impl Printable for Vec<usize> {
111    fn print(&self) {
112        println!("{:?}", self);
113    }
114}
115
116impl Printable for Vec<u8> {
117    fn print(&self) {
118        println!("{:?}", self);
119    }
120}
121
122impl Printable for Vec<u16> {
123    fn print(&self) {
124        println!("{:?}", self);
125    }
126}
127
128impl Printable for Vec<u32> {
129    fn print(&self) {
130        println!("{:?}", self);
131    }
132}
133
134impl Printable for Vec<u64> {
135    fn print(&self) {
136        println!("{:?}", self);
137    }
138}
139
140impl Printable for Vec<isize> {
141    fn print(&self) {
142        println!("{:?}", self);
143    }
144}
145
146impl Printable for Vec<i8> {
147    fn print(&self) {
148        println!("{:?}", self);
149    }
150}
151
152impl Printable for Vec<i16> {
153    fn print(&self) {
154        println!("{:?}", self);
155    }
156}
157
158impl Printable for Vec<i32> {
159    fn print(&self) {
160        println!("{:?}", self);
161    }
162}
163
164impl Printable for Vec<i64> {
165    fn print(&self) {
166        println!("{:?}", self);
167    }
168}
169
170impl Printable for Vec<char> {
171    fn print(&self) {
172        println!("{:?}", self);
173    }
174}
175
176impl Printable for Vec<&str> {
177    fn print(&self) {
178        println!("{:?}", self);
179    }
180}
181
182impl Printable for Vec<String> {
183    fn print(&self) {
184        println!("{:?}", self);
185    }
186}
187
188macro_rules! format_float_vec {
189    ($self:expr) => {{
190        let mut result = String::new();
191        result.push_str("[");
192        for i in 0..$self.len() {
193            let st1 = $self[i].fmt_lower_exp(2);
194            let st2 = $self[i].to_string();
195            let st = if st1.len() < st2.len() { st1 } else { st2 };
196            result.push_str(&st);
197            if i == $self.len() - 1 {
198                break;
199            }
200            result.push_str(", ");
201        }
202        result.push_str("]");
203        result
204    }};
205}
206
207impl Printable for Vec<f32> {
208    fn print(&self) {
209        let result = format_float_vec!(self);
210        println!("{}", result);
211    }
212}
213
214impl Printable for Vec<f64> {
215    fn print(&self) {
216        let result = format_float_vec!(self);
217        println!("{}", result);
218    }
219}
220
221impl Printable for &Vec<usize> {
222    fn print(&self) {
223        println!("{:?}", self);
224    }
225}
226
227impl Printable for &Vec<u8> {
228    fn print(&self) {
229        println!("{:?}", self);
230    }
231}
232
233impl Printable for &Vec<u16> {
234    fn print(&self) {
235        println!("{:?}", self);
236    }
237}
238
239impl Printable for &Vec<u32> {
240    fn print(&self) {
241        println!("{:?}", self);
242    }
243}
244
245impl Printable for &Vec<u64> {
246    fn print(&self) {
247        println!("{:?}", self);
248    }
249}
250
251impl Printable for &Vec<isize> {
252    fn print(&self) {
253        println!("{:?}", self);
254    }
255}
256
257impl Printable for &Vec<i8> {
258    fn print(&self) {
259        println!("{:?}", self);
260    }
261}
262
263impl Printable for &Vec<i16> {
264    fn print(&self) {
265        println!("{:?}", self);
266    }
267}
268
269impl Printable for &Vec<i32> {
270    fn print(&self) {
271        println!("{:?}", self);
272    }
273}
274
275impl Printable for &Vec<i64> {
276    fn print(&self) {
277        println!("{:?}", self);
278    }
279}
280
281impl Printable for &Vec<char> {
282    fn print(&self) {
283        println!("{:?}", self);
284    }
285}
286
287impl Printable for &Vec<&str> {
288    fn print(&self) {
289        println!("{:?}", self);
290    }
291}
292
293impl Printable for &Vec<String> {
294    fn print(&self) {
295        println!("{:?}", self);
296    }
297}
298
299impl Printable for &Vec<bool> {
300    fn print(&self) {
301        println!("{:?}", self);
302    }
303}
304
305impl Printable for &Vec<f32> {
306    fn print(&self) {
307        let result = format_float_vec!(self);
308        println!("{}", result);
309    }
310}
311
312impl Printable for &Vec<f64> {
313    fn print(&self) {
314        let result = format_float_vec!(self);
315        println!("{}", result);
316    }
317}
318
319impl Printable for Matrix {
320    fn print(&self) {
321        println!("{}", self);
322    }
323}
324
325impl Printable for Polynomial {
326    fn print(&self) {
327        println!("{}", self);
328    }
329}
330
331//impl Printable for Dual {
332//    fn print(&self) {
333//        println!("{}", self);
334//    }
335//}
336
337impl Printable for Multinomial {
338    fn print(&self) {
339        println!("{}", self);
340    }
341}
342
343//impl Printable for Vec<Dual> {
344//    fn print(&self) {
345//        println!("value:");
346//        self.values().print();
347//        println!("slope:");
348//        self.slopes().print();
349//    }
350//}
351//
352//impl Printable for HyperDual {
353//    fn print(&self) {
354//        println!("{}", self);
355//    }
356//}
357
358impl<T: Debug + PartialOrd + SampleUniform + Copy + Into<f64>> Printable for OPDist<T> {
359    fn print(&self) {
360        println!("{:?}", self);
361    }
362}
363
364impl<T: Debug + PartialOrd + SampleUniform + Copy + Into<f64>> Printable for TPDist<T> {
365    fn print(&self) {
366        println!("{:?}", self);
367    }
368}
369
370//impl Printable for Number {
371//    fn print(&self) {
372//        println!("{:?}", self)
373//    }
374//}
375//
376//impl Printable for Vec<Number> {
377//    fn print(&self) {
378//        println!("{:?}", self);
379//    }
380//}
381
382impl Printable for DType {
383    fn print(&self) {
384        println!("{}", self)
385    }
386}
387
388impl Printable for DTypeArray {
389    fn print(&self) {
390        println!("{}", self)
391    }
392}
393
394impl Printable for Scalar {
395    fn print(&self) {
396        println!("{}", self)
397    }
398}
399
400impl Printable for Series {
401    fn print(&self) {
402        self.values.print();
403    }
404}
405
406impl Printable for DataFrame {
407    fn print(&self) {
408        println!("{}", self)
409    }
410}
411
412impl Printable for AD {
413    fn print(&self) {
414        println!("{}", self)
415    }
416}
417
418impl Printable for ConfusionMatrix {
419    fn print(&self) {
420        println!("{}", self.to_matrix())
421    }
422}
423
424/// Format float number into lower exponent notation with '+' sign
425///
426/// # Example
427///
428/// ```rust
429/// use peroxide::fuga::*;
430///
431/// fn main() {
432///     let x = 123.456;
433///     assert_eq!(x.fmt_lower_exp(2), "1.23e+2");
434/// }
435/// ```
436pub trait LowerExpWithPlus: LowerExp {
437    fn fmt_lower_exp(&self, precision: usize) -> String {
438        let mut s = format!("{:.p$e}", self, p = precision);
439        let s_old = s.clone();
440        let mut e = s.split_off(s.find('e').unwrap());
441        if e.starts_with("e-") {
442            s_old
443        } else {
444            e.insert(1, '+');
445            format!("{}{}", s, e)
446        }
447    }
448}
449
450impl LowerExpWithPlus for f32 {}
451impl LowerExpWithPlus for f64 {}
452
453/// Format float number into upper exponent notation with '+' sign
454///
455/// # Example
456///
457/// ```rust
458/// use peroxide::fuga::*;
459///
460/// fn main() {
461///     let x = 123.456;
462///     assert_eq!(x.fmt_upper_exp(2), "1.23E+2");
463/// }
464/// ```
465pub trait UpperExpWithPlus: UpperExp {
466    fn fmt_upper_exp(&self, precision: usize) -> String {
467        let mut s = format!("{:.p$E}", self, p = precision);
468        let s_old = s.clone();
469        let mut e = s.split_off(s.find('E').unwrap());
470        if e.starts_with("E-") {
471            s_old
472        } else {
473            e.insert(1, '+');
474            format!("{}{}", s, e)
475        }
476    }
477}
478
479impl UpperExpWithPlus for f32 {}
480impl UpperExpWithPlus for f64 {}
481
482//impl<A: Array<Item=f64>> Printable for AD<A> {
483//    fn print(&self) {
484//        let mut result = String::new();
485//        result.push_str("AD [");
486//        for i in 0..self.len() {
487//            let st1 = format!("{:.4}", self[i]);
488//            let st2 = self[i].to_string();
489//            let mut st = st2.clone();
490//
491//            if st1.len() < st2.len() {
492//                st = st1;
493//            }
494//
495//            result.push_str(&st);
496//            if i == self.len() - 1 {
497//                break;
498//            }
499//            result.push_str(", ");
500//        }
501//        result.push_str("]");
502//
503//        println!("{}", result);
504//    }
505//}