use crate::backend_ops::{BackendOps, ReductionOp, ReductionOps};
use coeus_core::{BackendError, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn sum<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
backend: &B,
) -> Result<T, B::Error> {
if a.numel() == 0 {
return Ok(T::zero());
}
let reshaped = if a.is_contiguous() && a.layout().offset() == 0 {
a.reshape([a.numel()])
} else {
let contiguous = a.to_contiguous_on(backend);
contiguous.reshape([a.numel()])
};
let reduced = sum_axis(&reshaped, 0, backend)?;
let mut host_scalar = [T::zero()];
backend.copy_to_host(reduced.storage(), &mut host_scalar);
Ok(host_scalar[0])
}
#[inline]
pub fn sum_axis<T: Scalar, B: ReductionOps<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: "sum_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::Sum,
a.storage(),
a.layout(),
axis,
out_storage,
out_layout,
)?;
Ok(out)
}
#[inline]
pub fn max_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: "max_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::Max,
a.storage(),
a.layout(),
axis,
out_storage,
out_layout,
)?;
Ok(out)
}
#[inline]
pub fn min_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: "min_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::Min,
a.storage(),
a.layout(),
axis,
out_storage,
out_layout,
)?;
Ok(out)
}
#[inline]
pub fn amax<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
backend: &B,
) -> Result<T, B::Error> {
if a.numel() == 0 {
return Err(B::Error::from(BackendError::Storage {
operation: "amax",
reason: "empty tensor has no maximum".to_owned(),
}));
}
let flat = if a.is_contiguous() && a.layout().offset() == 0 {
a.reshape([a.numel()])
} else {
a.to_contiguous_on(backend).reshape([a.numel()])
};
let reduced = max_axis(&flat, 0, backend)?;
let mut host = [T::zero()];
backend.copy_to_host(reduced.storage(), &mut host);
Ok(host[0])
}
#[inline]
pub fn amin<T: Scalar, B: BackendOps<T> + Default>(
a: &Tensor<T, B>,
backend: &B,
) -> Result<T, B::Error> {
if a.numel() == 0 {
return Err(B::Error::from(BackendError::Storage {
operation: "amin",
reason: "empty tensor has no minimum".to_owned(),
}));
}
let flat = if a.is_contiguous() && a.layout().offset() == 0 {
a.reshape([a.numel()])
} else {
a.to_contiguous_on(backend).reshape([a.numel()])
};
let reduced = min_axis(&flat, 0, backend)?;
let mut host = [T::zero()];
backend.copy_to_host(reduced.storage(), &mut host);
Ok(host[0])
}