use ariadnetor_tensor::{DenseTensorData, MemoryOrder};
use num_complex::Complex;
const EPSILON: f64 = 1e-10;
#[test]
fn test_norm_f64_simple() {
let mut tensor = DenseTensorData::<f64>::zeros_in_order(vec![3, 3], MemoryOrder::ColumnMajor);
tensor.set(&[0, 0], 1.0);
tensor.set(&[1, 1], 1.0);
tensor.set(&[2, 2], 1.0);
let norm = tensor.norm();
assert!((norm - 3.0f64.sqrt()).abs() < EPSILON);
}
#[test]
fn test_norm_f64_all_ones() {
let tensor = DenseTensorData::<f64>::ones_in_order(vec![2, 3], MemoryOrder::ColumnMajor);
let norm = tensor.norm();
assert!((norm - 6.0f64.sqrt()).abs() < EPSILON);
}
#[test]
fn test_norm_f32() {
let tensor = DenseTensorData::<f32>::ones_in_order(vec![2, 2], MemoryOrder::ColumnMajor);
let norm = tensor.norm();
assert!((norm - 4.0f32.sqrt()).abs() < 1e-6);
}
#[test]
fn test_norm_complex_f64() {
let data: Vec<Complex<f64>> = vec![
Complex::new(1.0, 0.0),
Complex::new(0.0, 1.0),
Complex::new(2.0, 0.0),
Complex::new(0.0, 2.0),
];
let tensor = DenseTensorData::from_raw_parts(data, vec![2, 2], MemoryOrder::ColumnMajor);
let norm = tensor.norm();
assert!((norm - 10.0f64.sqrt()).abs() < EPSILON);
}
#[test]
fn test_norm_zero_tensor() {
let tensor = DenseTensorData::<f64>::zeros_in_order(vec![3, 3], MemoryOrder::ColumnMajor);
let norm = tensor.norm();
assert!(norm.abs() < EPSILON);
}
#[test]
fn test_normalize_f64_inplace() {
let mut tensor = DenseTensorData::<f64>::ones_in_order(vec![2, 2], MemoryOrder::ColumnMajor);
let norm = tensor.normalize();
assert!((norm - 2.0).abs() < EPSILON);
{
let data = tensor.data();
for &val in data {
assert!((val - 0.5).abs() < EPSILON);
}
}
let new_norm = tensor.norm();
assert!((new_norm - 1.0).abs() < EPSILON);
}
#[test]
fn test_normalize_f64_out_of_place() {
let tensor = DenseTensorData::<f64>::filled_in_order(vec![2, 2], 3.0, MemoryOrder::ColumnMajor);
let (normalized, norm) = tensor.normalized();
assert!((norm - 6.0).abs() < EPSILON);
{
let data = tensor.data();
assert!((data[0] - 3.0).abs() < EPSILON);
}
{
let data = normalized.data();
for &val in data {
assert!((val - 0.5).abs() < EPSILON);
}
}
}
#[test]
fn test_normalize_f32() {
let mut tensor = DenseTensorData::<f32>::ones_in_order(vec![3, 3], MemoryOrder::ColumnMajor);
let norm = tensor.normalize();
assert!((norm - 3.0f32).abs() < 1e-6);
{
let data = tensor.data();
for &val in data {
assert!((val - 1.0 / 3.0).abs() < 1e-6);
}
}
}
#[test]
fn test_normalize_complex_f64() {
let data: Vec<Complex<f64>> = vec![Complex::new(2.0, 0.0), Complex::new(0.0, 2.0)];
let mut tensor = DenseTensorData::from_raw_parts(data, vec![2], MemoryOrder::ColumnMajor);
let norm = tensor.normalize();
assert!((norm - 8.0f64.sqrt()).abs() < EPSILON);
{
let data = tensor.data();
let expected = 1.0 / 2.0f64.sqrt();
assert!((data[0].re - expected).abs() < EPSILON);
assert!(data[0].im.abs() < EPSILON);
assert!(data[1].re.abs() < EPSILON);
assert!((data[1].im - expected).abs() < EPSILON);
}
let new_norm = tensor.norm();
assert!((new_norm - 1.0).abs() < EPSILON);
}
#[test]
#[should_panic(expected = "Cannot normalize zero tensor")]
fn test_normalize_zero_tensor_panic() {
let mut tensor = DenseTensorData::<f64>::zeros_in_order(vec![2, 2], MemoryOrder::ColumnMajor);
tensor.normalize();
}