coeus-ops 0.10.0

Numerical tensor operators for Coeus.
Documentation
// ── Unary math ops ──

use super::kernel::{elementwise_unary, elementwise_unary_assign};
use crate::backend_ops::{BackendOps, ElementwiseOps, UnaryOp};
use coeus_core::{Float, Scalar};
use coeus_tensor::Tensor;

/// Element-wise sine: sin(x).
///
/// Backward: `d/dx sin(x) = cos(x)`.
#[inline]
pub fn sin<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Sin).expect("sin")
}
/// Element-wise cosine: cos(x).
///
/// Backward: `d/dx cos(x) = -sin(x)`.
#[inline]
pub fn cos<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Cos).expect("cos")
}

/// Element-wise exponential.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::exp;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([2], &[0.0, 1.0]);
/// let b = exp(&a, &backend);
/// let s = b.as_slice();
/// assert!((s[0] - 1.0).abs() < 1e-5);
/// assert!((s[1] - std::f32::consts::E).abs() < 1e-5);
/// ```
#[inline]
pub fn exp<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Exp).expect("exp")
}

/// Element-wise natural log.
#[inline]
pub fn log<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Log).expect("log")
}

/// Element-wise Gauss error function: erf(x) = (2/√π) ∫₀ˣ e^(−t²) dt.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::erf;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f64, SequentialBackend>::from_slice([2], &[0.0, 1.0]);
/// let b = erf(&a, &backend);
/// let s = b.as_slice();
/// assert!(s[0].abs() < 1e-15);
/// assert!((s[1] - 0.842_700_792_949_714_9).abs() < 1e-12);
/// ```
#[inline]
pub fn erf<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Erf).expect("erf")
}

/// Element-wise complementary error function: erfc(x) = 1 − erf(x).
///
/// Backward: `d/dx erfc(x) = −d/dx erf(x) = −(2/√π)·e^(−x²)`.
#[inline]
pub fn erfc<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Erfc).expect("erfc")
}

/// Element-wise natural logarithm of the absolute gamma function: `ln|Γ(x)|`.
///
/// Autograd for this operation requires `digamma(x)`, which is not exposed by
/// the current datatype-law provider.
#[inline]
pub fn lgamma<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Lgamma).expect("lgamma")
}

/// Element-wise tangent: tan(x).
///
/// Backward: `d/dx tan(x) = 1 / cos²(x)` (sec²(x)).
#[inline]
pub fn tan<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Tan).expect("tan")
}

/// Element-wise arc-sine: asin(x), domain x ∈ [-1, 1].
///
/// Backward: `d/dx asin(x) = 1 / sqrt(1 - x²)`.
#[inline]
pub fn asin<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Asin).expect("asin")
}

/// Element-wise arc-cosine: acos(x), domain x ∈ [-1, 1].
///
/// Backward: `d/dx acos(x) = -1 / sqrt(1 - x²)`.
#[inline]
pub fn acos<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Acos).expect("acos")
}

/// Element-wise arc-tangent: atan(x).
///
/// Backward: `d/dx atan(x) = 1 / (1 + x²)`.
#[inline]
pub fn atan<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Atan).expect("atan")
}

/// Element-wise hyperbolic sine: sinh(x).
/// Backward: d/dx sinh(x) = cosh(x).
#[inline]
pub fn sinh<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Sinh).expect("sinh")
}
/// Element-wise hyperbolic cosine: cosh(x).
/// Backward: d/dx cosh(x) = sinh(x).
#[inline]
pub fn cosh<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Cosh).expect("cosh")
}
/// Element-wise base-2 logarithm: log2(x).
/// Backward: d/dx log2(x) = 1/(x * ln(2)).
#[inline]
pub fn log2<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Log2).expect("log2")
}
/// Element-wise base-10 logarithm: log10(x).
/// Backward: d/dx log10(x) = 1/(x * ln(10)).
#[inline]
pub fn log10<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Log10).expect("log10")
}
/// Element-wise base-2 exponential: 2^x.
/// Backward: d/dx 2^x = 2^x * ln(2).
#[inline]
pub fn exp2<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Exp2).expect("exp2")
}
/// Element-wise inverse hyperbolic tangent: atanh(x), domain |x| < 1.
/// Backward: d/dx atanh(x) = 1/(1 - x²).
#[inline]
pub fn atanh<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Atanh).expect("atanh")
}
/// Element-wise inverse hyperbolic sine: asinh(x).
/// Backward: d/dx asinh(x) = 1/sqrt(x² + 1).
#[inline]
pub fn asinh<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Asinh).expect("asinh")
}
/// Element-wise inverse hyperbolic cosine: acosh(x), domain x > 1.
/// Backward: d/dx acosh(x) = 1/sqrt(x² - 1).
#[inline]
pub fn acosh<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Acosh).expect("acosh")
}
/// Element-wise exp(x) - 1.
/// Backward: d/dx expm1(x) = exp(x).
#[inline]
pub fn expm1<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Expm1).expect("expm1")
}
/// Element-wise ln(1 + x).
/// Backward: d/dx log1p(x) = 1/(1 + x).
#[inline]
pub fn log1p<T: Float, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Log1p).expect("log1p")
}

