Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum_dispatch for LnPriorTrait and LnPrior #6

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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` 0.3.9 (updated from 0.3.7) crate to implement `LnPriorTrait` for `LnPrior` https://github.com/light-curve/light-curve-feature/pull/6

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ anyhow = "<1.0.49"
conv = "^0.3.3"
emcee = "^0.3.0"
emcee_rand = { version = "^0.3.15", package = "rand" }
enum_dispatch = "^0.3.7"
enum_dispatch = "^0.3.9"
fftw = { version = "^0.7", default-features = false }
GSL = { version = "^6", default-features = false, optional = true }
itertools = "^0.10"
Expand Down
26 changes: 3 additions & 23 deletions 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