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
28 + Copy
29 + Zero
30 + One
31 + Add<Output = Self>
32 + Sub<Output = Self>
33 + Mul<Output = Self>
34 + Div<Output = Self>
35{
36}
37
38impl<T> LinalgScalar for T where
39 T: 'static
40 + Copy
41 + Zero
42 + One
43 + Add<Output = T>
44 + Sub<Output = T>
45 + Mul<Output = T>
46 + Div<Output = T>
47{
48}
49
50/// Floating-point element types `f32` and `f64`.
51///
52/// Trait `NdFloat` is only implemented for `f32` and `f64` but encompasses as
53/// much float-relevant ndarray functionality as possible, including the traits
54/// needed for linear algebra and for *right hand side* scalar
55/// operations (`ScalarOperand`).
56///
57/// This trait can only be implemented by `f32` and `f64`.
58#[cfg(feature = "std")]
59pub trait NdFloat:
60 Float
61 + AddAssign
62 + SubAssign
63 + MulAssign
64 + DivAssign
65 + RemAssign
66 + fmt::Display
67 + fmt::Debug
68 + fmt::LowerExp
69 + fmt::UpperExp
70 + ScalarOperand
71 + LinalgScalar
72 + Send
73 + Sync
74{
75}
76
77#[cfg(feature = "std")]
78impl NdFloat for f32 {}
79#[cfg(feature = "std")]
80impl NdFloat for f64 {}
81