mod cpu_kernel;
#[cfg(feature = "cuda")]
mod cuda_kernel;
use super::ops::{try_unary_op, UnaryKernel};
use crate::{shapes::*, tensor::*};
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct SinKernelOp;
pub fn sin<S: Shape, E: Dtype, D: UnaryKernel<SinKernelOp, E>, T: Tape<E, D>>(
t: Tensor<S, E, D, T>,
) -> Tensor<S, E, D, T> {
t.sin()
}
impl<S: Shape, E: Dtype, D: UnaryKernel<SinKernelOp, E>, T: Tape<E, D>> Tensor<S, E, D, T> {
pub fn sin(self) -> Self {
self.try_sin().unwrap()
}
pub fn try_sin(self) -> Result<Self, D::Err> {
try_unary_op(SinKernelOp, self)
}
}
#[cfg(test)]
mod tests {
use crate::tests::*;
use crate::{tensor::*, tensor_ops::*};
#[test]
fn test_sin() {
let dev: TestDevice = Default::default();
let x = dev
.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
.to_dtype::<TestDtype>();
let r = x.leaky_trace().sin();
assert_close_to_literal!(r, [-0.9092974, -0.84147096, 0.0, 0.84147096, 0.9092974]);
let g = r.mean().backward();
assert_close_to_literal!(
g.get(&x),
[-0.08322937, 0.10806046, 0.2, 0.10806046, -0.08322937]
);
}
}