Skip to main content

oxilean_std/convex_optimization/
l1normfunction_traits.rs

1//! # L1NormFunction - Trait Implementations
2//!
3//! This module contains trait implementations for `L1NormFunction`.
4//!
5//! ## Implemented Traits
6//!
7//! - `ConvexFunction`
8//! - `ProxableFunction`
9//!
10//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
11
12use super::functions::{ConvexFunction, ProxableFunction};
13use super::types::L1NormFunction;
14
15impl ConvexFunction for L1NormFunction {
16    fn eval(&self, x: &[f64]) -> f64 {
17        self.lambda * x.iter().map(|xi| xi.abs()).sum::<f64>()
18    }
19    /// Subgradient: sign(x_i) · λ (0 at x_i = 0 is replaced by 0.0).
20    fn gradient(&self, x: &[f64]) -> Vec<f64> {
21        x.iter().map(|xi| self.lambda * xi.signum()).collect()
22    }
23    fn is_strongly_convex(&self) -> bool {
24        false
25    }
26}
27
28impl ProxableFunction for L1NormFunction {
29    /// Soft thresholding: prox_{t·λ‖·‖₁}(v)_i = sign(v_i)·max(|v_i| - t·λ, 0).
30    fn prox(&self, v: &[f64], t: f64) -> Vec<f64> {
31        let threshold = t * self.lambda;
32        v.iter()
33            .map(|vi| vi.signum() * (vi.abs() - threshold).max(0.0))
34            .collect()
35    }
36}