use ndarray::Array2;
use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut};
pub use crate::solver::estimate::Dispersion;
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct PhiScaledCovariance(pub Array2<f64>);
impl PhiScaledCovariance {
#[inline]
pub fn wrap(cov: Array2<f64>) -> Self {
Self(cov)
}
#[inline]
pub fn as_array(&self) -> &Array2<f64> {
&self.0
}
#[inline]
pub fn as_array_mut(&mut self) -> &mut Array2<f64> {
&mut self.0
}
#[inline]
pub fn into_array(self) -> Array2<f64> {
self.0
}
}
impl From<Array2<f64>> for PhiScaledCovariance {
#[inline]
fn from(cov: Array2<f64>) -> Self {
Self(cov)
}
}
impl From<PhiScaledCovariance> for Array2<f64> {
#[inline]
fn from(cov: PhiScaledCovariance) -> Self {
cov.0
}
}
impl Deref for PhiScaledCovariance {
type Target = Array2<f64>;
#[inline]
fn deref(&self) -> &Array2<f64> {
&self.0
}
}
impl DerefMut for PhiScaledCovariance {
#[inline]
fn deref_mut(&mut self) -> &mut Array2<f64> {
&mut self.0
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct UnscaledPrecision(pub Array2<f64>);
impl UnscaledPrecision {
#[inline]
pub fn wrap(hessian: Array2<f64>) -> Self {
Self(hessian)
}
#[inline]
pub fn as_array(&self) -> &Array2<f64> {
&self.0
}
#[inline]
pub fn as_array_mut(&mut self) -> &mut Array2<f64> {
&mut self.0
}
#[inline]
pub fn into_array(self) -> Array2<f64> {
self.0
}
}
impl From<Array2<f64>> for UnscaledPrecision {
#[inline]
fn from(h: Array2<f64>) -> Self {
Self(h)
}
}
impl From<UnscaledPrecision> for Array2<f64> {
#[inline]
fn from(h: UnscaledPrecision) -> Self {
h.0
}
}
impl Deref for UnscaledPrecision {
type Target = Array2<f64>;
#[inline]
fn deref(&self) -> &Array2<f64> {
&self.0
}
}
impl DerefMut for UnscaledPrecision {
#[inline]
fn deref_mut(&mut self) -> &mut Array2<f64> {
&mut self.0
}
}
pub trait DispersionExt {
fn inv_phi(self) -> f64;
fn sqrt_phi(self) -> f64;
}
impl DispersionExt for Dispersion {
#[inline]
fn inv_phi(self) -> f64 {
1.0 / self.phi().max(1e-300)
}
#[inline]
fn sqrt_phi(self) -> f64 {
self.phi().max(0.0).sqrt()
}
}