kn0sys_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///
43/// **Requires default crate feature `"std"`**
44#[cfg(feature = "std")]
45#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
46pub trait NdFloat:
47    Float
48    + AddAssign
49    + SubAssign
50    + MulAssign
51    + DivAssign
52    + RemAssign
53    + fmt::Display
54    + fmt::Debug
55    + fmt::LowerExp
56    + fmt::UpperExp
57    + ScalarOperand
58    + LinalgScalar
59    + Send
60    + Sync
61{
62}
63
64#[cfg(feature = "std")]
65#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
66impl NdFloat for f32 {}
67#[cfg(feature = "std")]
68#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
69impl NdFloat for f64 {}