4 */ |
4 */ |
5 |
5 |
6 use super::base::*; |
6 use super::base::*; |
7 use crate::types::*; |
7 use crate::types::*; |
8 use std::ops::{Div, Mul, DivAssign, MulAssign, Neg}; |
8 use std::ops::{Div, Mul, DivAssign, MulAssign, Neg}; |
9 use serde::ser::{Serialize, Serializer, SerializeStruct}; |
9 use serde::{Serialize, Deserialize}; |
10 use alg_tools::norms::Norm; |
10 use alg_tools::norms::Norm; |
11 use alg_tools::linops::{Mapping, Linear}; |
11 use alg_tools::linops::{Mapping, Linear}; |
12 use alg_tools::instance::{Instance, Space}; |
12 use alg_tools::instance::{Instance, Space}; |
13 |
13 |
14 /// Representation of a delta measure. |
14 /// Representation of a delta measure. |
15 /// |
15 /// |
16 /// This is a single spike $\alpha \delta\_x$ for some location $x$ in `Domain` and |
16 /// This is a single spike $\alpha \delta\_x$ for some location $x$ in `Domain` and |
17 /// a mass $\alpha$ in `F`. |
17 /// a mass $\alpha$ in `F`. |
18 #[derive(Clone,Copy,Debug)] |
18 #[derive(Clone,Copy,Debug,Serialize,Deserialize)] |
19 pub struct DeltaMeasure<Domain, F : Num> { |
19 pub struct DeltaMeasure<Domain, F : Num> { |
20 // This causes [`csv`] to crash. |
20 // This causes [`csv`] to crash. |
21 //#[serde(flatten)] |
21 //#[serde(flatten)] |
22 /// Location of the spike |
22 /// Location of the spike |
23 pub x : Domain, |
23 pub x : Domain, |
24 /// Mass of the spike |
24 /// Mass of the spike |
25 pub α : F |
25 pub α : F |
26 } |
26 } |
27 |
|
28 const COORDINATE_NAMES : &'static [&'static str] = &[ |
|
29 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7" |
|
30 ]; |
|
31 |
|
32 // Need to manually implement serialisation as [`csv`] writer fails on |
|
33 // structs with nested arrays as well as with #[serde(flatten)]. |
|
34 impl<F : Num, const N : usize> Serialize for DeltaMeasure<Loc<F, N>, F> |
|
35 where |
|
36 F: Serialize, |
|
37 { |
|
38 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
|
39 where |
|
40 S: Serializer, |
|
41 { |
|
42 assert!(N <= COORDINATE_NAMES.len()); |
|
43 |
|
44 let mut s = serializer.serialize_struct("DeltaMeasure", N+1)?; |
|
45 for (i, e) in (0..).zip(self.x.iter()) { |
|
46 s.serialize_field(COORDINATE_NAMES[i], e)?; |
|
47 } |
|
48 s.serialize_field("weight", &self.α)?; |
|
49 s.end() |
|
50 } |
|
51 } |
|
52 |
|
53 |
27 |
54 impl<Domain, F : Float> Measure<F> for DeltaMeasure<Domain, F> { |
28 impl<Domain, F : Float> Measure<F> for DeltaMeasure<Domain, F> { |
55 type Domain = Domain; |
29 type Domain = Domain; |
56 } |
30 } |
57 |
31 |