ndarray/linalg_traits.rs
1// Copyright 2014-2016 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[cfg(feature = "std")]
10use num_traits::Float;
11use num_traits::{One, Zero};
12
13#[cfg(feature = "std")]
14use std::fmt;
15use std::ops::{Add, Div, Mul, Sub};
16#[cfg(feature = "std")]
17use std::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
18
19#[cfg(feature = "std")]
20use crate::ScalarOperand;
21
22/// Elements that support linear algebra operations.
23///
24/// `'static` for type-based specialization, `Copy` so that they don't need move
25/// semantics or destructors, and the rest are numerical traits.
26pub trait LinalgScalar:
27 'static + Copy + Zero + One + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
28{
29}
30
31impl<T> LinalgScalar for T where T: 'static + Copy + Zero + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>
32{}
33
34/// Floating-point element types `f32` and `f64`.
35///
36/// Trait `NdFloat` is only implemented for `f32` and `f64` but encompasses as
37/// much float-relevant ndarray functionality as possible, including the traits
38/// needed for linear algebra and for *right hand side* scalar
39/// operations (`ScalarOperand`).
40///
41/// This trait can only be implemented by `f32` and `f64`.
42#[cfg(feature = "std")]
43pub trait NdFloat:
44 Float
45 + AddAssign
46 + SubAssign
47 + MulAssign
48 + DivAssign
49 + RemAssign
50 + fmt::Display
51 + fmt::Debug
52 + fmt::LowerExp
53 + fmt::UpperExp
54 + ScalarOperand
55 + LinalgScalar
56 + Send
57 + Sync
58{
59}
60
61#[cfg(feature = "std")]
62impl NdFloat for f32 {}
63#[cfg(feature = "std")]
64impl NdFloat for f64 {}