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

Renamed ArgminNorm to ArgminL2Norm #253

Merged
merged 1 commit into from
Aug 27, 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
6 changes: 3 additions & 3 deletions argmin-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,16 @@ pub trait ArgminScaledSub<T, U, V> {
fn scaled_sub(&self, factor: &U, vec: &T) -> V;
}

/// Compute the l2-norm (`U`) of `self`
/// Compute the l1-norm (`U`) of `self`
pub trait ArgminL1Norm<U> {
/// Compute the l1-norm (`U`) of `self`
fn l1_norm(&self) -> U;
}

/// Compute the l2-norm (`U`) of `self`
pub trait ArgminNorm<U> {
pub trait ArgminL2Norm<U> {
/// Compute the l2-norm (`U`) of `self`
fn norm(&self) -> U;
fn l2_norm(&self) -> U;
}

// Suboptimal: self is moved. ndarray however offers array views...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::ArgminNorm;
use crate::ArgminL2Norm;

use nalgebra::{
base::{dimension::Dim, storage::Storage},
Matrix, SimdComplexField,
};

impl<N, R, C, S> ArgminNorm<N::SimdRealField> for Matrix<N, R, C, S>
impl<N, R, C, S> ArgminL2Norm<N::SimdRealField> for Matrix<N, R, C, S>
where
N: SimdComplexField,
R: Dim,
C: Dim,
S: Storage<N, R, C>,
{
#[inline]
fn norm(&self) -> N::SimdRealField {
fn l2_norm(&self) -> N::SimdRealField {
self.norm()
}
}
Expand All @@ -37,7 +37,7 @@ mod tests {
#[test]
fn [<test_norm_ $t>]() {
let a = Vector2::new(4 as $t, 3 as $t);
let res = <Vector2<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Vector2<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand All @@ -51,7 +51,7 @@ mod tests {
#[test]
fn [<test_norm_signed_ $t>]() {
let a = Vector2::new(-4 as $t, -3 as $t);
let res = <Vector2<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Vector2<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/nalgebra_m/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mod dot;
mod eye;
mod inv;
mod l1norm;
mod l2norm;
mod mul;
mod norm;
mod scaledadd;
mod scaledsub;
mod sub;
Expand All @@ -27,8 +27,8 @@ pub use dot::*;
pub use eye::*;
pub use inv::*;
pub use l1norm::*;
pub use l2norm::*;
pub use mul::*;
pub use norm::*;
pub use scaledadd::*;
pub use scaledsub::*;
pub use sub::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::ArgminNorm;
use crate::ArgminL2Norm;
use ndarray::Array1;
use num_complex::Complex;
use num_integer::Roots;

macro_rules! make_norm_float {
($t:ty) => {
impl ArgminNorm<$t> for Array1<$t> {
impl ArgminL2Norm<$t> for Array1<$t> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
self.iter().map(|a| a.powi(2)).sum::<$t>().sqrt()
}
}
Expand All @@ -23,16 +23,16 @@ macro_rules! make_norm_float {

macro_rules! make_norm_complex_float {
($t:ty) => {
impl ArgminNorm<Complex<$t>> for Array1<Complex<$t>> {
impl ArgminL2Norm<Complex<$t>> for Array1<Complex<$t>> {
#[inline]
fn norm(&self) -> Complex<$t> {
fn l2_norm(&self) -> Complex<$t> {
self.iter().map(|a| a.powf(2.0)).sum::<Complex<$t>>().sqrt()
}
}

impl ArgminNorm<$t> for Array1<Complex<$t>> {
impl ArgminL2Norm<$t> for Array1<Complex<$t>> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
self.iter()
.map(|a| a.powf(2.0))
.sum::<Complex<$t>>()
Expand All @@ -45,9 +45,9 @@ macro_rules! make_norm_complex_float {

macro_rules! make_norm_integer {
($t:ty) => {
impl ArgminNorm<$t> for Array1<$t> {
impl ArgminL2Norm<$t> for Array1<$t> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
self.iter().map(|a| a.pow(2)).sum::<$t>().sqrt()
}
}
Expand Down Expand Up @@ -81,7 +81,7 @@ mod tests {
#[test]
fn [<test_norm_ $t>]() {
let a = array![4 as $t, 3 as $t];
let res = <Array1<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Array1<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand All @@ -95,7 +95,7 @@ mod tests {
#[test]
fn [<test_norm_signed_ $t>]() {
let a = array![-4 as $t, -3 as $t];
let res = <Array1<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Array1<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/ndarray_m/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ mod eye;
))]
mod inv;
mod l1norm;
mod l2norm;
mod minmax;
mod mul;
mod norm;
mod scaledadd;
mod scaledsub;
mod signum;
Expand All @@ -39,9 +39,9 @@ pub use eye::*;
))]
pub use inv::*;
pub use l1norm::*;
pub use l2norm::*;
pub use minmax::*;
pub use mul::*;
pub use norm::*;
pub use scaledadd::*;
pub use scaledsub::*;
pub use signum::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::ArgminNorm;
use crate::ArgminL2Norm;
use num_complex::Complex;

macro_rules! make_norm_unsigned {
($t:ty) => {
impl ArgminNorm<$t> for $t {
impl ArgminL2Norm<$t> for $t {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
*self
}
}
Expand All @@ -21,9 +21,9 @@ macro_rules! make_norm_unsigned {

macro_rules! make_norm {
($t:ty) => {
impl ArgminNorm<$t> for $t {
impl ArgminL2Norm<$t> for $t {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
(*self).abs()
}
}
Expand All @@ -32,9 +32,9 @@ macro_rules! make_norm {

macro_rules! make_norm_complex {
($t:ty) => {
impl ArgminNorm<$t> for Complex<$t> {
impl ArgminL2Norm<$t> for Complex<$t> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
(*self).re.hypot((*self).im)
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ mod tests {
#[test]
fn [<test_norm_ $t>]() {
let a = 8 as $t;
let res = <$t as ArgminNorm<$t>>::norm(&a);
let res = <$t as ArgminL2Norm<$t>>::l2_norm(&a);
assert!(((a - res) as f64).abs() < std::f64::EPSILON);
}
}
Expand All @@ -81,7 +81,7 @@ mod tests {
#[test]
fn [<test_norm_signed_ $t>]() {
let a = -8 as $t;
let res = <$t as ArgminNorm<$t>>::norm(&a);
let res = <$t as ArgminL2Norm<$t>>::l2_norm(&a);
assert!(((8 as $t - res) as f64).abs() < std::f64::EPSILON);
}
}
Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod conj;
mod div;
mod dot;
mod l1norm;
mod l2norm;
mod mul;
mod norm;
mod scaledadd;
mod scaledsub;
mod sub;
Expand All @@ -24,8 +24,8 @@ pub use conj::*;
pub use div::*;
pub use dot::*;
pub use l1norm::*;
pub use l2norm::*;
pub use mul::*;
pub use norm::*;
pub use scaledadd::*;
pub use scaledsub::*;
pub use sub::*;
Expand Down
18 changes: 9 additions & 9 deletions argmin-math/src/vec/norm.rs → argmin-math/src/vec/l2norm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::ArgminNorm;
use crate::ArgminL2Norm;
use num_complex::Complex;
use num_integer::Roots;

macro_rules! make_norm_float {
($t:ty) => {
impl ArgminNorm<$t> for Vec<$t> {
impl ArgminL2Norm<$t> for Vec<$t> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
self.iter().map(|a| a.powi(2)).sum::<$t>().sqrt()
}
}
Expand All @@ -22,9 +22,9 @@ macro_rules! make_norm_float {

macro_rules! make_norm_complex_float {
($t:ty) => {
impl ArgminNorm<Complex<$t>> for Vec<Complex<$t>> {
impl ArgminL2Norm<Complex<$t>> for Vec<Complex<$t>> {
#[inline]
fn norm(&self) -> Complex<$t> {
fn l2_norm(&self) -> Complex<$t> {
self.iter().map(|a| a.powf(2.0)).sum::<Complex<$t>>().sqrt()
}
}
Expand All @@ -33,9 +33,9 @@ macro_rules! make_norm_complex_float {

macro_rules! make_norm_integer {
($t:ty) => {
impl ArgminNorm<$t> for Vec<$t> {
impl ArgminL2Norm<$t> for Vec<$t> {
#[inline]
fn norm(&self) -> $t {
fn l2_norm(&self) -> $t {
self.iter().map(|a| a.pow(2)).sum::<$t>().sqrt()
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ mod tests {
#[test]
fn [<test_norm_ $t>]() {
let a = vec![4 as $t, 3 as $t];
let res = <Vec<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Vec<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand All @@ -82,7 +82,7 @@ mod tests {
#[test]
fn [<test_norm_signed_ $t>]() {
let a = vec![-4 as $t, -3 as $t];
let res = <Vec<$t> as ArgminNorm<$t>>::norm(&a);
let res = <Vec<$t> as ArgminL2Norm<$t>>::l2_norm(&a);
let target = 5 as $t;
assert!(((target - res) as f64).abs() < std::f64::EPSILON);
}
Expand Down
4 changes: 2 additions & 2 deletions argmin-math/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ mod div;
mod dot;
mod eye;
mod l1norm;
mod l2norm;
mod minmax;
mod mul;
mod norm;
mod random;
mod scaledadd;
mod scaledsub;
Expand All @@ -28,9 +28,9 @@ pub use div::*;
pub use dot::*;
pub use eye::*;
pub use l1norm::*;
pub use l2norm::*;
pub use minmax::*;
pub use mul::*;
pub use norm::*;
pub use random::*;
pub use scaledadd::*;
pub use scaledsub::*;
Expand Down
2 changes: 1 addition & 1 deletion argmin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ default = ["slog-logger", "serde1"]
wasm-bindgen = ["instant/wasm-bindgen", "getrandom/js"]
slog-logger = ["slog", "slog-term", "slog-async"]
serde1 = ["serde", "serde_json", "rand/serde1", "bincode", "slog-json", "rand_xoshiro/serde1"]
_ndarrayl = ["argmin-math/ndarray_latest-serde"]
_ndarrayl = ["argmin-math/ndarray_latest-serde", "argmin-math/_dev_linalg_latest"]
_nalgebral = ["argmin-math/nalgebra_latest-serde"]

[badges]
Expand Down
10 changes: 5 additions & 5 deletions argmin/src/solver/conjugategradient/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Springer. ISBN 0-387-30303-0.

use crate::core::{ArgminFloat, SerializeAlias};
use argmin_math::{ArgminDot, ArgminNorm, ArgminSub};
use argmin_math::{ArgminDot, ArgminL2Norm, ArgminSub};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -123,7 +123,7 @@ impl PolakRibiere {

impl<G, P, F> NLCGBetaUpdate<G, P, F> for PolakRibiere
where
G: ArgminDot<G, F> + ArgminSub<G, G> + ArgminNorm<F>,
G: ArgminDot<G, F> + ArgminSub<G, G> + ArgminL2Norm<F>,
F: ArgminFloat,
{
/// Update beta using the Polak-Ribiere method.
Expand All @@ -143,7 +143,7 @@ where
/// # assert_relative_eq!(beta, 14.0/5.0, epsilon = f64::EPSILON);
/// ```
fn update(&self, dfk: &G, dfk1: &G, _pk: &P) -> F {
let dfk_norm_sq = dfk.norm().powi(2);
let dfk_norm_sq = dfk.l2_norm().powi(2);
dfk1.dot(&dfk1.sub(dfk)) / dfk_norm_sq
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ impl PolakRibierePlus {

impl<G, P, F> NLCGBetaUpdate<G, P, F> for PolakRibierePlus
where
G: ArgminDot<G, F> + ArgminSub<G, G> + ArgminNorm<F>,
G: ArgminDot<G, F> + ArgminSub<G, G> + ArgminL2Norm<F>,
F: ArgminFloat,
{
/// Update beta using the Polak-Ribiere+ (PR+) method.
Expand All @@ -197,7 +197,7 @@ where
/// # assert_relative_eq!(beta, 0.0, epsilon = f64::EPSILON);
/// ```
fn update(&self, dfk: &G, dfk1: &G, _pk: &P) -> F {
let dfk_norm_sq = dfk.norm().powi(2);
let dfk_norm_sq = dfk.l2_norm().powi(2);
let beta = dfk1.dot(&dfk1.sub(dfk)) / dfk_norm_sq;
float!(0.0).max(beta)
}
Expand Down
Loading