diff --git a/examples/linalg.rs b/examples/linalg.rs index 41fe55048..e0cb2f452 100644 --- a/examples/linalg.rs +++ b/examples/linalg.rs @@ -13,6 +13,7 @@ use std::ops::{Add, Sub, Mul, Div}; use ndarray::{RcArray, Ix}; use ndarray::{rcarr1, rcarr2}; +use ndarray::LinalgScalar; /// Column vector. pub type Col = RcArray; @@ -29,7 +30,7 @@ pub trait Field : Ring + Div { } impl> Field for A { } /// A real or complex number. -pub trait ComplexField : Copy + Field +pub trait ComplexField : LinalgScalar { #[inline] fn conjugate(self) -> Self { self } @@ -50,7 +51,7 @@ impl ComplexField for f64 fn sqrt_real(self) -> f64 { self.sqrt() } } -impl ComplexField for Complex +impl ComplexField for Complex { #[inline] fn conjugate(self) -> Complex { self.conj() } diff --git a/src/lib.rs b/src/lib.rs index a69bb9d4d..bde34026b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,10 +97,8 @@ pub use iterators::{ AxisChunksIterMut, }; -#[allow(deprecated)] -use linalg::{Field, Ring}; +pub use linalg::LinalgScalar; -pub mod linalg; mod arraytraits; #[cfg(feature = "serde")] mod arrayserialize; @@ -110,6 +108,7 @@ pub mod blas; mod dimension; mod indexes; mod iterators; +mod linalg; mod linspace; mod numeric_util; mod si; @@ -2252,7 +2251,7 @@ impl ArrayBase /// **Panics** if `axis` is out of bounds. #[allow(deprecated)] pub fn mean(&self, axis: usize) -> OwnedArray::Smaller> - where A: Copy + Field, + where A: LinalgScalar, D: RemoveAxis, { let n = self.shape()[axis]; @@ -2289,7 +2288,7 @@ impl ArrayBase /// **Panics** if the arrays are not of the same length. pub fn dot(&self, rhs: &ArrayBase) -> A where S2: Data, - A: Clone + Add + Mul + libnum::Zero, + A: LinalgScalar, { assert_eq!(self.len(), rhs.len()); if let Some(self_s) = self.as_slice() { @@ -2369,7 +2368,7 @@ impl ArrayBase /// #[allow(deprecated)] pub fn mat_mul(&self, rhs: &ArrayBase) -> OwnedArray - where A: Copy + Ring + where A: LinalgScalar, { // NOTE: Matrix multiplication only defined for Copy types to // avoid trouble with panicking + and *, and destructors @@ -2414,7 +2413,7 @@ impl ArrayBase /// **Panics** if shapes are incompatible. #[allow(deprecated)] pub fn mat_mul_col(&self, rhs: &ArrayBase) -> OwnedArray - where A: Copy + Ring + where A: LinalgScalar, { let ((m, a), n) = (self.dim, rhs.dim); let (self_columns, other_rows) = (a, n); @@ -2609,7 +2608,7 @@ impl<'a, A, S, S2, D, E> $trt<&'a ArrayBase> for &'a ArrayBase fn $mth (self, rhs: &'a ArrayBase) -> OwnedArray { // FIXME: Can we co-broadcast arrays here? And how? - self.to_owned().$mth(rhs.view()) + self.to_owned().$mth(rhs) } } diff --git a/src/linalg.rs b/src/linalg.rs index 03cc618ba..a183f833a 100644 --- a/src/linalg.rs +++ b/src/linalg.rs @@ -1,18 +1,28 @@ -#![allow(non_snake_case, deprecated)] -#![cfg_attr(has_deprecated, deprecated(note="`linalg` is not in good shape."))] - -//! ***Deprecated: linalg is not in good shape.*** -//! -//! A few linear algebra operations on two-dimensional arrays. - -use libnum::{Zero, One}; +use libnum::{Zero, One, Float}; use std::ops::{Add, Sub, Mul, Div}; +use std::any::Any; -/// Trait union for a ring with 1. -pub trait Ring : Clone + Zero + Add + Sub - + One + Mul { } -impl + Sub + One + Mul> Ring for A { } +/// Trait union for scalars (array elements) that support linear algebra operations. +/// +/// `Any` for type-based specialization, `Copy` so that they don't need move +/// semantics or destructors, and the rest are numerical traits. +pub trait LinalgScalar : + Any + + Copy + + Zero + One + + Add + + Sub + + Mul + + Div +{ } -/// Trait union for a field. -pub trait Field : Ring + Div { } -impl> Field for A {} +impl LinalgScalar for T + where T: + Any + + Copy + + Zero + One + + Add + + Sub + + Mul + + Div +{ }