use crate::backend_ops::BackendOps;
use coeus_core::Scalar;
use coeus_tensor::Tensor;
#[inline]
#[must_use]
pub fn dot<T: Scalar, B: BackendOps<T> + Default>(a: &Tensor<T, B>, b: &Tensor<T, B>) -> T {
assert_eq!(
a.numel(),
b.numel(),
"dot: numel mismatch: a={}, b={}",
a.numel(),
b.numel()
);
if a.numel() == 0 {
return T::zero();
}
let backend = B::default();
let a_c = a.to_contiguous_on(&backend);
let b_c = b.to_contiguous_on(&backend);
let mut a_host = vec![T::zero(); a_c.numel()];
let mut b_host = vec![T::zero(); b_c.numel()];
backend.copy_to_host(a_c.storage(), &mut a_host);
backend.copy_to_host(b_c.storage(), &mut b_host);
let mut acc = T::zero();
for (&ai, &bi) in a_host.iter().zip(b_host.iter()) {
acc += ai * bi;
}
acc
}
#[inline]
#[must_use]
pub fn cross<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
dim: usize,
) -> Tensor<T, B> {
let shape = a.shape();
assert_eq!(
shape,
b.shape(),
"cross: shape mismatch: a={:?}, b={:?}",
shape,
b.shape()
);
assert!(
dim < shape.len(),
"cross: dim {dim} out of bounds for {}-D shape {:?}",
shape.len(),
shape
);
assert_eq!(
shape[dim], 3,
"cross: dim {dim} must have size 3 (got {})",
shape[dim]
);
let pre: usize = shape[..dim].iter().product();
let post: usize = shape[dim + 1..].iter().product();
let stride_pre = 3 * post;
let stride_k = post;
let mut out_shape: Vec<usize> = shape.to_vec();
out_shape[dim] = 3;
let backend = B::default();
let a_c = a.to_contiguous_on(&backend);
let b_c = b.to_contiguous_on(&backend);
let mut a_host = vec![T::zero(); a_c.numel()];
let mut b_host = vec![T::zero(); b_c.numel()];
backend.copy_to_host(a_c.storage(), &mut a_host);
backend.copy_to_host(b_c.storage(), &mut b_host);
let mut out_host = vec![T::zero(); a_c.numel()];
for pre_idx in 0..pre {
for post_idx in 0..post {
let base = pre_idx * stride_pre + post_idx;
let ax = base;
let ay = base + stride_k;
let az = base + 2 * stride_k;
let bx = ax;
let by = ay;
let bz = az;
out_host[ax] = a_host[ay] * b_host[bz] - a_host[az] * b_host[by];
out_host[ay] = a_host[az] * b_host[bx] - a_host[ax] * b_host[bz];
out_host[az] = a_host[ax] * b_host[by] - a_host[ay] * b_host[bx];
}
}
Tensor::from_slice(out_shape, &out_host)
}
#[cfg(test)]
mod tests {
use super::*;
use coeus_core::SequentialBackend;
use coeus_tensor::Tensor as CoTensor;
type B = SequentialBackend;
fn t_1d(data: &[f32]) -> CoTensor<f32, B> {
CoTensor::<f32, B>::from_slice(vec![data.len()], data)
}
fn t_2d(rows: usize, cols: usize, data: &[f32]) -> CoTensor<f32, B> {
CoTensor::<f32, B>::from_slice(vec![rows, cols], data)
}
fn t_3d(d0: usize, d1: usize, d2: usize, data: &[f32]) -> CoTensor<f32, B> {
CoTensor::<f32, B>::from_slice(vec![d0, d1, d2], data)
}
#[test]
fn dot_1d_matches_sum_pairwise_product() {
let a = t_1d(&[1.0_f32, 2.0, 3.0]);
let b = t_1d(&[4.0_f32, 5.0, 6.0]);
let got = dot::<f32, B>(&a, &b);
assert_eq!(got, 32.0_f32);
}
#[test]
fn dot_flattens_2d_inputs() {
let a = t_2d(2, 3, &[1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0]);
let b = t_2d(2, 3, &[7.0_f32, 8.0, 9.0, 10.0, 11.0, 12.0]);
let got = dot::<f32, B>(&a, &b);
assert_eq!(got, 217.0_f32);
}
#[test]
fn dot_orthogonal_vectors_is_zero() {
let a = t_1d(&[1.0_f32, 0.0, 0.0]);
let b = t_1d(&[0.0_f32, 1.0, 0.0]);
let got = dot::<f32, B>(&a, &b);
assert_eq!(got, 0.0_f32);
}
#[test]
fn dot_empty_returns_zero() {
let a: CoTensor<f32, B> = CoTensor::from_slice(vec![0], &[]);
let b: CoTensor<f32, B> = CoTensor::from_slice(vec![0], &[]);
let got = dot::<f32, B>(&a, &b);
assert_eq!(got, 0.0_f32);
}
#[test]
#[should_panic(expected = "numel mismatch")]
fn dot_numel_mismatch_panics() {
let a = t_1d(&[1.0_f32, 2.0]);
let b = t_1d(&[1.0_f32, 2.0, 3.0]);
let _ = dot::<f32, B>(&a, &b);
}
#[test]
fn cross_3d_basis_vectors_along_last_axis() {
let a = t_1d(&[1.0_f32, 0.0, 0.0]);
let b = t_1d(&[0.0_f32, 1.0, 0.0]);
let out = cross::<f32, B>(&a, &b, 0);
assert_eq!(out.as_slice(), &[0.0_f32, 0.0, 1.0]);
}
#[test]
fn cross_3d_basis_y_x_is_minus_z() {
let a = t_1d(&[0.0_f32, 1.0, 0.0]);
let b = t_1d(&[1.0_f32, 0.0, 0.0]);
let out = cross::<f32, B>(&a, &b, 0);
assert_eq!(out.as_slice(), &[0.0_f32, 0.0, -1.0]);
}
#[test]
fn cross_anticommutative_flips_sign() {
let a = t_1d(&[2.0_f32, 3.0, 4.0]);
let b = t_1d(&[5.0_f32, 6.0, 7.0]);
let ab = cross::<f32, B>(&a, &b, 0);
let ba = cross::<f32, B>(&b, &a, 0);
let ab_s = ab.as_slice();
let ba_s = ba.as_slice();
for i in 0..3 {
assert_eq!(ab_s[i], -ba_s[i]);
}
}
#[test]
fn cross_2d_per_row_dim_last() {
let a = t_2d(2, 3, &[1.0_f32, 0.0, 0.0, 0.0, 1.0, 0.0]);
let b = t_2d(2, 3, &[0.0_f32, 1.0, 0.0, 0.0, 0.0, 1.0]);
let out = cross::<f32, B>(&a, &b, 1);
assert_eq!(out.shape(), &[2, 3]);
assert_eq!(out.as_slice(), &[0.0_f32, 0.0, 1.0, 1.0, 0.0, 0.0]);
}
#[test]
fn cross_2d_per_col_dim_first() {
let a = t_2d(3, 3, &[1.0_f32, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 4.0]);
let b = t_2d(3, 3, &[0.0_f32, 0.0, 5.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0]);
let out = cross::<f32, B>(&a, &b, 0);
assert_eq!(out.shape(), &[3, 3]);
assert_eq!(
out.as_slice(),
&[0.0_f32, 0.0, 0.0, -5.0, 0.0, 20.0, 0.0, 0.0, 0.0]
);
}
#[test]
fn cross_3d_along_middle_axis() {
let a_data = vec![2.0_f32, 0.0, 0.0, 0.0, 0.0, 4.0];
let b_data = vec![0.0_f32, 3.0, 0.0, 5.0, 0.0, 0.0];
let a = t_3d(2, 3, 1, &a_data);
let b = t_3d(2, 3, 1, &b_data);
let out = cross::<f32, B>(&a, &b, 1);
assert_eq!(out.shape(), &[2, 3, 1]);
assert_eq!(out.as_slice(), &[0.0_f32, 0.0, 6.0, 0.0, 20.0, 0.0]);
}
#[test]
fn cross_parallel_vectors_is_zero() {
let a = t_1d(&[2.0_f32, 3.0, 4.0]);
let out = cross::<f32, B>(&a, &a, 0);
assert_eq!(out.as_slice(), &[0.0_f32, 0.0, 0.0]);
}
#[test]
#[should_panic(expected = "size 3")]
fn cross_wrong_axis_size_panics() {
let a = t_1d(&[1.0_f32, 2.0, 3.0, 4.0]);
let b = t_1d(&[5.0_f32, 6.0, 7.0, 8.0]);
let _ = cross::<f32, B>(&a, &b, 0);
}
#[test]
#[should_panic(expected = "out of bounds")]
fn cross_axis_out_of_bounds_panics() {
let a = t_1d(&[1.0_f32, 2.0, 3.0]);
let b = t_1d(&[4.0_f32, 5.0, 6.0]);
let _ = cross::<f32, B>(&a, &b, 5);
}
}