Skip to content

Commit

Permalink
enum_dispatch for LnPriorTrait and LnPrior
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed Dec 10, 2021
1 parent e74b797 commit adfbe63
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 25 deletions.
2 changes: 1 addition & 1 deletion light-curve-feature/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Use `enum_dispatch` crate to implement `LnPriorTrait` for `LnPrior`

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion light-curve-feature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ conv = "^0.3.3"
dyn-clonable = "^0.9.0"
emcee = "^0.3.0"
emcee_rand = { version = "^0.3.15", package = "rand" }
enum_dispatch = "^0.3.7"
enum_dispatch = { git = "https://gitlab.com/antonok/enum_dispatch.git", rev = "b88e87870b80aab51ff65da194a16ce514387060" }
fftw = { version = "0.7.0", default-features = false }
GSL = { version = "~6.0.0", default-features = false, optional = true }
itertools = "^0.10.0"
Expand Down
26 changes: 3 additions & 23 deletions light-curve-feature/src/nl_fit/prior/ln_prior.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use crate::nl_fit::prior::ln_prior_1d::{LnPrior1D, LnPrior1DTrait};

use enum_dispatch::enum_dispatch;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

#[enum_dispatch]
pub trait LnPriorTrait<const NPARAMS: usize>: Clone + Debug + Serialize + DeserializeOwned {
fn ln_prior(&self, params: &[f64; NPARAMS]) -> f64;
}

/// Natural logarithm of prior for non-linear curve-fit problem
#[enum_dispatch(LnPriorTrait<NPARAMS>)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub enum LnPrior<const NPARAMS: usize> {
Expand Down Expand Up @@ -55,29 +58,6 @@ impl<const NPARAMS: usize> LnPrior<NPARAMS> {
}
}

// Looks like enum_dispatch doesn't work with const generics yet
// https://gitlab.com/antonok/enum_dispatch/-/issues/51
impl<const NPARAMS: usize> LnPriorTrait<NPARAMS> for LnPrior<NPARAMS> {
fn ln_prior(&self, params: &[f64; NPARAMS]) -> f64 {
match self {
Self::None(x) => x.ln_prior(params),
Self::IndComponents(x) => x.ln_prior(params),
}
}
}

impl<const NPARAMS: usize> From<NoneLnPrior> for LnPrior<NPARAMS> {
fn from(item: NoneLnPrior) -> Self {
Self::None(item)
}
}

impl<const NPARAMS: usize> From<IndComponentsLnPrior<NPARAMS>> for LnPrior<NPARAMS> {
fn from(item: IndComponentsLnPrior<NPARAMS>) -> Self {
Self::IndComponents(item)
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct NoneLnPrior {}

Expand Down

0 comments on commit adfbe63

Please sign in to comment.