4 Div,Mul,DivAssign,MulAssign,Neg, |
4 Div,Mul,DivAssign,MulAssign,Neg, |
5 Add,Sub,AddAssign,SubAssign, |
5 Add,Sub,AddAssign,SubAssign, |
6 Index,IndexMut, |
6 Index,IndexMut, |
7 }; |
7 }; |
8 use std::iter::Sum; |
8 use std::iter::Sum; |
9 use serde::ser::{Serializer, Serialize, SerializeSeq}; |
9 use serde::{Serialize, Deserialize}; |
10 use nalgebra::DVector; |
10 use nalgebra::DVector; |
11 |
11 |
12 use alg_tools::norms::Norm; |
12 use alg_tools::norms::Norm; |
13 use alg_tools::tabledump::TableDump; |
13 use alg_tools::tabledump::TableDump; |
14 use alg_tools::linops::{Mapping, Linear}; |
14 use alg_tools::linops::{Mapping, Linear}; |
24 /// Representation of a discrete measure. |
24 /// Representation of a discrete measure. |
25 /// |
25 /// |
26 /// This is the measure $μ = ∑_{k=1}^n α_k δ_{x_k}$, consisting of several |
26 /// This is the measure $μ = ∑_{k=1}^n α_k δ_{x_k}$, consisting of several |
27 /// [`DeltaMeasure`], i.e., “spikes” $α_k δ_{x_k}$ with weights $\alpha_k$ in `F` at locations |
27 /// [`DeltaMeasure`], i.e., “spikes” $α_k δ_{x_k}$ with weights $\alpha_k$ in `F` at locations |
28 /// $x_k$ in `Domain`. |
28 /// $x_k$ in `Domain`. |
29 #[derive(Clone,Debug)] |
29 #[derive(Clone,Debug,Serialize,Deserialize)] |
30 pub struct DiscreteMeasure<Domain, F : Num> { |
30 pub struct DiscreteMeasure<Domain, F : Num> { |
31 pub(super) spikes : Vec<DeltaMeasure<Domain, F>>, |
31 pub(super) spikes : Vec<DeltaMeasure<Domain, F>>, |
32 } |
32 } |
33 |
33 |
34 pub type RNDM<F, const N : usize> = DiscreteMeasure<Loc<F, N>, F>; |
34 pub type RNDM<F, const N : usize> = DiscreteMeasure<Loc<F, N>, F>; |
454 // Ensure order matching the headers above |
454 // Ensure order matching the headers above |
455 self.spikes.iter() |
455 self.spikes.iter() |
456 } |
456 } |
457 } |
457 } |
458 |
458 |
459 // Need to manually implement serialisation for DeltaMeasure<Loc<F, N>, F> [`csv`] writer fails on |
|
460 // structs with nested arrays as well as with #[serde(flatten)]. |
|
461 // Then derive no longer works for DiscreteMeasure |
|
462 impl<F : Num, const N : usize> Serialize for DiscreteMeasure<Loc<F, N>, F> |
|
463 where |
|
464 F: Serialize, |
|
465 { |
|
466 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
|
467 where |
|
468 S: Serializer, |
|
469 { |
|
470 let mut s = serializer.serialize_seq(Some(self.spikes.len()))?; |
|
471 for δ in self.spikes.iter() { |
|
472 s.serialize_element(δ)?; |
|
473 } |
|
474 s.end() |
|
475 } |
|
476 } |
|
477 |
|
478 impl<Domain : PartialEq, F : Float> Measure<F> for DiscreteMeasure<Domain, F> { |
459 impl<Domain : PartialEq, F : Float> Measure<F> for DiscreteMeasure<Domain, F> { |
479 type Domain = Domain; |
460 type Domain = Domain; |
480 } |
461 } |
481 |
462 |
482 impl<Domain : PartialEq, F : Float> Norm<F, Radon> for DiscreteMeasure<Domain, F> |
463 impl<Domain : PartialEq, F : Float> Norm<F, Radon> for DiscreteMeasure<Domain, F> |