use numrs::array::Array;
use numrs::autograd::Tensor;
#[test]
fn test_sin_backward() {
let x = Tensor::new(Array::new(vec![2], vec![0.0, 1.570796]), true);
let y = x.sin().unwrap();
y.backward().unwrap();
let g = x.grad.unwrap();
let d = g.borrow().to_f32().data.clone();
assert!((d[0] - 1.0).abs() < 1e-4);
assert!(d[1].abs() < 1e-4);
}
#[test]
fn test_exp_log() {
let x = Tensor::new(Array::new(vec![1], vec![2.0]), true);
let y = x.exp().unwrap().log().unwrap();
assert!((y.item() - 2.0).abs() < 1e-5);
y.backward().unwrap();
let g = x.grad.unwrap();
assert!((g.borrow().to_f32().data[0] - 1.0).abs() < 1e-5);
}
#[test]
fn test_pow_sqrt() {
let x = Tensor::new(Array::new(vec![1], vec![4.0]), true);
let y = x.sqrt().unwrap();
let z = y.pow(3.0).unwrap();
assert!((z.item() - 8.0).abs() < 1e-5);
z.backward().unwrap();
let g = x.grad.unwrap();
assert!((g.borrow().to_f32().data[0] - 3.0).abs() < 1e-5);
}