use crate::dtype::DType;
use crate::scalar::{Scalar, Float};
#[cfg(feature = "half")]
use half::{bf16, f16};
#[cfg(feature = "complex")]
use num_complex::Complex;
impl Scalar for f64 {
#[cfg(feature = "half")]
fn from_bf16(t: bf16) -> Self {
t.into()
}
#[cfg(feature = "half")]
fn from_f16(t: f16) -> Self {
t.into()
}
fn from_f32(t: f32) -> Self {
t as f64
}
fn from_f64(t: f64) -> Self {
t
}
#[cfg(feature = "complex")]
fn from_cf32(t: Complex<f32>) -> Self {
t.re as f64
}
#[cfg(feature = "complex")]
fn from_cf64(t: Complex<f64>) -> Self {
t.re
}
fn from_u8(t: u8) -> Self {
t as f64
}
fn from_i8(t: i8) -> Self {
t as f64
}
fn from_i16(t: i16) -> Self {
t as f64
}
fn from_i32(t: i32) -> Self {
t as f64
}
fn from_i64(t: i64) -> Self {
t as f64
}
fn from_bool(t: bool) -> Self {
t as i64 as f64
}
fn from_le_bytes(bytes: &[u8]) -> Self {
f64::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
])
}
fn dtype() -> DType {
DType::F64
}
fn zero() -> Self {
0.
}
fn one() -> Self {
1.
}
fn byte_size() -> usize {
8
}
fn abs(self) -> Self {
self.abs()
}
fn reciprocal(self) -> Self {
1.0 / self
}
fn floor(self) -> Self {
self.floor()
}
fn neg(self) -> Self {
-self
}
fn relu(self) -> Self {
self.max(0.)
}
fn sin(self) -> Self {
self.sin()
}
fn cos(self) -> Self {
self.cos()
}
fn sqrt(self) -> Self {
return self.sqrt();
}
fn add(self, rhs: Self) -> Self {
self + rhs
}
fn sub(self, rhs: Self) -> Self {
self - rhs
}
fn mul(self, rhs: Self) -> Self {
self * rhs
}
fn div(self, rhs: Self) -> Self {
self / rhs
}
fn pow(self, rhs: Self) -> Self {
self.powf(rhs)
}
fn cmplt(self, rhs: Self) -> Self {
(self < rhs) as i32 as f64
}
fn max(self, rhs: Self) -> Self {
f64::max(self, rhs)
}
fn max_value() -> Self {
f64::MAX
}
fn min_value() -> Self {
f64::MIN
}
fn epsilon() -> Self {
0.00001
}
fn is_equal(self, rhs: Self) -> bool {
(self == -f64::INFINITY && rhs == -f64::INFINITY)
|| (self - rhs).abs() < Self::epsilon()
|| (self - rhs).abs() < self.abs() * 0.01
}
fn exp2(self) -> Self {
self.exp2()
}
fn log2(self) -> Self {
self.log2()
}
fn inv(self) -> Self {
1./self
}
fn not(self) -> Self {
if self != 0. { 0. } else { 1. }
}
fn nonzero(self) -> Self {
(self != 0.) as i32 as f64
}
fn cmpgt(self, rhs: Self) -> Self {
(self > rhs) as i32 as f64
}
fn or(self, rhs: Self) -> Self {
(self != 0. || rhs != 0.) as i32 as f64
}
}
impl Float for f64 {}