use crate::backend_ops::{BackendOps, CrossOps};
use coeus_core::Scalar;
use coeus_tensor::Tensor;
#[inline]
pub fn dot<T: Scalar + leto_ops::Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
) -> Result<T, B::Error> {
let n = a.numel();
assert_eq!(n, b.numel(), "dot: numel mismatch: a={n}, b={}", b.numel());
if n == 0 {
return Ok(T::ZERO);
}
let backend = B::default();
let flatten = |x: &Tensor<T, B>| -> Result<Tensor<T, B>, B::Error> {
if x.is_contiguous() && x.layout().offset() == 0 {
Ok(x.reshape([n]))
} else {
Ok(x.to_contiguous_on(&backend)?.reshape([n]))
}
};
let a_flat = flatten(a)?;
let b_flat = flatten(b)?;
let product = crate::binary::mul(&a_flat, &b_flat, &backend)?;
super::sum(&product, &backend)
}
#[inline]
pub fn cross<T: Scalar, B: CrossOps<T> + Default>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
dim: usize,
) -> Result<Tensor<T, B>, B::Error> {
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 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 storage = backend.cross_storage(a_c.storage(), a_c.layout(), b_c.storage(), dim)?;
Ok(Tensor::from_raw_parts(
storage,
coeus_core::Layout::new(out_shape.into()),
))
}
#[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)
.expect("invariant: test backend operation succeeds")
}
fn t_2d(rows: usize, cols: usize, data: &[f32]) -> CoTensor<f32, B> {
CoTensor::<f32, B>::from_slice(vec![rows, cols], data)
.expect("invariant: test backend operation succeeds")
}
fn t_3d(d0: usize, d1: usize, d2: usize, data: &[f32]) -> CoTensor<f32, B> {
CoTensor::<f32, B>::from_slice(vec![d0, d1, d2], data)
.expect("invariant: test backend operation succeeds")
}
#[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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
assert_eq!(got, 0.0_f32);
}
#[test]
fn dot_empty_returns_zero() {
let a: CoTensor<f32, B> =
CoTensor::from_slice(vec![0], &[]).expect("invariant: test backend operation succeeds");
let b: CoTensor<f32, B> =
CoTensor::from_slice(vec![0], &[]).expect("invariant: test backend operation succeeds");
let got = dot::<f32, B>(&a, &b).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
}
#[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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
let ba = cross::<f32, B>(&b, &a, 0).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
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).expect("invariant: test operation succeeds");
}
#[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).expect("invariant: test operation succeeds");
}
}