use scirs2_core::ndarray::{Array1, Array2};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sklears_core::{error::Result, types::Float};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CDConfig {
pub max_iter: usize,
pub tol: Float,
pub alpha: Float,
}
impl Default for CDConfig {
fn default() -> Self {
Self {
max_iter: 1000,
tol: 1e-4,
alpha: 1.0,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CDResult {
pub coefficients: Array1<Float>,
pub objective: Float,
pub n_iter: usize,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SoftThresholding {
pub threshold: Float,
}
impl SoftThresholding {
pub fn new(threshold: Float) -> Self {
Self { threshold }
}
pub fn apply(&self, x: Float) -> Float {
if x > self.threshold {
x - self.threshold
} else if x < -self.threshold {
x + self.threshold
} else {
0.0
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CDEncoder {
config: CDConfig,
}
impl CDEncoder {
pub fn new(config: CDConfig) -> Self {
Self { config }
}
pub fn encode(&self, dictionary: &Array2<Float>, _signal: &Array1<Float>) -> Result<CDResult> {
let n_atoms = dictionary.nrows();
let coefficients = Array1::zeros(n_atoms);
let objective = 0.0;
Ok(CDResult {
coefficients,
objective,
n_iter: 0,
})
}
}