/// Element-wise negation (works for any Scalar).
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::neg;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([3], &[1.0, -2.0, 3.0]);
/// let b = neg(&a, &backend);
/// assert_eq!(b.as_slice(), &[-1.0, 2.0, -3.0]);
/// ```
#[inline]
pub fn neg<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Neg).expect("neg")
}

/// Element-wise absolute value.
#[inline]
pub fn abs<T: Scalar, B: ElementwiseOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Abs).expect("abs")
}

/// Element-wise square root.
///
/// # Examples
///
/// ```
/// use coeus_tensor::Tensor;
/// use coeus_core::SequentialBackend;
/// use coeus_ops::sqrt;
///
/// let backend = SequentialBackend::new();
/// let a = Tensor::<f32, SequentialBackend>::from_slice([3], &[1.0, 4.0, 9.0]);
/// let b = sqrt(&a, &backend);
/// let s = b.as_slice();
/// assert!((s[0] - 1.0).abs() < 1e-5);
/// assert!((s[1] - 2.0).abs() < 1e-5);
/// assert!((s[2] - 3.0).abs() < 1e-5);
/// ```
#[inline]
pub fn sqrt<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Sqrt).expect("sqrt")
}

/// In-place element-wise sine.
#[inline]
pub fn sin_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sin)?;
    Ok(())
}

/// In-place element-wise cosine.
#[inline]
pub fn cos_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Cos)?;
    Ok(())
}

/// In-place element-wise exponential.
#[inline]
pub fn exp_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Exp)?;
    Ok(())
}

/// In-place element-wise natural log.
#[inline]
pub fn log_assign<T: Float, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Log)?;
    Ok(())
}

/// In-place element-wise negation.
#[inline]
pub fn neg_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Neg)?;
    Ok(())
}

/// In-place element-wise absolute value.
#[inline]
pub fn abs_assign<T: Scalar, B: ElementwiseOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Abs)?;
    Ok(())
}

/// In-place element-wise square root.
#[inline]
pub fn sqrt_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sqrt)?;
    Ok(())
}

/// Element-wise reciprocal: 1/x.
#[inline]
pub fn recip<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Recip).expect("recip")
}

/// Element-wise signum: -1, 0, or 1.
#[inline]
pub fn sign<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Sign).expect("sign")
}

/// Element-wise floor.
#[inline]
pub fn floor<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Floor).expect("floor")
}

/// Element-wise ceil.
#[inline]
pub fn ceil<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Ceil).expect("ceil")
}

/// Element-wise round to nearest integer.
#[inline]
pub fn round<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Round).expect("round")
}

/// Element-wise truncation toward zero.
#[inline]
pub fn trunc<T: Scalar, B: BackendOps<T>>(input: &Tensor<T, B>, backend: &B) -> Tensor<T, B> {
    elementwise_unary(input, backend, UnaryOp::Trunc).expect("trunc")
}

/// In-place element-wise reciprocal.
#[inline]
pub fn recip_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Recip)?;
    Ok(())
}

/// In-place element-wise signum.
#[inline]
pub fn sign_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Sign)?;
    Ok(())
}

/// In-place element-wise floor.
#[inline]
pub fn floor_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Floor)?;
    Ok(())
}

/// In-place element-wise ceil.
#[inline]
pub fn ceil_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Ceil)?;
    Ok(())
}

/// In-place element-wise round.
#[inline]
pub fn round_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Round)?;
    Ok(())
}

/// In-place element-wise truncation.
#[inline]
pub fn trunc_assign<T: Scalar, B: BackendOps<T>>(
    input: &mut Tensor<T, B>,
    backend: &B,
) -> Result<(), B::Error> {
    elementwise_unary_assign(input, backend, UnaryOp::Trunc)?;
    Ok(())
}