use crate::backend_ops::{BackendOps, ReductionOp};
use coeus_core::{BackendError, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn prod_axis<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
axis: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
if axis >= a.ndim() {
return Err(B::Error::from(BackendError::AxisOutOfRange {
operation: "prod_axis",
axis,
rank: a.ndim(),
}));
}
let mut out_shape = a.shape_cloned();
out_shape[axis] = 1;
let mut out = Tensor::alloc_on(out_shape, backend);
let (out_storage, out_layout) = out.storage_mut_and_layout();
backend.reduce(
ReductionOp::Prod,
a.storage(),
a.layout(),
axis,
out_storage,
out_layout,
)?;
Ok(out)
}
pub fn prod_tensor<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
let mut reduced = if a.ndim() == 0 {
a.reshape([1])
} else {
a.clone()
};
for axis in 0..a.ndim() {
reduced = prod_axis(&reduced, axis, backend)?;
}
Ok(reduced.reshape([1]))
}
#[inline]
pub fn prod<T: Scalar, B: BackendOps<T> + Default>(a: &Tensor<T, B>, backend: &B) -> T {
let reduced = prod_tensor(a, backend).expect("prod: provider reduction failed");
let mut scalar = [T::zero()];
backend.copy_to_host(reduced.storage(), &mut scalar);
scalar[0]
}