Skip to content

Commit 30e68b7

Browse files
committed
Rename Vector -> Norm, Squared -> NormedField
1 parent a699e4c commit 30e68b7

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
pub use vector::Vector;
2+
pub use vector::Norm;
33
pub use matrix::Matrix;
44
pub use square::SquareMatrix;
55
pub use hermite::HermiteMatrix;

src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn all_close_max<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>,
3636
truth: &ArrayBase<S2, D>,
3737
atol: Tol)
3838
-> Result<Tol, Tol>
39-
where A: LinalgScalar + Squared<Output = Tol>,
39+
where A: LinalgScalar + NormedField<Output = Tol>,
4040
Tol: Float + Sum,
4141
S1: Data<Elem = A>,
4242
S2: Data<Elem = A>,
@@ -47,7 +47,7 @@ pub fn all_close_max<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>,
4747
}
4848

4949
pub fn all_close_l1<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2, D>, rtol: Tol) -> Result<Tol, Tol>
50-
where A: LinalgScalar + Squared<Output = Tol>,
50+
where A: LinalgScalar + NormedField<Output = Tol>,
5151
Tol: Float + Sum,
5252
S1: Data<Elem = A>,
5353
S2: Data<Elem = A>,
@@ -58,7 +58,7 @@ pub fn all_close_l1<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBas
5858
}
5959

6060
pub fn all_close_l2<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2, D>, rtol: Tol) -> Result<Tol, Tol>
61-
where A: LinalgScalar + Squared<Output = Tol>,
61+
where A: LinalgScalar + NormedField<Output = Tol>,
6262
Tol: Float + Sum,
6363
S1: Data<Elem = A>,
6464
S2: Data<Elem = A>,

src/vector.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,62 @@ use ndarray::{Array, NdFloat, Ix1, Ix2, LinalgScalar, ArrayBase, Data, Dimension
55
use num_traits::float::Float;
66
use super::impls::outer::ImplOuter;
77

8-
/// Methods for vectors
9-
pub trait Vector {
10-
type Scalar;
11-
/// rename of norm_l2
12-
fn norm(&self) -> Self::Scalar {
8+
/// Norms of ndarray
9+
pub trait Norm {
10+
type Output;
11+
/// rename of `norm_l2`
12+
fn norm(&self) -> Self::Output {
1313
self.norm_l2()
1414
}
1515
/// L-1 norm
16-
fn norm_l1(&self) -> Self::Scalar;
16+
fn norm_l1(&self) -> Self::Output;
1717
/// L-2 norm
18-
fn norm_l2(&self) -> Self::Scalar;
18+
fn norm_l2(&self) -> Self::Output;
1919
/// maximum norm
20-
fn norm_max(&self) -> Self::Scalar;
20+
fn norm_max(&self) -> Self::Output;
2121
}
2222

23-
impl<A, S, D, T> Vector for ArrayBase<S, D>
24-
where A: LinalgScalar + Squared<Output = T>,
23+
impl<A, S, D, T> Norm for ArrayBase<S, D>
24+
where A: LinalgScalar + NormedField<Output = T>,
2525
T: Float + Sum,
2626
S: Data<Elem = A>,
2727
D: Dimension
2828
{
29-
type Scalar = T;
30-
fn norm_l1(&self) -> Self::Scalar {
31-
self.iter().map(|x| x.sq_abs()).sum()
29+
type Output = T;
30+
fn norm_l1(&self) -> Self::Output {
31+
self.iter().map(|x| x.norm()).sum()
3232
}
33-
fn norm_l2(&self) -> Self::Scalar {
33+
fn norm_l2(&self) -> Self::Output {
3434
self.iter().map(|x| x.squared()).sum::<T>().sqrt()
3535
}
36-
fn norm_max(&self) -> Self::Scalar {
36+
fn norm_max(&self) -> Self::Output {
3737
self.iter().fold(T::zero(), |f, &val| {
38-
let v = val.sq_abs();
38+
let v = val.norm();
3939
if f > v { f } else { v }
4040
})
4141
}
4242
}
4343

44-
pub trait Squared {
45-
type Output;
44+
/// Field with norm
45+
pub trait NormedField {
46+
type Output: Float;
4647
fn squared(&self) -> Self::Output;
47-
fn sq_abs(&self) -> Self::Output;
48+
fn norm(&self) -> Self::Output {
49+
self.squared().sqrt()
50+
}
4851
}
4952

50-
impl<A: Float> Squared for A {
53+
impl<A: Float> NormedField for A {
5154
type Output = A;
5255
fn squared(&self) -> A {
5356
*self * *self
5457
}
55-
fn sq_abs(&self) -> A {
58+
fn norm(&self) -> A {
5659
self.abs()
5760
}
5861
}
5962

63+
/// Outer product
6064
pub fn outer<A, S1, S2>(a: &ArrayBase<S1, Ix1>, b: &ArrayBase<S2, Ix1>) -> Array<A, Ix2>
6165
where A: NdFloat + ImplOuter,
6266
S1: Data<Elem = A>,

0 commit comments

Comments
 (0)